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

@Component({
  selector: 'app-receivable-order-detail',
  templateUrl: './receivable-order-detail.component.html',
  styleUrls: ['./receivable-order-detail.component.less', '../../../../commom/less/expend-but.less', '../../../../commom/less/box.less'],
  providers: [CurrencyPipe]
})
export class ReceivableOrderDetailComponent 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;
  billHId = null;
  headerInfo: any = {};
  constructor(public service: FreightAccountService, private route: ActivatedRoute, private currencyPipe: CurrencyPipe) {
    this.id = route.snapshot.params.id;
    this.billHId = route.snapshot.queryParams.billHId;
    this.loadHeadInfo();
  }

  ngOnInit(): void {}

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

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

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

  /**
   * 重置表单
   */
  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
          }
        },
        feeHCode: {
          type: 'string',
          title: '费用单',
          ui: {
            placeholder: '请输入'
          }
        },
        billHCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount()
          },
          default: ''
        },
        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',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   } as SFDateWidgetSchema
        // }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '序号', render: 'no', width: 80 },
      { title: '费用号', index: 'feeLId', 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: 'billLTypeLabel', width: 100 },
      { title: '费用科目', index: 'feeSubName', width: 140 },
      { title: '结算客户', index: 'cnoName', width: 100 },
      {
        title: '已收金额',
        index: 'ahxmoney',
        width: 100,
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.ahxmoney)}`
      }
    ];
  }
}
