import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';
import { cacheConf } from '@conf/cache.conf';
import { SFComponent, SFSchema, SFStringWidgetSchema, SFUISchema } from '@delon/form';
import { EACacheService } from '@shared';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { BankCardManagementService } from '../../../services/bank-card-management.service';

interface EmitType {
  success: boolean,
  current: number,
  value: object
};

@Component({
  selector: 'app-cwc-bank-card-management-bind',
  templateUrl: './bind.component.html',
})

export class CwcBankCardManagementBindComponent implements OnInit {
  schema: SFSchema = {};
  ui: SFUISchema = {};
  record: any;
  i: any;
  userInfo: any = {};
  bankBranchName = '';
  bankArea = '';
  bankName = '';
  loading = false;
  current = 0; // 当前节点

  @Output() toNextStep = new EventEmitter<EmitType>();

  @ViewChild('sf', { static: false }) sf!: SFComponent;
  enterpriseName = '';

  branchBanks: any[] = [];
  constructor(public service: BankCardManagementService, public modalService: NzModalService, public eaCacheSrv: EACacheService, public modalRef: NzModalRef) {
  }


  ngOnInit() {
    this.initSF();
  }

  initSF() {
    this.schema = {
      properties: {
        bankAccountName: {
          type: 'string',
          title: '企业名称',
          default: this.i?.ltdName,
          ui: {
            widget: 'text'
          }
        },
        bankCardNumber: {
          type: 'string',
          title: '银行卡号',
          maxLength: 30,
          ui: {
            showRequired: true,
            placeholder: '请输入银行卡号',
            autocomplete: 'off'
          } as SFStringWidgetSchema,
        },
        bankName: {
          type: 'string',
          title: '开户银行',
          // readOnly: true,
          ui: {
            showRequired: true,
            autocomplete: 'off',
            placeholder: '请输入开户银行',
          } as SFStringWidgetSchema,
        },
        ltdId: {
          type: 'string',
          title: '',
          default: this.i?.ltdId,
          ui: {
            widget: 'text',
            hidden: true
          }
        },

        // bankBranchName: {
        //   type: 'string',
        //   title: '开户支行',
        //   ui: {
        //     showRequired: true,
        //     placeholder: '请输入开户支行',
        //     autocomplete: 'off'
        //   },
        // },
        // bankBranchCode: {
        //   type: 'string',
        //   title: '联行号',
        //   ui: {
        //     placeholder: '请输入联行号'
        //   },
        // },
        mobile: {
          type: 'string',
          title: '手机号',
          maxLength: 11,
          format: 'mobile',
          ui: {
            placeholder: '请输入手机号'
          },
        },
      },
      required: ['bankCardNumber', 'mobile', 'bankName'],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 18 },
      },
    };
  }
  trackById(_: number, item: any): number {
    return item.id;
  }

  submit() {
    if (this.sf.valid) {
      this.service.request(this.service.$api_bank_card_add, { ...this.sf.value }).subscribe(res => {
        if (res) {
          this.modalRef.destroy(true);
        }
      })
    }
  }


  formatCard() {
    return /[1-9]\d{12,18}/;
  }

}
