import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions } 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-driver-account-detail',
  templateUrl: './driver-account-detail.component.html',
  styleUrls: ['../../../../commom/less/box.less', '../../../../commom/less/expend-but.less']
})
export class DriverAccountDetailComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  info: any = {};
  params: any = {};
  constructor(public service: FreightAccountService, 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,
      bankType: this.params.bankType
    });
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value,
        createTime: {
          start: this.sf.value?.createTime?.[0] || '',
          end: this.sf.value?.createTime?.[1] || ''
        }
      });
    }
    return requestOptions;
  };

  loadInfo() {
    this.service
      .request(this.service.$api_get_driver_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,
        bankType: this.params.bankType,
        createTime: {
          start: this.sf?.value?.createTime?.[0] || '',
          end: this.sf?.value?.createTime?.[1] || ''
        }
      })
      .subscribe(res => {
        if (res) {
          this.info = 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,
        bankType: this.params.bankType,
        createTime: {
          start: this.sf.value?.createTime?.[0] || '',
          end: this.sf.value?.createTime?.[1] || ''
        }
      },
      this.service.$api_get_exportAccountBalanceDriverByOperatorPage
    );
  }

  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
        },
        channelPaySn: {
          type: 'string',
          title: '流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        businessNumber: {
          type: 'string',
          title: '交易单号',
          ui: {
            placeholder: '请输入'
          }
        },
        tradeType: {
          type: 'string',
          title: '交易类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'trade:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        incomeType: {
          type: 'string',
          title: '收支类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'income:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '交易时间', index: 'createTime', type: 'date', width: 150 },
      { title: '流水号', index: 'channelPaySn', width: 170 },
      { title: '交易类型', index: 'tradeTypeLabel', className: 'text-center', width: 150 },
      { title: '交易单号', index: 'businessNumber', width: 190 },
      { title: '订单号', index: 'orderSn', width: 190 },
      { title: '运单号', index: 'transportSn', width: 190 },
      { title: '收支类型', index: 'incomeTypeLabel', className: 'text-center', width: 150 },
      {
        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: 150 },
      { title: '收款方', index: 'incomeName', width: 150 },
      { title: '备注', index: 'tradeContent', width: 150 }
    ];
  }
}
