import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { DynamicSettingModalComponent } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { UsermanageService } from '../../../services/usercenter.service';
@Component({
  selector: 'app-usercenter-components-driver-config',
  templateUrl: './driver-config.component.html',
  styleUrls: ['./driver-config.component.less']
})
export class UserCenterComponentsDriverConfigComponent implements OnInit {
  schema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;

  constructor(public service: UsermanageService, private modal: NzModalService) {}

  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = {
      auditStatus: 20,
      ...(this.sf && this.sf.value)
    };
    delete params.effectiveDate;
    delete params.expand;
    return params;
  }

  ngOnInit() {}

  settingAction(item?: any) {
    const modal = this.modal.create({
      nzTitle: '配置',
      nzContent: DynamicSettingModalComponent,
      nzWidth: 900,
      nzComponentParams: {
        extendType: '3',
        businessId: item.appUserId,
        configvalue: 'sys.config'
      },
      nzFooter: null
    });

    modal.afterClose.subscribe(res => {
      if (res) {
        this.st.load(1);
      }
    });
  }

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

  exportList() {
    const params = this.reqParams;
    this.service.downloadFile(this.service.$api_driver_exportConfig, { ...params, pageSize: -1 });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        name: { title: '司机姓名', type: 'string', ui: { placeholder: '请输入', showRequired: false } },
        mobile: {
          title: '手机号',
          type: 'string',
          maxLength: 11,
          ui: {
            placeholder: '请输入'
          }
        },
        isCaptain: {
          type: 'string',
          title: '类型',
          enum: [
            { label: '全部', value: '' },
            { label: '车队长', value: 1 },
            { label: '司机', value: 0 }
          ],
          default: '',
          ui: {
            widget: 'select'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      // { title: '', type: 'checkbox', className: 'text-center' },
      { title: '司机姓名', className: 'text-center', width: '170px', index: 'name' },
      { title: '手机号', className: 'text-center', width: '170px', index: 'mobile' },
      { title: '类型', className: 'text-center',  width: '170px',render: 'isCaptain' },
      { title: '月承运金额上限（元）', className: 'text-center', width: '200px', render: 'monthFreightAmount' },
      { title: '日提现金额上限（元）', className: 'text-center', width: '200px', render: 'dayWithdrawalAmount' },
      { title: '月提现金额上限（元）', className: 'text-center', width: '200px', render: 'monthWithdrawalAmount' },
      { title: '月收款金额上限（元）', className: 'text-center', width: '200px', render: 'monthReceivableAmount' },
      {
        title: '操作',
        width: '170px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '配置',
            click: item => this.settingAction(item),
            acl: { ability: ['USERCENTER-DRIVER-CONFIG-config'] }
          }
        ]
      }
    ];
  }
}
