import { CurrencyPipe } from '@angular/common';
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 { TicketService } from 'src/app/routes/ticket-management/services/ticket.service';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-receivable-order',
  templateUrl: './receivable-order.component.html',
  styleUrls: ['../../../commom/less/box.less', '../../../commom/less/expend-but.less'],
  providers: [CurrencyPipe]
})
export class ReceivableOrderComponent 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[] = [];

  info: any = {};
  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    private router: Router,
    private currencyPipe: CurrencyPipe
  ) {}

  ngOnInit(): void {
  }

  loadInfo() {
    this.service.request(this.service.$api_get_fico_sum).subscribe(res => {
      if (res) {
        this.info = res;
      }
    });
  }

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

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

  /**
   * 重置表单
   */
  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
          }
        },
        ahxcode: {
          type: 'string',
          title: '核销单号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        },
        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
            }
          }
        },
        arvattype: {
          type: 'string',
          title: '付款类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'pay:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount(),
            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: '核销状态',
          enum: [
            { value: 1, label: '已核销' },
            { value: 0, label: '待核销' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        banktype: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        ahxdate: {
          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
            }
          }
        },
        // orderS3: {
        //   type: 'string',
        //   title: '费用号',
        //   ui: {
        //     placeholder: '请输入',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        remarks: {
          type: 'string',
          title: '核销备注',
          ui: {
            autocomplete: 'off',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '核销单号', index: 'ahxcode', type: 'link', width: 210 },
      { title: '订单号', index: 'billHCode', type: 'link', width: 180 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '核销日期', index: 'ahxdate', type: 'date', width: 160 },
      { title: '收款账户', index: 'ltdaccountId', width: 200 },
      { title: '核销类型', index: 'ahxType', type: 'enum', enum: { '1': '预收款' }, width: 120 },
      {
        title: '核销金额',
        index: 'ahxmoney',
        width: 140,
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.ahxmoney)}`
      },
      {
        title: '应收金额',
        index: 'armoney',
        width: 140,
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.armoney)}`
      },
      { title: '银行类型', index: 'banktype', type: 'enum', enum: { '1': '平安', '2': '浦发' }, width: 120 },
      { title: '收款类型', index: 'arvattype', type: 'enum', enum: { '1': '费用款项' }, width: 120 },
      { title: '付款人', index: 'artoname', width: 200 },
      { title: '付款账户', index: 'shipperaccount', width: 170 },
      { title: '结算客户', index: 'cnoName', width: 120 },
      { title: '银行水单', index: 'bankreceipt', width: 200 },
      { title: '创建时间', index: 'createTime', width: 180 },
      // { title: '收款人', index: 'driver2IdName', width: 120 },
      // { title: '创建人', index: 'createUserIdLabel', width: 120 },
      { title: '核销状态', index: 'sts', type: 'enum', enum: { 0: '待核销', 1: '已核销' }, width: 120 },
      { title: '核销备注', index: 'remarks', width: 120 },
      {
        title: '操作',
        fixed: 'right',
        width: 120,
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            click: item =>
              this.router.navigate(['/financial-management/receivable-order/detail/' + item.id], { queryParams: { billHId: item.billHId } })
          }
          // {
          //   text: '核销'
          // }
        ]
      }
    ];
  }
}
