import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BankCardManagementService } from '../../../services/bank-card-management.service';
import { CwcBankCardManagementAddComponent } from '../add/add.component';
import { CwcBankCardManagementBindComponent } from '../bind/bind.component';

@Component({
  selector: 'app-cwc-bank-card-management-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.less']
})
export class CwcBankCardManagementIndexComponent implements OnInit {
  list: any = [];
  ltdId = '';
  ltdName = '';
  constructor(public modal: NzModalService, public service: BankCardManagementService, public router: Router, public ar: ActivatedRoute) { }

  ngOnInit() {
    this.ltdId = this.ar.snapshot.queryParams?.ltdId;
    this.ltdName = this.ar.snapshot.queryParams?.ltdName;
    this.getBankList(this.ltdId);
  }

  getBankList(roleId = '') {
    this.service.request(this.service.$api_bank_card_list, { roleId, accountType: '3' }).subscribe((res) => {
      if (res) {
        this.list = res;
      }
    });
  }

  add() {
    const modalRef = this.modal.create({
      nzTitle: '添加银行卡',
      nzContent: CwcBankCardManagementBindComponent,
      nzWidth: '40%',
      nzFooter: null,
      nzComponentParams: {
        i: {
          ltdId: this.ltdId,
          ltdName: this?.ltdName
        }
      }
    });
    modalRef.afterOpen.subscribe(() => { });
    modalRef.afterClose.subscribe((result) => {
      if (result === true) {
        this.getBankList(this.ltdId);
      }
    });
  }

  edit(record: any) {
    const modalRef = this.modal.create({
      nzTitle: '编辑',
      nzWidth: '700',
      nzContent: CwcBankCardManagementAddComponent,
      nzComponentParams: {
        record,
      },
      nzFooter: null,
      nzMaskClosable: false,
    });
    modalRef.afterOpen.subscribe(() => { });
    modalRef.afterClose.subscribe((result) => {
      if (result) {
        this.getBankList();
      }
    });
  }


  // 删除
  del(record: any) {
    this.modal.confirm({
      nzTitle: '<b>确认删除该银行账户吗？</b>',
      nzContent: `<p>银行卡号: ${record.bankCardNumber}</p>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_bank_card_del, { id: record.id, ltdId: this.ltdId }).subscribe((res) => {
          if (res === true) {
            this.service.msgSrv.success('数据删除成功！');
            this.getBankList();
          }
        }),
    });
  }

  formatBankCard(value: any) {
    return value.replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
  }
}
