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: ['./abnormal-gold.component.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;

  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 });
    }
    return requestOptions;
  };

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

  clearingAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '清分',
      nzContent: ClearingModalComponent,
      nzOnOk: com => {
        console.log(com.sf.value);

        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
          }
        },
        orderSn: {
          type: 'string',
          title: '银行流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        receiveName: {
          type: 'string',
          title: '网络货运人',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        orderSn22: {
          type: 'string',
          title: '银行类型',
          ui: {
            placeholder: '请输入'
          }
        },
        orderSn2: {
          type: 'string',
          title: '付款账户',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        orderSn221: {
          type: 'string',
          title: '付款账号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        orderS2n221: {
          type: 'string',
          title: '付款银行',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        createTime: {
          title: '转账时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '银行流水号', index: 'no', width: 150 },
      { title: '网络货运人', index: 'callNo', width: 120 },
      { title: '银行类型', index: 'callNo', width: 100 },
      { title: '资金总账号', index: 'callNo', width: 120 },
      { title: '充值金额', index: 'callNo', width: 100 },
      { title: '付款账户', index: 'callNo', width: 100 },
      { title: '付款账号', index: 'callNo', width: 100 },
      { title: '付款银行', index: 'callNo', width: 100 },
      { title: '转账时间', index: 'callNo', type: 'date', width: 150 },
      { title: '转账备注', index: 'callNo', width: 100 },
      { title: '操作人', index: 'callNo', width: 90 },
      { title: '操作时间', index: 'callNo', type: 'date', width: 150 },
      { title: '状态', index: 'callNo', width: 90 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '清分',
            click: item => this.clearingAction(item)
          },
          {
            text: '退款',
            click: item => this.refund(item)
          },
          {
            text: '查看',
            click: item => this.router.navigate(['/financial-management/withdrawals-record/detail/1'])
          }
        ]
      }
    ];
  }
}
