import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STChange, STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { UsermanageService } from '../../../services/usercenter.service';
import { AuditAdminComponent } from './audit-admin/audit-admin.component';

@Component({
  selector: 'app-Freight-components-enterprise-audit',
  templateUrl: './enterprise-audit.component.html',
  styleUrls: ['./enterprise-audit.component.less']
})
export class FreightComponentsEnterpriseAuditComponent implements OnInit {
  _$expand = false;
  ui: SFUISchema = { '*': { spanLabelFixed: 90, grid: { lg: 8, md: 12, sm: 12, xs: 24 } } };
  schema: SFSchema = this.initSF();
  enterColumns: STColumn[] = this.initEnterST();
  adminColumns: STColumn[] = this.initAdminST();
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  tabType = 1;
  tabs = [
    {
      name: '企业审核',
      type: 1,
      isActived: false
    },
    {
      name: '企业管理员审核',
      type: 2,
      isActived: false
    }
  ];
  constructor(public service: UsermanageService, private router: Router, private modal: NzModalService, private ar: ActivatedRoute) {}

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {}, {
      flag: this.tabType
    });
    delete params._$expand;
    return { ...params, listSource: 2 };
  }

  /**
   * 伸缩查询条件
   */
  expandToggle(status: boolean) {
    this._$expand = status;
    this.sf?.setValue('/expand', this._$expand);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }
  /**
   * 程序初始化入口
   */
  ngOnInit() {}

  /**
   * 数据列表状态变化事件
   */
  change(e: STChange) {
    // console.log(e.checkbox);
  }

  /**
   * 查看详情
   */
  ViewAdimin(record: any) {
    const modal = this.modal.create({
      nzContent: AuditAdminComponent,
      nzComponentParams: { i: { ...record } },
      nzFooter: [
        {
          label: '驳回',
          type: 'primary',
          onClick: instance => {
            if (!instance?.sf.value.approvalOpinion) {
              this.service.msgSrv.warning('请填写备注');
              return false;
            }
            this.adminAuditUser(
              {
                id: record.id,
                approvalStatus: '30',
                approvalOpinion: instance?.sf.value.approvalOpinion
              },
              modal
            );
            return;
          }
        },
        {
          label: '通过',
          type: 'primary',
          onClick: () => {
            this.adminAuditUser(
              {
                id: record.id,
                approvalStatus: '20'
              },
              modal
            );
          }
        }
      ]
    });
    modal.afterClose.subscribe(res => {
      // this.st.load(1);
    });
  }

  View(record: any) {
    this.router.navigate(['./view', record.id], {
      relativeTo: this.ar
    });
  }

  // 切换Tab
  changeTab(item: any) {
    this.tabType = item.type;
    this.expandToggle(false);
    this.sf?.reset();
    // setTimeout(() => {
    //   this.st.load(1);
    // }, 100);
  }

  private adminAuditUser(params: any, modal: any) {
    this.service.request(this.service.$api_audit_enterprise_admin, params, 'POST', false).subscribe(res => {
      console.log(res);
      if (res) {
        this.service.msgSrv.success('审核成功');
        modal.destroy();
      }
    });
  }

  /**
   * 初始化数据列表
   */
  initEnterST(): STColumn[] {
    return [
      { title: '企业名称', className: 'text-center', index: 'enterpriseName' },
      { title: '联系人', className: 'text-center', index: 'contacter' },
      { title: '手机号', className: 'text-center', index: 'mobile' },
      { title: '申请时间', className: 'text-center', index: 'createTime' },
      {
        title: '审核状态',
        className: 'text-center',
        index: 'approvalStatus',
        type: 'badge',
        badge: {
          10: { text: '待审核', color: 'processing' },
          20: { text: '已成功', color: 'success' },
          30: { text: '审核失败', color: 'warning' }
        }
      },
      {
        title: '常用服务',
        className: 'text-center',
        index: 'oftenUsedServices',
        type: 'enum',
        enum: { 10: '整车发货', 20: '大宗发货' }
      },
      { title: '推广业务员', className: 'text-center', index: 'promotersTelephone' },
      {
        title: '操作',
        fixed: 'right',
        width: '180px',
        className: 'text-center',
        buttons: [{ text: '查看', click: _record => this.View(_record) }]
      }
    ];
  }

  initAdminST(): STColumn[] {
    return [
      { title: '企业名称', className: 'text-center', index: 'enterpriseName' },
      { title: '当前管理员', className: 'text-center', index: 'oldAdminName' },
      { title: '当前管理员手机号', className: 'text-center', index: 'oldAdminMobile' },
      { title: '转授对象', className: 'text-center', index: 'newAdminName' },
      { title: '转授对象手机号', className: 'text-center', index: 'newAdminMobile' },
      { title: '申请时间', className: 'text-center', index: 'createTime' },
      {
        title: '状态',
        className: 'text-center',
        index: 'approvalStatus',
        type: 'badge',
        badge: {
          10: { text: '待审核', color: 'processing' },
          20: { text: '已成功', color: 'success' },
          30: { text: '已驳回', color: 'warning' }
        }
      },
      {
        title: '操作',
        fixed: 'right',
        width: '180px',
        className: 'text-center',
        buttons: [{ text: '审核', click: _record => this.ViewAdimin(_record) }]
      }
    ];
  }

  /**
   * 初始化查询表单
   */
  initSF(): SFSchema {
    return {
      properties: {
        expand: { type: 'boolean', ui: { hidden: true } },
        enterpriseName: {
          title: '企业名称',
          type: 'string',
          ui: {
            placeholder: '请输入'
          }
        },
        contactName: {
          title: '联系人',
          type: 'string',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => this.tabType === 1
            }
          }
        },
        contactName2: {
          title: '管理员',
          type: 'string',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => this.tabType !== 1
            }
          }
        },
        mobile: {
          title: '手机号',
          type: 'string',
          format: 'mobile',
          maxLength: 11,
          ui: {
            placeholder: '请输入'
          }
        },
        lockedStatus: {
          type: 'string',
          title: '状态',
          enum: [
            { label: '全部', value: '' },
            { label: '正常', value: 0 },
            { label: '冻结', value: 1 }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }
}
