import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STColumnBadge, 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';
@Component({
  selector: 'app-Freight-components-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class FreightComponentsListComponent implements OnInit {
  _$expand = false;
  ui!: SFUISchema;
  schema!: SFSchema;
  columns!: STColumn[];
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  
  @ViewChild('promoterModal', { static: false })
  promoterModal!: any;
  promotersTelephone = '';
  constructor(public service: UsermanageService, private modal: NzModalService, private router: Router, private ar: ActivatedRoute) {}

  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = {
      ...(this.sf && this.sf.value),
      listSource: 1
    };
    if (this.sf?.value.effectiveDate) {
      params.effectiveDateStart = this.sf?.value.effectiveDate[0];
      params.effectiveDateEnd = this.sf?.value.effectiveDate[1];
    }
    delete params.effectiveDate;
    delete params.expand;
    return params;
  }

  ngOnInit() {
    this.initSF();
    this.initST();
    this.ar.url.subscribe(params => {
      this.st?.load(1);
    });
  }
  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.showSortFlag = false;
      return i;
    });
  }
  initSF() {
    this.schema = {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        enterpriseName: {
          title: '企业名称',
          type: 'string',
          ui: {
            placeholder: '请输入',
            showRequired: false
          }
        },
        contactName: {
          title: '联系人',
          type: 'string',
          ui: {
            placeholder: '请输入',
            showRequired: false
          }
        },
        networkTransporter: {
          title: '网络货运人',
          type: 'string',
          ui: {
            placeholder: '请输入',
            showRequired: false
          }
        },
        source: {
          type: 'string',
          title: '注册渠道',
          enum: [
            { label: '全部', value: '' },
            { label: '自己录入', value: 1 },
            { label: '运营录入', value: 2 }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        crmStatus: {
          type: 'string',
          title: 'CRM审核状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待审核', value: '10' },
            { label: '审核通过', value: '20' },
            { label: '驳回', value: '30' }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        lockedStatus: {
          type: 'string',
          title: '企业状态',
          enum: [
            { label: '全部', value: '' },
            { label: '正常', value: 0 },
            { label: '冻结', value: 1 }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
    this.ui = { '*': { spanLabelFixed: 110, grid: { lg: 8, md: 12, sm: 12, xs: 24 }, enter: () => this.st.load() } };
  }

  initST() {
    this.columns = [
      // { title: '', type: 'checkbox', className: 'text-center' },
      { title: '企业名称', className: 'text-center', index: 'enterpriseName' },
      { title: '联系人', className: 'text-center', index: 'contacter' },
      { title: '网络货运人', className: 'text-center', index: 'networkTransporter' },
      { title: '常用服务', className: 'text-center', index: 'oftenUsedServices', type: 'enum', enum: { 10: '整车发货', 20: '大宗发货' } },
      { title: '推广业务员', className: 'text-center', render: 'promotersTelephone' },
      { title: '注册渠道', className: 'text-center', index: 'source', type: 'enum', enum: { 1: '自己录入', 2: '运营录入' } },
      {
        title: 'CRM审核状态',
        className: 'text-center',
        index: 'crmStatus',
        type: 'badge',
        badge: {
          10: { text: '待审核', color: 'default' },
          20: { text: '已成功', color: 'success' },
          30: { text: '审核失败', color: 'warning' }
        }
      },
      {
        title: '企业状态',
        className: 'text-center',
        index: 'lockedStatus',
        type: 'badge',
        badge: {
          0: { text: '正常', color: 'success' },
          1: { text: '冻结', color: 'error' }
        }
      },
      {
        title: '操作',
        width: '170px',
        className: 'text-center',
        buttons: [
          {
            text: '查看',
            click: item => {
              this.router.navigate(['./detail', item.id], { relativeTo: this.ar });
              // this.router.navigate(['./view', item.id], { relativeTo: this.ar, queryParams: { tenantId: item.tenantId } });
            }
          }
        ]
      }
    ];
  }
  daoyun(item: any) {
    this.router.navigate(['./view', item.tenantId], { relativeTo: this.ar });
  }

  addPromoter(item?: any) {
    this.promotersTelephone = item?.promotersTelephone;
    const modal = this.modal.create({
      nzTitle: '推广业务员',
      nzContent: this.promoterModal,
      nzOnOk: () => {
        if (!!!this.promotersTelephone) {
          return false;
        }
        if (typeof this.promotersTelephone === 'string' && !/(^1\d{10}$)/.test(this.promotersTelephone)) {
          return false;
        }
        this.service.request(this.service.$api_add_salesman, { ids: [item.id], salesmanMobile: this.promotersTelephone }).subscribe(res => {
          if (res) {
            this.service.msgSrv.success(item?.promotersTelephone ? '添加推广员成功' : '修改推广员成功');
          }
          this.st.load();
        });
        return;
      }
    });
  }

  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }
  creat() {
    this.router.navigate(['./new'], { relativeTo: this.ar });
  }

  exportList() {
    const params = this.reqParams;
    this.service
      .request(this.service.$api_export_freight_list, { ...params, pageSize: this.st.ps, pageIndex: this.st.pi })
      .subscribe(res => {
        if (res) {
          this.service.msgSrv.success('导出成功');
        }
      });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
    this.st.reload();
  }
}
