import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { copy } from '@delon/util';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { EAEnterpriseService } from 'src/app/shared/services/business/enterprise.service';
import { SystemService } from '../../../services/system.service';

@Component({
  selector: 'app-system-add',
  templateUrl: './staff-modal.component.html',
  styleUrls: ['./staff-modal.less']
})
export class SystemStaffStaffModalComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  i: any;
  schema!: SFSchema;
  ui!: SFUISchema;
  roleList = [];
  roleNames: any = [];
  constructor(
    private modal: NzModalRef,
    public msgSrv: NzMessageService,
    public service: SystemService,
    private enterpriseSrv: EAEnterpriseService
  ) {}

  ngOnInit(): void {
    if (this.i?.id !== 0) {
      this.i.roleIds = this.i.roleId !== '' ? this.i.roleId.split(',') : [];
    }
    
    this.initSF(this.i);
  }
  initSF(staff: any) {
    console.log(staff);
    this.schema = {
      properties: {
        name: {
          title: '员工姓名',
          type: 'string',
          maxLength: 32,
          ui: { widget: staff?.name ? 'text' : 'string', placeholder: '请输入员工姓名' },
          default: staff.name
        },
        phone: {
          title: '手机号码',
          type: 'string',
          format: 'mobile',
          maxLength: 11,
          ui: { widget: staff?.phone ? 'text' : 'string', placeholder: '请输入员工手机号' },
          default: staff.phone
        },
        roleIds: {
          title: '角色',
          type: 'string',
          ui: {
            widget: 'select',
            placeholder: '请选择员工角色',
            mode: 'multiple',
            maxMultipleCount: 5,
            // asyncData: () => {
            //   return this.service.request(this.service.$api_getAppRoleList).pipe(
            //     map((res: any) => {
            //       this.roleList = res;
            //       return res.map((item: any) => {
            //         return { label: item.roleName, value: item.id };
            //       });
            //     }),
            //   );
            // },
            change: (i: any) => {
              this.sf.value.roleIds = i;
              this.sf?.setValue('/roleIds', i);
            }
          },
          default: staff?.roleIds
        }
      },
      required: ['name', 'phone', 'roleIds']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 24 }
      }
    };
  }

  sure() {
    if (!this.sf.value.roleIds || this.sf.value.roleIds.length === 0) {
      this.service.msgSrv.error('员工角色不能为空！');
      return;
    }
    this.roleNames = [];
    this.roleList.forEach((item: { id: any; roleName: string }) => {
      this.sf.value.roleIds.forEach((ele: any) => {
        if (ele === item.id) {
          this.roleNames.push(item.roleName);
        }
      });
    });
    if (this.i.id === 0) {
      const params: any = {
        ...this.sf.value,
        roleId: this.sf.value.roleIds,
        roleNames: this.roleNames.join(','),
        telephone: this.sf.value.phone,
        staffName: this.sf.value.name
      };
      // this.service.request(this.service.$api_addStaff, params).subscribe((res) => {
      //   console.log(res);
      //   if (res) {
      //     this.service.msgSrv.success('保存成功！');
      //     this.modal.close(true);
      //   }
      //   // this.showInviteFlag = true;
      //   // this.inviteCode = res.inviteCode;
      // });
    } else {
      const params: any = {
        appUserId: this.i.appUserId,
        staffName: this.sf.value.name,
        roleId: this.sf.value.roleIds,
        telephone: this.i.telephone
      };
      // this.service.request(this.service.$api_editorStaff, params).subscribe((res) => {
      //   this.service.msgSrv.success('编辑成功！');
      //   // this.loadMyIdentity();
      //   this.modal.close(true);
      // });
    }
  }
  loadMyIdentity() {
    this.enterpriseSrv.loadEnterpises().subscribe((data: any[]) => {
      this.enterpriseSrv.setCache(data);
    });
  }

  close() {
    this.modal.destroy();
  }
}
