import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } 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';
import { ClearingModalComponent } from './clearing-modal/clearing-modal.component';

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

  _$expand = false;

  rechargeStatus = '1';
  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

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

  changePaymentStatus(status: string) {
    this.rechargeStatus = status;
    this.st.load(1);
  }

  refund(item: any) {
    this.nzModalService.warning({
      nzTitle: '确定要将该笔款项原路退回？',
      nzOnOk: () => {}
    });
  }

  clearingAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '清分',
      nzContent: ClearingModalComponent,
      nzOnOk: com => {
        return false;
      }
    });
  }

  /**
   * 重置表单
   */
  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
          }
        },
        paySerialNumber: {
          type: 'string',
          title: '银行流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: '' },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        transferBankAccount: {
          type: 'string',
          title: '付款账户',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        transferBankCardNumber: {
          type: 'string',
          title: '付款账号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        transferBankOpenName: {
          type: 'string',
          title: '付款银行',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        transferDate: {
          title: '转账时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true,
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '银行流水号', index: 'paySerialNumber', width: 180 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '资金总账号', index: 'virtualAccount', width: 180 },
      {
        title: '充值金额',
        index: 'rechargeAmount',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.rechargeAmount }) }
      },
      { title: '付款账户', index: 'transferBankAccount', width: 180 },
      { title: '付款账号', index: 'transferBankCardNumber', width: 180 },
      { title: '付款银行', index: 'transferBankOpenName', width: 220 },
      { title: '转账时间', index: 'createTime', type: 'date', width: 150 },
      { title: '转账备注', index: 'rechargeRemark', width: 180 },
      { title: '退款时间', index: 'refundTime', type: 'date', width: 150 },
      // { title: '操作人', index: 'rechargeName', width: 140 },
      // { title: '操作时间', index: 'callNo', type: 'date', width: 160 },
      { title: '状态', index: 'rechargeStatusLabel', width: 110 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '查看',
            click: item => this.router.navigate(['/financial-management/abnormal-gold/detail/' + item.id])
          }
        ]
      }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf.value, pageSize: -1 }, this.service.$api_get_exportAbnormalAmountPage);
  }
}
