import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STColumnBadge, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { UsermanageService } from '../../services/usercenter.service';
@Component({
  selector: 'app-usercenter-components-driver',
  styleUrls: ['./driver.component.less'],
  templateUrl: './driver.component.html'
})
export class UserCenterComponentsDriverComponent implements OnInit {
  _$expand = false;
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  ui: SFUISchema = { '*': { spanLabelFixed: 120, grid: { lg: 8, md: 12, sm: 12, xs: 24 }, enter: () => this.st.load() } };
  schema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();

  @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)
    };
    if (this.sf?.value.effectiveDate) {
      Object.assign(params, {
        time: {
          start: this.sf?.value.effectiveDate[0],
          end: this.sf?.value.effectiveDate[1]
        }
      });
      // params.effectiveDateStart = this.sf?.value.effectiveDate[0];
      // params.effectiveDateEnd = this.sf?.value.effectiveDate[1];
    }
    delete params.effectiveDate;
    delete params.expand;
    return params;
  }

  ngOnInit() {
    this.ar.url.subscribe(params => {
      this.st?.load(1);
    });
  }

  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);
  }
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        name: { title: '司机姓名', type: 'string', ui: { placeholder: '请输入', showRequired: false } },
        mobile: {
          title: '手机号',
          type: 'string',
          format: 'mobile',
          maxLength: 11,
          ui: {
            placeholder: '请输入'
          }
        },
        identityStatus: {
          type: 'string',
          title: '实名状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待审核', value: 0 },
            { label: '通过', value: 1 },
            { label: '驳回', value: 2 }
          ],
          default: '',
          ui: {
            widget: 'select'
          }
        },
        driverLicenseStatus: {
          type: 'string',
          title: '驾驶证状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待审核', value: 10 },
            { label: '审核通过', value: 20 },
            { label: '驳回', value: 30 },
            { label: '证件过期', value: 40 }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        practiceSenioritLicenseStatus: {
          type: 'string',
          title: '从业资格证状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待审核', value: 10 },
            { label: '审核通过', value: 20 },
            { label: '驳回', value: 30 },
            { label: '证件过期', value: 40 }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        promotersTelephone: {
          title: '推广业务员',
          type: 'string',
          format: 'mobile',
          maxLength: 11,
          ui: {
            placeholder: '请输入手机号',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        source: {
          type: 'string',
          title: '注册渠道',
          enum: [
            { label: '全部', value: '' },
            { label: '自己录入', value: 1 },
            { label: '货主录入', value: 2 }
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        effectiveDate: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '司机姓名', className: 'text-center', index: 'name' },
      { title: '手机号', className: 'text-center', index: 'mobile' },
      { title: '当前车辆', className: 'text-center', index: 'carNo' },
      {
        title: '实名认证状态',
        className: 'text-center',
        index: 'identityStatus',
        type: 'badge',
        badge: {
          0: { text: '待审核', color: 'default' },
          1: { text: '通过', color: 'success' },
          2: { text: '驳回', color: 'warning' }
        }
      },
      {
        title: '驾驶证状态',
        className: 'text-center',
        index: 'driverLicenseStatus',
        type: 'badge',
        badge: {
          10: { text: '待审核', color: 'default' },
          20: { text: '审核通过', color: 'success' },
          30: { text: '驳回', color: 'warning' },
          40: { text: '证件过期', color: 'error' }
        }
      },
      {
        title: '从业资格证状态',
        className: 'text-center',
        index: 'practiceSenioritLicenseStatus',
        type: 'badge',
        badge: {
          10: { text: '待审核', color: 'default' },
          20: { text: '审核通过', color: 'success' },
          30: { text: '驳回', color: 'warning' },
          40: { text: '证件过期', color: 'error' }
        }
      },
      { title: '推广业务员', className: 'text-center', render: 'promotersTelephone' },
      { title: '注册渠道', className: 'text-center', index: 'source', type: 'enum', enum: { 1: '自己录入', 2: '运营录入' } },
      { title: '注册时间', className: 'text-center', index: 'createTime' },
      {
        title: '操作',
        width: '170px',
        className: 'text-center',
        buttons: [
          {
            text: '查看',
            click: item => {
              this.router.navigate(['./detail', item.appUserId], { relativeTo: this.ar });
              // this.router.navigate(['./view', item.id], { relativeTo: this.ar, queryParams: { tenantId: item.tenantId } });
            }
          }
        ]
      }
    ];
  }
}
