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 { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-payable-order',
  templateUrl: './payable-order.component.html',
  styleUrls: ['../../../commom/less/box.less', '../../../commom/less/expend-but.less'],
  providers: [CurrencyPipe]
})
export class PayableOrderComponent 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_ph_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?.phxdate) {
        Object.assign(requestOptions.body, {
          phxdate: {
            start: this.sf.value.phxdate?.[0] || '',
            end: this.sf.value.phxdate?.[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
          }
        },
        phxcode: {
          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: '付款类型',
          enum: [{ value: '1', label: '费用款项' }],
          ui: {
            widget: 'select',
            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
            }
          }
        },
        driver2IdName: {
          type: 'string',
          title: '收款人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            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
            }
          }
        },
        phxdate: {
          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
            }
          }
        },
        feeHCode: {
          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: 'phxcode', type: 'link', width: 140 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '核销日期', index: 'phxdate', type: 'date', width: 160 },
      { title: '付款账户', index: 'shipperaccount', width: 170 },
      { title: '收款账户', index: 'ltdaccountId', width: 190 },
      {
        title: '核销金额',
        index: 'phxmoney',
        width: 140,
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.phxmoney || 0)}`
      },
      {
        title: '应付金额',
        index: 'prmoney',
        width: 140,
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.prmoney || 0)}`
      },
      { title: '银行类型', index: 'banktype', type: 'enum', enum: { '1': '平安', '2': '浦发' }, width: 120 },
      { title: '付款类型', index: 'brmtype', type: 'enum', enum: { '1': '费用款项' }, width: 120 },
      { title: '收款人', index: 'driver2IdName', width: 120 },
      { title: '结算客户', index: 'cnoName', width: 180 },
      { title: '银行水单', index: 'bankreceipt', width: 190 },
      { title: '创建时间', index: 'createTime', width: 180 },
      // { title: '创建人', index: 'createUserIdLabel', width: 120 },
      { title: '核销状态', index: 'sts', type: 'enum', enum: { 0: '待核销', 1: '已核销' }, width: 120 },
      { title: '核销备注', index: 'remarks', width: 120 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '浏览',
            click: item =>
              this.router.navigate(['/financial-management/payable-order/detail/' + item.id], { queryParams: { billHId: item.id } })
          }
        ]
      }
    ];
  }
}
