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

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

  info: any = {};
  params: any = {};
  static: any = {};
  _$expand = false;
  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private route: ActivatedRoute) {
    this.params = route.snapshot.queryParams;
  }

  ngOnInit(): void {
    this.loadInfo();
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, {
      ltdId: this.params.ltdId,
      projectId: this.params.projectId,
      enterpriseId: this.params.enterpriseId,
      roleId: this.params.roleId
    });
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        }
      });
    }
    // this.loadStatistics(requestOptions.body);
    return requestOptions;
  };

  loadInfo() {
    this.service
      .request(this.service.$api_get_shipper_account_balance_detail, {
        ...this.sf?.value,
        ltdId: this.params.ltdId,
        projectId: this.params.projectId,
        enterpriseId: this.params.enterpriseId,
        roleId: this.params.roleId,
        pageIndex: this.st.pi,
        pageSize: this.st.ps,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        }
      })
      .subscribe(res => {
        if (res) {
          this.info = res;
        }
      });
  }

  loadStatistics(params: any) {
    this.service.request(this.service.$api_get_shipper_account_balance_detail, params).subscribe(res => {
      if (res) {
        this.static = res;
      }
    });
  }

  stChange(e: STChange): void {}

  exportList() {
    this.service.exportStart(
      {
        ...this.sf.value,
        pageSize: -1,
        ltdId: this.params.ltdId,
        projectId: this.params.projectId,
        enterpriseId: this.params.enterpriseId,
        roleId: this.params.roleId,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        }
      },
      this.service.$api_get_exportAccountBalanceShipperByOperatorPage
    );
  }

  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
          }
        },
        createTime: {
          title: '交易时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        transactionNumber: {
          type: 'string',
          title: '流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        businessNumber: {
          type: 'string',
          title: '交易单号',
          ui: {
            placeholder: '请输入'
          }
        },
        // tradeType: {
        //   type: 'string',
        //   title: '交易类型',
        //   enum: [
        //     { label: '全部', value: '' },
        //     { label: '整车订单退款', value: '1' },
        //     { label: '整车订单支付', value: '2' },
        //     { label: '提现失败退回', value: '3' },
        //     { label: '提现', value: '4' },
        //     { label: '充值', value: '5' },
        //     { label: '运货订单结算F', value: '5' }
        //   ],
        //   ui: {
        //     widget: 'select',
        //     placeholder: '请选择',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   },
        //   default: ''
        // },
        tradeType: {
          type: 'string',
          title: '交易类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'trade:type' },
            visibleIf: {
              expand: (value: boolean) => value
            },
          } as SFSelectWidgetSchema
        },
        incomeType: {
          type: 'string',
          title: '收支类型',
          enum: [
            { label: '全部', value: '' },
            { label: '收入', value: '2' },
            { label: '支出', value: '1' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        projectName: {
          type: 'string',
          title: '所属项目',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
        // projectId: {
        //   title: '所属项目',
        //   type: 'string',
        //   default: '',
        //   ui: {
        //     widget: 'select',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     },
        //     asyncData: () => this.service.getEnterpriseProject()
        //   } as SFSelectWidgetSchema
        // }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '交易时间', index: 'createTime', type: 'date', width: 170 },
      { title: '流水号', index: 'transactionNumber', width: 170 },
      { title: '交易类型', index: 'tradeTypeLabel', className: 'text-center', width: 140 },
      { title: '收支类型', index: 'tradeTypeLabel', className: 'text-center', width: 140 },
      { title: '交易单号', index: 'businessNumber', width: 170 },
      { title: '订单号', index: 'orderSn', width: 170 },
      { title: '运单号', index: 'transportSn', width: 170 },
      { title: '货主', index: 'enterpriseName', width: 170 },
      { title: '所属项目', index: 'projectName', width: 170 },
      { title: '收支类型', index: 'incomeTypeLabel', className: 'text-center', width: 140 },
      {
        title: '交易金额',
        index: 'amount',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.amount }) }
      },
      {
        title: '账户余额',
        index: 'accountBalance',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.accountBalance }) }
      },
      { title: '付款方', index: 'payName', width: 170 },
      { title: '收款方', index: 'incomeName', width: 170 },
      { title: '备注', index: 'tradeContent', width: 170 }
    ];
  }
}
