import { Component, OnInit, ViewChild } from '@angular/core';
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-recharge-record',
  templateUrl: './recharge-record.component.html',
  styleUrls: ['../../../commom/less/box.less', '../../../commom/less/expend-but.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();

  @ViewChild('remarkodal', { static: true })
  remarkodal!: any;
  rechargeRemark = '';

  _$expand = false;
  constructor(public service: FreightAccountService, private modal: NzModalService) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf.value });
      if (this.sf.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf.value.createTime?.[0] || '',
            end: this.sf.value.createTime?.[1] || ''
          }
        });
      }
    }
    return requestOptions;
  };

  addRemark(item: any) {
    this.rechargeRemark = item.remark;
    const modal = this.modal.create({
      nzTitle: this.rechargeRemark ? '修改备注' : '添加备注',
      nzContent: this.remarkodal,
      nzFooter: [
        {
          type: 'primary',
          label: '确认',
          loading: () => this.service.http.loading,
          onClick: () => {
            this.service.request(this.service.$api_edit_remark, { id: item.id, remark: this.rechargeRemark }).subscribe(res => {
              if (res) {
                this.service.msgSrv.success('修改成功');
                this.st.load(1);
                modal.destroy();
              }
            });
            return false;
          }
        }
      ]
    });
  }

  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.exportStart({ ...this.sf.value, pageSize: -1 }, this.service.$api_get_exportPageByOperator);
  }

  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: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        roleName: {
          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: 140 },
      { title: '网络货运人', index: 'ltdName', width: 160 },
      { title: '银行类型', index: 'bankTypeLabel', width: 100 },
      { title: '账户类型', index: 'accountTypeLabel', width: 100 },
      { title: '账户名称', index: 'roleName', width: 160 },
      { title: '虚拟账户', index: 'virtualAccount', width: 100 },
      {
        title: '充值金额',
        index: 'rechargeAmount',
        width: 160,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.rechargeAmount }) }
      },
      { title: '充值银行账户', render: 'transferBankAccount', width: 200 },
      { title: '充值方式', index: 'payChannelLabel', width: 100 },
      { title: '充值状态', index: 'rechargeStatusLabel', width: 100 },
      { title: '银行流水号', index: 'paySerialNumber', width: 120 },
      {
        title: '操作',
        width: 120,
        fixed: 'right',
        className: 'text-center',
        buttons: [
          { type: 'divider' },
          {
            text: '查看回单<br>',
            click: item =>
              this.service.getReceiptUrl(item.receiptUrl, {
                bankType: item.bankType,
                rmYll: item.roleId,
                snglFlgCd: item.paySerialNumber2,
                bussType: '05',
                ltdId: item.ltdId,
                accountType: item.accountType
              })
          },
          {
            text: '添加备注',
            click: item => this.addRemark(item)
          }
        ]
      }
    ];
  }
}
