import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Route, Router, RouterOutlet } from '@angular/router';
import { CompTableListComponent } from '../../../app/comp-table/comp-table-list/comp-table-list.component';
import _Role from '../../../schemas/_Role';
// import { TranslateService } from '@ngx-translate/core';
import * as Parse from 'parse';
import { CommonModule } from '@angular/common';
import { Department } from '../../../schemas/Department';
import { NzSpaceModule } from 'ng-zorro-antd/space';
import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
import { CommonCompModule } from '../../../services/common.modules';
import { NzModalModule, NzModalService } from 'ng-zorro-antd/modal';
import {
  NzFormatEmitEvent,
  NzTreeModule,
  NzTreeNode,
} from 'ng-zorro-antd/tree';
import {
  NzContextMenuService,
  NzDropdownMenuComponent,
} from 'ng-zorro-antd/dropdown';
import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzRadioModule } from 'ng-zorro-antd/radio';
import { NzMessageService } from 'ng-zorro-antd/message';
import { textbookServer } from '../../../services/textbook';
import { NzSelectModule } from 'ng-zorro-antd/select';
import { NzSpinModule } from 'ng-zorro-antd/spin';

interface nodes {
  title: string;
  key: string;
  isLeaf?: boolean;
  isParent?: boolean;
  children?: Array<any>;
}
interface depart {
  name: string;
  id?: string;
  code?: string;
  desc?: string;
  parent?: object | any;
  branch: string;
}
@Component({
  selector: 'app-page-role',
  templateUrl: './page-role.component.html',
  styleUrls: ['./page-role.component.scss'],
  imports: [
    CommonModule,
    CommonCompModule,
    RouterOutlet,
    CompTableListComponent,
    NzSpaceModule,
    NzPageHeaderModule,
    NzBreadCrumbModule,
    NzTreeModule,
    NzCheckboxModule,
    NzEmptyModule,
    NzModalModule,
    NzRadioModule,
    NzSelectModule,NzSpinModule
  ],
  standalone: true,
})
export class PageRoleComponent implements OnInit {
  @ViewChild(CompTableListComponent) list: CompTableListComponent | undefined;

  // _Role = _Role
  Department = Department;
  user?: Parse.User;
  className: string | undefined;
  queryParams: any | undefined;
  fieldsArray: Array<any> | undefined;
  searchValue: string = ''; //搜索内容
  nodes: Array<nodes | any> = [];
  currentDepart: nodes | any = null;

  profiles: Array<any> = [];
  checkedShowFilter: boolean = false;

  checkedAll: boolean = false; //全选
  indeterminate = false;
  loading = false;
  pageSize: number = 10;
  pageIndex: number = 1;
  profileLength:number = 0

  isVisible: boolean = false;
  activatedNode: NzTreeNode | any; //当前选择节点
  editType: string = 'add'; //弹窗类型
  activeDepart?: Parse.Object; //当前编辑部门
  editObject: depart = {
    name: '',
    code: '',
    desc: '',
    parent: {},
    branch: '',
  };
  parentMap: Array<any> = [];
  parentList: Array<any> = [];
  radio: string = '';

  /* 添加账号 */
  accountIsVisible: boolean = false;
  account: any = {
    name: '',
    phone: '',
    email: '',
    password: '',
    identity: '',
    department: {},
    companyType: '',
  };
  userType: Array<string> = ['教师'];
  parents: Array<any> = []; //所有一级列表

  constructor(
    public tbookSer: textbookServer,
    private route: Router,
    private modal: NzModalService,
    private activeRoute: ActivatedRoute,
    private nzContextMenuService: NzContextMenuService,
    private message: NzMessageService
  ) {
    this.user = Parse.User.current();
    // this.className = this.Department.className;
    // this.fieldsArray = this.Department.fieldsArray;
    // this.queryParams = {
    //   where: {
    //     // user:this.user?.toPointer(),
    //     isDeleted: { $ne: true },
    //   },
    // };
  }

