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

@Component({
  selector: 'app-recharge-record',
  templateUrl: './recharge-record.component.html',
  styleUrls: ['./recharge-record.component.less']
})
export class RechargeRecordComponent 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) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf.value });
    }
    return requestOptions;
  };

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

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

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

  exportList() {
    this.service.downloadFile(this.service.$mock_url, { ...this.sf.value, pageIndex: this.st.pi, pageSize: this.st.ps });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        rechargeNo: {
          type: 'string',
          title: '充值单号',
          ui: {
            placeholder: '请输入'
          }
        },
        rechargeStatus: {
          type: 'string',
          title: '充值状态',
          enum: [
            { label: '全部', value: '' },
            { label: '充值中', value: '1' },
            { label: '充值失败', value: '2' },
            { label: '充值成功', value: '3' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        createTime: {
          title: '充值时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        rechargeName: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
            autocomplete: 'off',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        accountType: {
          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: ''
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            },
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '充值时间', index: 'createTime', type: 'date', width: 180 },
      { title: '充值单号', index: 'rechargeNo', width: 120 },
      { title: '网络货运人', index: 'ltdName', width: 120 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '账户类型', index: 'accountTypeLabel', width: 100 },
      { title: '账户名称', index: 'rechargeName', width: 100 },
      { title: '虚拟账户', index: 'virtualAccount', width: 100 },
      { title: '充值金额', index: 'rechargeAmount', width: 100 },
      { title: '充值银行账户', render: 'transferBankAccount', width: 140 },
      { title: '充值方式', index: 'payChannelLabel', width: 100 },
      { title: '充值状态', index: 'rechargeStatusLabel', width: 100 },
      { title: '银行流水号', index: 'paySerialNumber', width: 120 },
      {
        title: '操作',
        width: 100,
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '查看回单'
            // click: item => this.router.navigate(['/financial-management/freight-account/detail/1'])
          }
        ]
      }
    ];
  }
}
