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

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

  ngOnInit(): void {
    this.initSF(this.i);
  }
  initSF(staff: any) {
    this.schema = {
      properties: {
        staffName: {
          title: '员工姓名',
          type: 'string',
          maxLength: 10,
          ui: { widget: staff?.appUserId ? 'text' : 'string', placeholder: '请输入员工姓名' },
          default: staff.name
        },
        telephone: {
          title: '手机号码',
          type: 'string',
          maxLength: 11,
          ui: { widget: staff?.appUserId ? 'text' : 'string', placeholder: '请输入员工手机号' },
          default: staff.telephone
        },
        roleId: {
          title: '角色',
          type: 'string',
          ui: {
            widget: 'select',
            placeholder: '请选择员工角色',
            mode: 'multiple',
            maxMultipleCount: 5,
            asyncData: () => {
              return this.service.request(this.service.$api_getAppRoleList).pipe(
                map((res: any) => {
                  return res
                    .filter((role: any) => role.roleCode !== 'Administrator')
                    .map((item: any) => {
                      return { label: item.roleName, value: item.id };
                    });
                })
              );
            }
          },
          default: staff?.roleId || null
        }
      },
      required: ['staffName', 'telephone', 'roleId']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 24 }
      }
    };
  }

  sure() {
    if (!this.sf.value.roleId || this.sf.value.roleId.length === 0) {
      this.service.msgSrv.error('员工角色不能为空！');
      return;
    }
    if (this.i.userId === 0) {
      const params: any = {
        ...this.sf.value
      };
      this.service.request(this.service.$api_add_staff, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('保存成功！');
          this.modal.close(true);
        }
      });
    } else {
      const params: any = {
        appUserId: this.i.appUserId,
        ...this.sf.value
      };
      this.service.request(this.service.$api_edit_staff, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('编辑成功！');
          this.modal.close(true);
        }
      });
    }
  }

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