import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemService } from '../../services/system.service';
import { SystemStaffStaffModalComponent } from './staff-modal/staff-modal.component';
import { BuyerTranspowerComponent } from './transpower/transpower.component';

@Component({
  selector: 'app-staff-management',
  templateUrl: './staff-management.component.html',
  styleUrls: ['./staff-management.component.less']
})
export class StaffManagementComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;

  url = `/rule?_allow_anonymous=true`;

  searchSchema: SFSchema = {
    properties: {
      receiveName: {
        type: 'string',
        title: '输入搜索',
        ui: { placeholder: '手机号码 / 成员姓名' }
      }
    }
  };

  columns: STColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },
    { title: '员工姓名', index: 'no' },
    { title: '手机号码', index: 'description' },
    { title: '角色', index: 'description' },
    {
      title: '最后登录时间',
      index: 'updatedAt',
      type: 'date'
    },
    {
      title: '成员状态',
      className: 'text-center',
      index: 'status',
      type: 'badge',
      badge: {
        0: { text: '正常', color: 'success' },
        2: { text: '废弃', color: 'warning' },
        3: { text: '废弃', color: 'warning' },
        1: { text: '冻结', color: 'error' }
      }
    },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          click: item => this.staffAction(item)
        },
        {
          text: '恢复',
          iif: item => item.status === 1,
          click: item => this.action(2)
        },
        {
          text: '冻结',
          iif: item => item.status === 0,
          click: item => this.action(1)
        },
        {
          text: '超管转授',
          iif: item => item.status === 0,
          click: item => this.transpowerAction(item)
        },
        {
          text: '删除',
          iif: item => item.status === 1,
          click: item => this.action(3)
        }
      ]
    }
  ];

  selectedRows: any[] = [];

  reqParams = { pageIndex: 1, pageSize: 10 };

  actionLabel = {
    1: { title: '确认冻结？', text: '冻结后用户在本系统将无法登录使用，请谨慎操作！' },
    2: { title: '确认恢复？', text: '恢复后用户在本系统的权限将一并重新开启。' },
    3: { title: '确认删除？', text: '删除后该用户ID将在本系统中将无法登录使用并删除，请谨慎操作！' }
  };
  constructor(public service: SystemService, private nzModalService: NzModalService) {}

  ngOnInit(): void {}

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }

  action(type: 1 | 2 | 3) {
    this.nzModalService.error({
      nzTitle: this.actionLabel[type].title,
      nzContent: `<label class="error-color">${this.actionLabel[type].text}</label>`,
      nzClosable: false,
      nzCancelText: '取消',
      nzOnOk: () => {
        switch (type) {
          case 1:
            break;

          default:
            break;
        }
      }
    });
  }

  transpowerAction(item: any) {
    const modal = this.nzModalService.create({
      nzContent: BuyerTranspowerComponent,
      nzComponentParams: { i: { ...item } },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      this.st.load();
    });
  }

  staffAction(item?: any) {
    const modal = this.nzModalService.create({
      nzContent: SystemStaffStaffModalComponent,
      nzComponentParams: item ? { i: { ...item, roleId: '1,2,3', name: '用户名', phone: 18555555555 } } : { i: { id: 0 } },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      this.st.load();
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
}
