import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, 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-transaction-flow',
  templateUrl: './transaction-flow.component.html',
  styleUrls: ['./transaction-flow.component.less']
})
export class TransactionFlowComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

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

  /**
   * 重置表单
   */
  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: '请选择'
          } 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: '保费退款', value: 6 },
            { label: '余额充值', value: 7 },
            { label: '余额提现', value: 8 },
            { label: '资金分配', value: 9 },
            { label: '资金回收', value: 10 }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        incomeType: {
          type: 'string',
          title: '收支类型',
          enum: [
            { label: '全部', value: '部' },
            { label: '收入', value: 1 },
            { label: '支出', value: 2 }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        channelSource: {
          type: 'string',
          title: '账户类型',
          enum: [
            { label: '全部', value: '' },
            { label: '货主端', value: '1' },
            { label: '司机端', value: '2' },
            { label: '司机端', value: '3' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        orderS2n2221: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        orderSn221f: {
          type: 'string',
          title: '所属项目',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              expand: (value: boolean) => value
            },
            asyncData: () => this.service.getEnterpriseProject()
          },
          default: ''
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              expand: (value: boolean) => value
            },
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '交易时间', index: 'createTime', width: 180 },
      { title: '流水号', index: 'transactionNumber', width: 150 },
      { title: '交易类型', index: 'tradeTypeLabel', width: 120 },
      { title: '关联单号', index: 'businessNumber', width: 150 },
      { title: '账户类型', index: 'accountTypeLabel', width: 100 },
      { title: '账户名称', index: 'roleName', width: 100 },
      { title: '所属项目', index: 'projectName', width: 100 },
      { title: '收支类型', index: 'incomeTypeLabel', width: 100 },
      { title: '交易金额', index: 'amount', width: 100 },
      { title: '账户余额', index: 'accountBalance', width: 100 },
      { title: '网络货运人', index: 'ltdName', width: 120 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '银行流水号', index: 'channelPaySn', width: 120 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '查看回单'
            // click: item => this.refund(item)
          }
        ]
      }
    ];
  }
}
