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

  _$expand = false;

  selectedRows: any[] = [];
  totalCallNo = 0;
  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;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        this.totalCallNo = this.selectedRows.reduce((total, cv) => total + cv.callNo, 0);
        break;
    }
  }

  approval(): void {}

  add(): void {}

  auditAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '审核',
      nzContent: this.auditModal,
      nzFooter: [
        {
          label: '拒绝',
          type: 'default',
          onClick: () => {
            modal.destroy();
          }
        },
        {
          label: '通过',
          type: 'primary',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
    modal.afterClose.subscribe(res => {
      this.st.load();
    });
  }

  showReason(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '查看原因',
      nzContent: '运单数据异常，暂时无法开票，请联系客服400-xxxx-xxxx',
      nzFooter: [
        {
          label: '关闭',
          type: 'primary',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
  }

  /**
   * 重置表单
   */
  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: '全部' },
            { label: '待审核', value: '待审核' },
            { label: '处理中', value: '处理中' },
            { label: '已完成', value: '已完成' },
            { label: '已拒绝', value: '已拒绝' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        createTime: {
          title: '提现时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        orderSn2: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveName2: {
          type: 'string',
          title: '账户类型',
          enum: [
            { label: '全部', value: '全部' },
            { label: '货主账户', value: '货主账户' },
            { label: '司机账户', value: '司机账户' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveName3: {
          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: 'key', type: 'checkbox' },
      { title: '提现时间', index: 'no' },
      { title: '提现单号', index: 'callNo' },
      { title: '网络货运人', index: 'callNo' },
      { title: '银行类型', index: 'callNo' },
      { title: '账户类型', index: 'callNo' },
      { title: '账户名称', index: 'callNo' },
      { title: '虚拟账户', index: 'callNo' },
      { title: '提现金额', index: 'callNo' },
      { title: '提现银行账户', index: 'callNo' },
      { title: '提现状态', index: 'callNo' },
      { title: '银行流水号', index: 'callNo' },
      { title: '失败原因', index: 'callNo' },
      {
        title: '操作',
        buttons: [
          {
            text: '审核',
            click: item => this.auditAction(item)
          },
          {
            text: '查看回单'
            // click: item => this.auditAction(item)
          },
          {
            text: '详情',
            click: item => this.router.navigate(['/financial-management/withdrawals-record/detail/1'])
          }
        ]
      }
    ];
  }
}
