import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { FreightAccountService } from '../../services/freight-account.service';
import { ClearingModalComponent } from '../abnormal-gold/clearing-modal/clearing-modal.component';

@Component({
  selector: 'app-payment-record',
  templateUrl: './payment-record.component.html',
  styleUrls: ['./payment-record.component.less']
})
export class PaymentRecordComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  paymentStatus: any = '';

  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value,
        applyDate: {
          start: this.sf.value.applyDate?.[0] || '',
          end: this.sf.value.applyDate?.[1] || ''
        },
        handlerDate: {
          start: this.sf.value.handlerDate?.[0] || '',
          end: this.sf.value.handlerDate?.[1] || ''
        },
        paymentStatus: this.paymentStatus || null
      });
    }
    return requestOptions;
  };

  changePaymentStatus(status?: string) {
    this.paymentStatus = status || null;
    this.st.load(1);
  }

  refund(item: any) {
    this.nzModalService.warning({
      nzTitle: '确定要关闭所选支付记录？',
      nzOnOk: () => {}
    });
  }

  /**
   * 重置表单
   */
  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
          }
        },
        orderPaymentCode: {
          type: 'string',
          title: '支付编号',
          ui: {
            placeholder: '请输入'
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        resourceCode: {
          type: 'string',
          title: '货源编号',
          ui: {
            placeholder: '请输入'
          }
        },
        serviceType: {
          type: 'string',
          title: '服务类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'service:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        driver: {
          type: 'string',
          title: '承运司机',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        driverLicensePlate: {
          type: 'string',
          title: '车牌号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        captain: {
          type: 'string',
          title: '收款人',
          ui: {
            placeholder: '请输入收款人姓名/手机号',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        isCaptain: {
          type: 'string',
          title: '车队长收款',
          enum: [
            {label: '全部', value: ''},
            {label: '是', value: '1'},
            {label: '否', value: '2'}
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        payType: {
          type: 'string',
          title: '支付类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'paybill:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        applyDate: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true,
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        handlerDate: {
          title: '处理时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true,
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder(),
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '支付编号', render: 'orderPaymentCode', width: 200 },
      {
        title: '支付金额',
        render: 'payAmount',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.payAmount }) }
      },
      {
        title: '运费明细',
        render: 'amountDetails',
        width: 160
        // format: item => {
        //   (item.amountDetails as Array<any>).forEach(detail => {
        //     detail.surcharge = detail.price + (detail.surcharge || 0);
        //   });
        //   console.log(item.amountDetails);

        //   return '';
        // }
      },
      {
        title: '支付类型',
        index: 'payTypeLabel',
        width: 140
      },
      { title: '货主', index: 'enterpriseInfoName', width: 180 },
      { title: '订单号', render: 'billCode', width: 170 },
      { title: '运单号', render: 'wayBillCode', width: 170 },
      { title: '货源编号', index: 'resourceCode', width: 160 },
      {
        title: '服务类型',
        index: 'serviceTypeLabel',
        width: 150
      },
      { title: '承运司机', render: 'driverId', width: 140 },
      { title: '收款人', render: 'captainName', width: 140 },
      { title: '银行类型', index: 'bankType', width: 150, type: 'enum', enum: { 1: '平安银行', 2: '浦发银行' } },
      { title: '网络货运人', index: 'ltdName', width: 160 },
      { title: '申请时间', index: 'applyTime', width: 160 },
      { title: '申请人', index: 'applyUserName', width: 90 },
      { title: '处理时间', index: 'handlerTime', width: 180 },
      { title: '处理人', index: 'handlerUserName', width: 90 },
      { title: '退款状态', index: 'refundStatusLabel', width: 220 },
      { title: '备注', index: 'failCause', width: 250 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '关闭',
            iif: item => item.paymentStatus === '4',
            click: item => this.refund(item)
          }
        ]
      }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf.value, pageSize: -1 }, this.service.$api_get_asyncExportBillPaymentApplicationList);
  }
}
