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
          }
        },
        orderSn2: {
          type: 'string',
          title: '充值单号',
          ui: {
            placeholder: '请输入'
          }
        },
        receiveName: {
          type: 'string',
          title: '充值状态',
          enum: [
            { label: '全部', value: '' },
            { label: '充值中', value: '充值中' },
            { label: '已完成', value: '已完成' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            change: (i: any) => {
              this.sf.value.receiveName = i;
              this.sf?.setValue('/receiveName', i);
            }
          },
          default: ''
        },
        createTime: {
          title: '充值时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        orderSn3: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
            autocomplete: 'off',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveName2: {
          type: 'string',
          title: '账户类型',
          enum: [
            { label: '全部', value: '' },
            { label: '货主账户', value: '货主账户' },
            { label: '司机账户', value: '司机账户' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            change: (i: any) => {
              this.sf.value.receiveName2 = i;
              this.sf?.setValue('/receiveName2', i);
            },
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        page2: {
          type: 'string',
          title: '网络货运人',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        orderSn23: {
          type: 'string',
          title: '银行类型',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '充值时间', index: 'updatedAt', type: 'date' },
      { title: '充值单号', index: 'callNo' },
      { title: '网络货运人', index: 'callNo' },
      { title: '银行类型', index: 'callNo' },
      { title: '账户类型', index: 'callNo' },
      { title: '账户名称', index: 'callNo' },
      { title: '虚拟账户', index: 'callNo' },
      { title: '充值金额', index: 'callNo' },
      { title: '充值银行账户', index: 'callNo',format: item=> `中国招商银行</br>6238293829388888` },
      { title: '充值方式', index: 'callNo' },
      { title: '充值状态', index: 'callNo' },
      { title: '银行流水号', index: 'callNo' },
      {
        title: '操作',
        buttons: [
          {
            text: '查看回单',
            // click: item => this.router.navigate(['/financial-management/freight-account/detail/1'])
          }
        ]
      }
    ];
  }
}