  ngOnInit(): void {
    this.activeRoute.paramMap.subscribe(async (params) => {
      this.refresh();
    });
  }
  async refresh() {
    if (!this.tbookSer.profile.user.department?.objectId) {
      this.message.warning('权限不足');
      history.back();
      return;
    }
    let query = new Parse.Query('Department');
    query.equalTo('objectId', this.tbookSer.profile.user.department?.objectId);
    query.select('code', 'name', 'branch', 'parent', 'type', 'hasChildren');
    let r = await query.first();
    let n = [
      {
        title: this.tbookSer.profile.user.department.name,
        key: this.tbookSer.profile.user.department?.objectId,
        children: [
          ...(await this.getDepart(this.tbookSer.profile.user.department?.objectId)),
        ],
        isParent: !r?.get('hasChildren'),
        isLeaf: !r?.get('hasChildren'),
        branch: r?.get('branch'),
        parent: r?.get('parent')?.id, //上级
        type: r?.get('branch'),
      },
    ];
    this.nodes = n
    let query2 = new Parse.Query('Department');
    query2.select('name');
    query2.equalTo('parent', null);
    this.parents = await query2.find();
  }
  async getDepart(
    parent?: string,
    searchValue?: string
  ): Promise<Array<nodes>> {
    console.log(searchValue);
    let nodes: any = [];
    let query = new Parse.Query('Department');
    query.equalTo('parent', parent ? parent : this.tbookSer.profile.user.department?.objectId);
    searchValue && query.contains('name', searchValue);
    query.notEqualTo('isDeleted', true);
    query.select('code', 'name', 'branch', 'parent', 'type', 'hasChildren');
    query.descending('createdAt');
    query.equalTo('branch', this.tbookSer.profile.companyType);
    // if (searchValue && searchValue != undefined) {
    //   query.equalTo('parent', this.tbookSer.profile.user.department?.objectId);
    // }
    query.limit(100);
    if (this.activeDepart) query.notEqualTo('objectId', this.activeDepart?.id);
    let res = await query.find();
    res.forEach((item) => {
      nodes.push({
        title: item.get('name'),
        key: item.id,
        children: [],
        branch: item.get('branch'),
        parent: item.get('parent')?.id, //上级
        isLeaf: !item.get('hasChildren') ? true : false, //是否是最下级
        type: item.get('type')
      });
    });
    return nodes;
  }
  //搜索
  async onSearchNodes(e: string, modal?: boolean, id?:string) {
    if (modal) {
      this.parentList = await this.getDepart(id, e);
      return;
    }
    this.nodes = await this.getDepart('', e);
  }
  //搜索失去焦点
  onblur(type:string){
    console.log(type);
    if(type == 'account'){
      this.modalValue = this.account.department?.title
    }else if(type == 'editObject'){
      this.modalValue = this.editObject.parent?.title
    }
  }
  reset(type:string){
    this.modalValue = ''
    this.parentList = this.nodes;
    this.parentMap = []
    if(type == 'account'){
      this.account.department = {}
      this.account.identity = ''
    }else if(type == 'editObject'){
      this.editObject.parent = {}
    }
  }
  //添加成员
  addMember() {
    this.radio = ''
    this.parentList = this.nodes;
    this.account = {
      name: '',
      phone: '',
      email: '',
      password: '',
      identity: '',
      department: {},
      companyType: '',
    };
    this.accountIsVisible = true;
  }
  //展开/合并
  async nzEvent(event: NzFormatEmitEvent): Promise<void> {
    console.log(event);
    let node: any = event.node;
    if (event.eventName === 'expand') {
      // if (node.origin.isParent) {
      //   node.addChildren([]);
      //   return;
      // }
      if (node?._children.length <= 0) {
        let data = await this.getDepart(node.key);
        node.addChildren(data);
      }
    } else {
      // if (node.origin.isParent) {
      this.currentDepart = node.origin;
      this.getProfile();
      // }
    }
  }
  async getProfile() {
    this.profiles = [];
    this.loading = true;
    let childrens = [this.currentDepart.key]
    if(!this.checkedShowFilter){
      childrens = await this.tbookSer.getChild(this.currentDepart.key)
    }
    let queryParams = {
      where: {
        $or: [
          {
            user: {
              $inQuery: {
                where: {
                  $or: [
                    {
                      department: { $in: [childrens] },
                    },
                  ],
                },
                className: '_User',
              },
            },
          },
        ],
      },
    };
    let query = Parse.Query.fromJSON('Profile', queryParams);
    query.include('user');
    if(this.tbookSer.profile.identity == '工作联系人'){
      query.containedIn('identity',['教师','高校联系人','评审专家'])
    }else if(this.tbookSer.profile.identity == '高校联系人'){
      query.containedIn('identity',['教师','评审专家'])
    }
    query.notEqualTo('identity', '国家级管理员');
    query.notEqualTo('isDeleted', true);
    this.profileLength = await query.count()
    query.limit(this.pageSize)
    query.skip((this.pageIndex - 1) * this.pageSize)
    let r = await query.find();
    // let profiles: any[] = [];
    // r.forEach((item) => {
    //   let _item = item.toJSON();
    //   _item['checked'] = false;
    //   profiles.push(_item);
    // });
    this.profiles = r;
    this.loading = false;
  }
  //分页切换
  pageIndexChange(e: any) {
    console.log(e);
    this.pageIndex = e;
    this.getProfile();
  }
  onChecked(){
    this.pageIndex = 1;
    this.getProfile()
  }
  //搜索触发
  onSearch(event: NzFormatEmitEvent) {
    console.log(event);
  }
  contextMenu(
    $event: MouseEvent,
    menu: NzDropdownMenuComponent,
    node?: any
  ): void {
    console.log(node);
    this.activatedNode = node;
    this.nzContextMenuService.create($event, menu);
  }
  //删除部门
  onDelDepart() {
    console.log(this.activatedNode);
    this.modal.confirm({
      nzTitle: '删除',
      nzContent: '删除后数据不可恢复，请谨慎操作',
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        new Promise(async (resolve, reject) => {
          if (this.activatedNode?.key) {
            let query = new Parse.Query('Department');
            let r = await query.get(this.activatedNode?.key);
            if (r?.id) {
              r.set('isDeleted', true);
              await r.save();
            }
            this.message.success('删除成功');
            this.ngOnInit();
            resolve(true);
          }
        }).catch(() => console.log('Oops errors!'));
      },
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel'),
    });
  }

  onAllChecked(checked: boolean): void {
    console.log(checked);
    this.profiles = this.profiles.map((item) => {
      item.checked = checked;
      return item;
    });
    this.checkedAll = checked;
  }
  onItemChecked(id: string, e: boolean) {
    let checkedAll = true;
    this.profiles = this.profiles.map((item) => {
      if (id == item.objectId) item.checked = e;
      if (!item.checked) checkedAll = false;
      return item;
    });
    this.checkedAll = checkedAll;
  }
  //新建打开弹窗
  async showModalDepart(type: string) {
    this.radio = ''
    this.parentList = this.nodes;
    this.editObject = {
      name: '',
      code: '',
      desc: '',
      parent: '',
      branch: '',
    };
    if (type == 'edit') {
      let query = new Parse.Query('Department');
      let r = await query.get(this.activatedNode?.key);
      this.activeDepart = r;
      this.editObject = {
        name: this.activeDepart.get('name'),
        code: this.activeDepart.get('code'),
        desc: this.activeDepart.get('desc'),
        parent: {
          title: this.activeDepart.get('parent')?.get('name'),
          id: this.activeDepart.get('parent')?.id,
        },
        branch: this.activeDepart.get('branch'),
      };
      this.parentMap = await this.tbookSer.formatNode(
        this.activeDepart.get('parent')?.id
      );
      if (this.activatedNode?.parentNode?.origin?.parentNode?.origin.key) {
        this.parentList = await this.getDepart(
          this.activatedNode?.parentNode?.origin?.parentNode?.origin.key
        );
      }
    } else if (type == 'add' && this.activatedNode) {
      console.log(this.activatedNode);
      this.editObject.parent = {
        title: this.activatedNode.origin.title,
        id: this.activatedNode.origin.key,
      };
      this.editObject.branch =
        this.activatedNode.origin.branch || this.activatedNode.origin.title;
      this.parentMap = await this.tbookSer.formatNode(this.activatedNode.origin.key);
      if (this.activatedNode?.parentNode?.origin.key) {
        this.parentList = await this.getDepart(
          this.activatedNode.parentNode.origin.key
        );
      }
    }
    console.log(this.parentMap);
    this.editType = type;
    this.isVisible = true;
    this.resetChange()
  }
  async onPre(type:string, data?: any, index?: number) {
    console.log(data);
    if (!data?.key || !data.hasChildren) {
      // this.parentList = await this.getDepart();
      // this.parentMap = []
      return;
    }
    // if(index == this.parentMap.length-1) return
    this.parentMap.splice((index || 0) + 1);
    this.radio = '';
    this.pushValue(type,this.parentMap[this.parentMap.length - 1])
    this.parentList = this.parentMap[this.parentMap.length - 1].key == this.tbookSer.profile.user.department?.objectId ? this.nodes : await this.getDepart(data?.parent);
  }
  //选择所属类别下级列表
  async onCheckedDepart(type: string, e: any, checked?: boolean) {
    this.radio = '';
    console.log(e);
    if (type == 'account') this.account.identity = '';
    this.parentMap = await this.tbookSer.formatNode(e.key);
    let index = this.parentMap.findIndex(
      (item) => this.tbookSer.profile.user.department?.objectId == item.key
    );
    if (index != -1) {
      this.parentMap.forEach((item, i) => {
        if (i >= index) {
          this.parentMap[i].verify = true;
        }
      });
    }
    if (checked || e.isLeaf) {
      this.pushValue(type,e)
      return;
    }
    if (e.isLeaf) {
      return;
    }
    this.parentList = await this.getDepart(e?.key);
  }
  modalValue:string = ''
  //赋值
  pushValue(type:string, e: any){
    console.log(e);
    this.radio = e.key;
    this.modalValue = e.title
    if (type == 'account') {
      this.account.department = { title: e.title, id: e.key };
      this.account.companyType = e.branch || e.title;
      if(this.tbookSer.profile.identity == '工作联系人'){
        this.userType = this.parents.some((item) => e.parent == item.id)
        ? ['评审专家', '教师','高校联系人']
        : e.type == '单位'
        ? ['评审专家', '高校联系人', '教师']
        : ['评审专家', '教师'];
      }
    } else {
      this.editObject.parent = {
        title: e.title,
        id: e.key,
      };
      this.editObject.branch = e.branch || e.title;
    }
  }
  async handleOk(): Promise<void> {
    if (!this.editObject?.name || !this.editObject.parent?.id) {
      this.message.error('请填写完整信息');
      return;
    }
    if (this.activeDepart?.id && this.editType == 'edit') {
      this.activeDepart.set('name', this.editObject?.name);
      this.activeDepart.set('code', this.editObject?.code);
      this.activeDepart.set('desc', this.editObject.desc);
      this.activeDepart.set('parent', {
        __type: 'Pointer',
        className: 'Department',
        objectId: this.editObject.parent?.id,
      });
      this.activeDepart.set('branch', this.editObject.branch);
    } else {
      let obj = Parse.Object.extend('Department');
      this.activeDepart = new obj();
      this.activeDepart?.set('name', this.editObject?.name);
      this.activeDepart?.set('code', this.editObject?.code);
      this.activeDepart?.set('desc', this.editObject.desc);
      this.activeDepart?.set('parent', {
        __type: 'Pointer',
        className: 'Department',
        objectId: this.editObject.parent?.id,
      });
      this.activeDepart?.set('branch', this.editObject.branch);
    }
    await this.activeDepart?.save();
    if(this.activeDepart?.id){
      //判断添加的是部门还是单位
      let filters = ['出版单位','教育部直属高校']
      let leng = await this.tbookSer.formatNode(this.activeDepart.id)
      if(leng.length > 2){
        console.log(leng.length);
        if(filters.includes(leng[0].title)){
          this.activeDepart?.set('type', '部门');
        }else{
          this.activeDepart?.set('type',leng.length > 3 ? '部门' : '单位');
        }
        leng.slice()
        await this.activeDepart?.save();
      }
    }
    await this.updateChildren();
    this.isVisible = false;
    this.message.success(this.editType == 'edit' ? '保存' : '添加' + '成功');
    this.activeDepart = undefined;
    this.ngOnInit();
  }
  //更新上级children字段
  async updateChildren() {
    let query = new Parse.Query('Department');
    query.equalTo('objectId', this.editObject.parent?.id);
    query.select('hasChildren')
    let r = await query.first();
    if (r?.id && !r.get('hasChildren')) {
      r?.set('hasChildren', true);
      await r.save();
    }
    return;
  }
  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.modalValue = ''
    this.isVisible = false;
    this.activatedNode = undefined;
    this.parentMap = [];

    this.accountIsVisible = false;
    this.account = null;
  }

  /* 组织 */
  showModalOrganize() {
    this.message.warning('权限灰度中');
  }
  goDateil(id: string) {
    this.route.navigate(['/nav-admin/manage/user/edit', { id: id }]);
  }
  randomPassword() {
    this.account.password = this.tbookSer.randomPassword();
  }
  /* 添加账号 */
  isLoadingOne:boolean = false
  async accountComplete() {
    if(this.isLoadingOne) return
    this.isLoadingOne = true
    this.account.email = this.account?.email.trim();
    this.account.phone = this.account?.phone.trim();
    this.account.name = this.account?.name.trim();
    this.account.password = this.account?.password.trim();
    if(!await this.authVrifly()){
      this.isLoadingOne = false
      return
    }
    try {
      let obj = Parse.Object.extend('_User');
      let user = new obj();
      user?.set('username', this.account?.email || this.account?.phone);
      user?.set('name', this.account?.name);
      user?.set('phone', this.account?.phone);
      this.account?.email && user?.set('email', this.account?.email);
      user?.set('password', this.account.password);
      user?.set('accountState', '已认证');
      user?.set('department', {
        __type: 'Pointer',
        className: 'Department',
        objectId: this.account.department?.id,
      });
      let u = await user.save();
      let p = Parse.Object.extend('Profile');
      let profile = new p();
      profile?.set('user', u?.toPointer());
      profile?.set('companyType', this.account.companyType);
      profile?.set('email', this.account.email);
      profile?.set('identity', this.account.identity);
      let res = await profile?.save();
      this.isLoadingOne = false
      this.accountIsVisible = false;
      this.account = null;
      Parse.Cloud.run('aliSmsSend',{
        "mobileList": [this.account?.phone],"templateCode":"SMS_469060724","params":{},"signName":"普通高等教育教材网"
      })
      this.modal.success({
        nzTitle: '添加成功',
        nzContent: '',
        nzOnOk: () => {
          this.currentDepart && this.getProfile();
        },
      });
    } catch (err: any) {
      console.warn('添加用户错误', err);
      this.isLoadingOne = false
      this.message.error(err?.Error || '错误：请检查用户邮箱是否已存在');
      return;
    }
  }
  async authVrifly():Promise< boolean | undefined>{
    this.account.email = this.account?.email.trim();
    this.account.phone = this.account?.phone.trim();
    this.account.name = this.account?.name.trim();
    this.account.password = this.account?.password.trim();
    if(!this.account?.name|| !this.account.department?.id || !this.account.password
      || !this.account.phone || !this.account?.email){
      this.message.warning('请填写必填项');
      return
    }
    if(!this.account.identity){
      this.message.error("请选择人员类型");
      return;
    }
    let a = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
    if(this.account.phone && !String(this.account.phone).match(a)){
      this.message.error("请填写正确手机号");
      return;
    }
    let m = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    if(!String(this.account.email).match(m)){
      this.message.error("邮箱格式有误");
      return;
    }
    // if(!this.account?.email && !this.account?.phone){
    //   this.message.error("邮箱或手机号必须填写一项");
    //   return;
    // }
    console.log(this.account.password);
    if(!(this.account.password.length >= 6 && this.account.password.length <= 18)){
      this.message.error('密码格式错误，请填写6-18位非空字符串（数字、大小写字母或英文符号）');
      return;
    }
    if(!await this.tbookSer.userFind(this.account.phone)){
      this.message.error('手机号已存在');
      return
    }
    return true
  }


  setOfCheckedId = new Set<string>();
  //移除部门
  async removeBranch(data: Parse.Object) {
    if (data?.get('user')?.id) {
      data?.get('user')?.set('department', null);
      await data?.get('user')?.save();
      this.message.error('移除成功');
      this.getProfile();
    }
  }
  removeBranchAll() {
    this.modal.confirm({
      nzTitle: '批量移除',
      nzContent: `请谨慎操作`,
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        let selectedList = this.profiles.filter((item: any) =>
          this.setOfCheckedId.has(item?.id)
        );
        let romovePromiseList = selectedList.map((item: any) => {
          return new Promise(async (resolve) => {
            item?.get('user')?.set('department', null);
            await item?.get('user')?.save();
            resolve(true);
          });
        });
        try {
          await Promise.all(romovePromiseList);
          this.message.error('移除成功');
          this.getProfile();
          this.resetChange();
        } catch (err) {}
      },
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel'),
    });
  }
  resetChange() {
    this.setOfCheckedId = new Set<string>();
    this.checkedAll = false;
  }
}
