import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { AddCollectionInvoiceModalComponent } from 'src/app/routes/ticket-management/components/input-invoice/add-collection-invoice-modal/add-collection-invoice-modal.component';
import { FreightAccountService } from '../../services/freight-account.service';

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

  _$expand = false;

  selectedRows: 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
      });
      if (this.sf.value.brmdate) {
        Object.assign(requestOptions.body, {
          brmdate: {
            start: this.sf.value.brmdate?.[0] || '',
            end: this.sf.value.brmdate?.[1] || ''
          }
        });
      }
      if (this.sf.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf.value.createTime?.[0] || '',
            end: this.sf.value.createTime?.[1] || ''
          }
        });
      }
    }
    return requestOptions;
  };

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

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择收款单');
      return;
    }
    // const modal = this.nzModalService.create({
    //   nzTitle: '收款单',
    //   nzContent: AddCollectionInvoiceModalComponent,
    //   nzComponentParams: { i: { userId: 0 } },
    //   nzFooter: null
    // });
  }

  /**
   * 重置表单
   */
  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
          }
        },
        brmcode: {
          type: 'string',
          title: '收款单号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: null
        },
        bankreceipt: {
          type: 'string',
          title: '银行水单',
          ui: {
            placeholder: '请输入'
          }
        },
        brmtype: {
          type: 'string',
          title: '收款类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'driverrecord:receive:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        brmmode: {
          type: 'string',
          title: '收款方式',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'receive:mode' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        arto: {
          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
            }
          }
        },
        sts: {
          type: 'string',
          title: '收款状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'write:off:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        brmdate: {
          title: '到账日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        // billHCode: {
        //   type: 'string',
        //   title: '订单号',
        //   ui: {
        //     placeholder: '请输入',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        // feeHCodes: {
        //   type: 'string',
        //   title: '费用号',
        //   ui: {
        //     placeholder: '请输入',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        remarks: {
          type: 'string',
          title: '收款备注',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, fixed: 'left', className: 'text-center' },
      { title: '收款单号', index: 'brmcode', type: 'link', width: 180, className: 'text-left' },
      // { title: '订单号', index: 'billHCode',  width: 180, className: 'text-left' },
      // { title: '费用号', index: 'feeCode',  width: 180, className: 'text-left' },
      { title: '网络货运人', index: 'ltdName', width: 200, className: 'text-left' },
      { title: '到账日期', index: 'brmdate', type: 'date', width: 200, className: 'text-left' },
      { title: '收款账户', index: 'ltdaccountId', width: 200, className: 'text-left' },
      {
        title: '到账金额',
        index: 'brmmoney',
        width: 200,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.brmmoney }) }
      },
      {
        title: '核销金额',
        index: 'brmmoney',
        width: 200,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.brmmoney }) }
      },
      {
        title: '预收金额',
        index: 'yskmoney',
        width: 200,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yskmoney }) }
      },
      { title: '银行类型', index: 'banktypeLabel', width: 200, className: 'text-left' },
      { title: '收款类型', index: 'brmtypeLabel', width: 150, className: 'text-left' },
      { title: '收款方式', index: 'brmModeLabel', width: 150, className: 'text-left' },
      { title: '付款人', index: 'artoname', width: 200, className: 'text-left' },
      { title: '银行水单', index: 'bankreceipt', width: 200, className: 'text-left' },
      { title: '创建时间', index: 'createTime', type: 'date', width: 200, className: 'text-left' },
      // { title: '创建人', index: 'createUserName', width: 150, className: 'text-left' },
      { title: '收款状态', index: 'stsLabel', width: 200, className: 'text-left' },
      { title: '收款备注', index: 'remarks', width: 200, className: 'text-left' },
      {
        title: '操作',
        width: '110px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            click: item => this.router.navigate(['/financial-management/receipt-order/detail/' + item.id])
          }
          // {
          //   text: '核销'
          // }
        ]
      }
    ];
  }
}
