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
      });
    }
    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('申请开票成功');
              modal.destroy();
              this.st.load(1);
            }
          });

        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: 'refund:apply:status' },
            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: '托运人',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        ltdId: {
          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' },
      { title: '运单号', index: 'wayBillCode' },
      { title: '订单号', index: 'billCode' },
      { title: '订单类型', index: 'billType' },
      { title: '装货地', index: 'loadingPlace' },
      { title: '卸货地', index: 'dischargePlace' },
      { title: '司机信息', render: 'call1No' },
      { title: '车辆信息', render: 'call1N2o' },
      { title: '托运人', index: 'shipperAppUserName' },
      { title: '网络货运人', index: 'enterpriseInfoName' },
      { title: '接单时间', index: 'orderReceivingTime', type: 'date' },
      { title: '装货时间', index: 'shipperAppUserName', type: 'date' },
      { title: '卸货时间', index: 'unloadingTime', type: 'date' },
      { title: '签收时间', index: 'submissionTime', type: 'date' }
    ];
  }
}
