import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STComponent, STColumn, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { FreightAccountService } from '../../../services/freight-account.service';

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

  _$expand = false;

  id = null;
  headerInfo: any = {};
  constructor(public service: FreightAccountService, private route: ActivatedRoute) {
    this.id = route.snapshot.params.id;
    this.loadHeadInfo();
  }

  ngOnInit(): void {}

  loadHeadInfo() {
    this.service.request(this.service.$api_get_payment_header, { id: this.id }).subscribe(res => {
      if (res) {
        this.headerInfo = res;
      }
    });
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value,
        payHId: this.id,
        billTime: {
          start: this.sf.value.billTime?.[0] || null,
          end: this.sf.value.billTime?.[1] || null
        },
        feedate: {
          start: this.sf.value.feedate?.[0] || null,
          end: this.sf.value.feedate?.[1] || null
        }
      });
    } else {
      Object.assign(requestOptions.body, {
        payHId: this.id,
      });
    }
    return requestOptions;
  };

  goBack() {
    history.go(-1);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        // feeHCode: {
        //   type: 'string',
        //   title: '费用单号',
        //   ui: {
        //     placeholder: '请输入'
        //   }
        // },
        billHCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        waybillHCode: {
          type: 'string',
          title: '运单号',
          ui: {
            placeholder: '请输入'
          }
        },
        // feedate: {
        //   title: '费用日期',
        //   type: 'string',
        //   ui: {
        //     widget: 'sl-from-to-search',
        //     format: 'yyyy-MM-dd',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   } as SFDateWidgetSchema
        // },
        billTime: {
          title: '订单日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
          } as SFDateWidgetSchema
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount(),
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '序号', render: 'billHCode', width: 80 },
      // { title: '费用号', index: 'feeHCode', width: 100 },
      // { title: '费用日期', index: 'feedate', type: 'date', width: 150 },
      { title: '订单号', index: 'billHCode', width: 100 },
      { title: '订单日期', index: 'billTime', width: 150 },
      { title: '结算客户', index: 'cnoName', width: 90 },
      { title: '费用科目', index: 'feeSubId', width: 100 },
      {
        title: '收票金额',
        index: 'paymoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.paymoney }) }
      },
      {
        title: '收票税额',
        index: 'invtax',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.invtax }) }
      }
    ];
  }
}
