import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../services/ticket.service';

@Component({
  selector: 'app-etc-invoiced-requested',
  templateUrl: './etc-invoiced-requested.component.html',
  styleUrls: ['../../../commom/less/box.less', '../../../commom/less/expend-but.less']
})
export class ETCInvoicedRequestedComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  selectedRows: any[] = [];
  constructor(public service: TicketService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value
      });
    }
    this.selectedRows = [];
    return requestOptions;
  };

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

  auditAction() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择申请记录');
      return;
    }
    const modal = this.nzModalService.warning({
      nzTitle: '确定对已选运单批量申请开票？',
      nzOnOk: () => {
        this.service
          .request(this.service.$api_get_apply_invoice, { wayBillIds: this.selectedRows.map(item => item.id) }, 'POST', false)
          .subscribe(res => {
            if (res) {
              this.service.msgSrv.success('申请开票成功');
              this.st.load(1);
            }
            modal.destroy();
          });

        return false;
      }
    });
  }

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

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

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        wayBillCode: {
          type: 'string',
          title: '运单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        billType: {
          type: 'string',
          title: '订单类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'bill:type' },
            placeholder: '请选择'
          }
        },
        driverName: {
          type: 'string',
          title: '司机姓名',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        driverPhone: {
          type: 'string',
          title: '司机手机',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        licenseCarNo: {
          type: 'string',
          title: '车牌号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        licenseBelonging: {
          type: 'string',
          title: '车辆所有人',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        dischargePlace: {
          type: 'string',
          title: '卸货地',
          ui: {
            autocomplete: 'off',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        loadingPlace: {
          type: 'string',
          title: '装货地',
          ui: {
            autocomplete: 'off',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        shipperId: {
          type: 'string',
          title: '托运人',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => this.service.getEnterpriceList({ enterpriseName: q }),
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        enterpriseInfoName: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              expand: (value: boolean) => value
            },
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: '60px' },
      { title: '运单号', index: 'wayBillCode', width: '170px' },
      { title: '订单号', index: 'billCode', width: '170px' },
      { title: '订单类型', index: 'billTypeLabel', width: '140px' },
      { title: '装货地', index: 'loadingPlace', width: '220px' },
      { title: '卸货地', index: 'dischargePlace', width: '220px' },
      { title: '司机信息', render: 'call1No', width: '140px' },
      { title: '车辆信息', render: 'call1N2o', width: '200px' },
      { title: '车牌颜色', index: 'licenseCarNOColorLabel', width: '140px' },
      { title: '车辆是否已备案', index: 'putOnRecord', width: '150px', type: 'enum', enum: { false: '否', true: '是' } },
      { title: '托运人', index: 'shipperAppUserName', width: '140px' },
      { title: '网络货运人', index: 'enterpriseInfoName', width: '220px' },
      { title: '接单时间', index: 'orderReceivingTime', type: 'date', width: '150px' },
      { title: '装货时间', index: 'loadingTime', type: 'date', width: '150px' },
      { title: '卸货时间', index: 'unloadingTime', type: 'date', width: '150px' },
      { title: '签收时间', index: 'submissionTime', type: 'date', width: '150px' }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_asyncExportEtcApplyList);
  }
}
