import { Component, OnInit } from '@angular/core';
import { NavComponent } from '../../../app/components/nav/nav.component';

import * as Parse from 'parse';
import { ToastController } from '@ionic/angular';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpService } from '../../../services/http.service';
@Component({
  selector: 'app-bankcard',
  templateUrl: './bankcard.component.html',
  styleUrls: ['./bankcard.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent, CommonModule, FormsModule],
})
export class BankcardComponent implements OnInit {
  constructor(
    private http: HttpService,
    private toastController: ToastController
  ) {}
  company: string = localStorage.getItem('company') || '';
  account: any = null;
  bankinfo: any = {
    bankcard: '',
    name: '',
    idcard: '',
    mobile: '',
  };
  adding: boolean = false;
  isModalOpen: boolean = false;
  async ngOnInit() {
    await this.getAccountInfo();
  }
  async getAccountInfo() {
    let user = Parse.User.current();
    let Account = new Parse.Query('Account');
    Account.equalTo('user', user?.id);
    Account.equalTo('company', this.company);
    let account = await Account.first();
    if (account && account.id) {
      this.account = account.toJSON();
      console.log(this.account);
    }
  }

  async confirm() {
    let cardInfo = this.bankinfo;
    for (const key in cardInfo) {
      if (!cardInfo[key]) {
        const toast = await this.toastController.create({
          message: '请将信息填写完整',
          color: 'warning',
          duration: 1500,
        });
        toast.present();
        this.adding = false;
        return;
      }
    }
    this.http
      .verifyCard({
        company: this.company,
        bankcard: this.bankinfo.bankcard,
        name: this.bankinfo.name,
        idcard: this.bankinfo.idcard,
        mobile: this.bankinfo.mobile,
      })
      .then(async (res: any) => {
        if (res.data.code == 200) {
          if (res.data.data.result == 0 || res.data.data.result == 1) {
            this.bankinfo.bankname = res.data.data.bank_info.bank;
            this.bankinfo.logo = res.data.data.bank_info.logo;
            if (this.account.bank && this.account.bank.length > 0) {
              this.account.bank.push(this.bankinfo);
            } else {
              this.account.bank = [this.bankinfo];
            }
            this.updateAccount();
            this.isModalOpen = false;
            setTimeout(() => {
              this.adding = false;
            }, 800);
          } else {
            const toast = await this.toastController.create({
              message: '卡状态异常,请更换卡',
              color: 'warning',
              duration: 1500,
            });
            toast.present();
            this.adding = false;
            return;
          }
        } else if (res.data.code == 400) {
          const toast = await this.toastController.create({
            message: res.data.msg,
            color: 'warning',
            duration: 1500,
          });
          toast.present();
          this.adding = false;
          return;
        } else {
          const toast = await this.toastController.create({
            message: '服务错误请稍后重试',
            color: 'warning',
            duration: 1500,
          });
          toast.present();
          this.adding = false;
          return;
        }
      });
  }

  async updateAccount() {
    let Account = new Parse.Query('Account');
    let account = await Account.get(this.account.objectId);
    if (account) {
      account.set('bank', this.account.bank);
      await account.save();
    }
  }
  changeCityCancel(type: string) {
    if (type === 'confirm') {
      this.confirm();
    }
    this.isModalOpen = false;
  }
  addCard() {
    this.isModalOpen = true;
  }
}
