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

import * as Parse from 'parse';
import { HttpClient } from '@angular/common/http';
import { ToastController } from '@ionic/angular';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
@Component({
  selector: 'app-bankcard',
  templateUrl: './bankcard.component.html',
  styleUrls: ['./bankcard.component.scss'],
	standalone: true,
  imports: [...ionicStandaloneModules,NavComponent],
})
export class BankcardComponent implements OnInit {
  constructor(
    private http: HttpClient,
    public toastController: ToastController
  ) {}
  company: string = localStorage.getItem('company') || '';
  account: any = null;
  bankinfo: any = {
    bankcard: '',
    name: '',
    idcard: '',
    mobile: '',
  };
  adding: boolean = false;
  isModalOpen: boolean = false;
  setOpen(isOpen: boolean) {
    this.isModalOpen = isOpen;
  }
  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);
    }
  }

  footer = [
    {
      text: '取消',
      onPress: () => {
        console.log('ok');
        this.onClose();
      },
    },
    {
      text: '确定',
      onPress: () => {
        console.log('ok');
        if (this.adding) {
          return;
        }
        this.adding = true;
        this.confirm();
      },
    },
  ];

  onClose() {
    this.isModalOpen = false;
  }

  async confirm() {
    let cardInfo = this.bankinfo;
    for (const key in cardInfo) {
      if (!cardInfo[key]) {
        const toast = await this.toastController.create({
          message: '请将信息填写完整',
          color: 'warning',
          duration: 100000,
        });
        toast.present();
        this.adding = false;
        return;
      }
    }
    this.verifyCard().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: 100000,
          });
          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: 100000,
        });
        toast.present();
        this.adding = false;
        return;
      } else {
        const toast = await this.toastController.create({
          message: '服务错误请稍后重试',
          color: 'warning',
          duration: 100000,
        });
        toast.present();
        this.adding = false;
        return;
      }
    });
  }
  async verifyCard() {
    return new Promise((resolve, reject) => {
      this.http
        .post('https://test.fmode.cn/api/apig/bankcard', {
          company: this.company,
          bankcard: this.bankinfo.bankcard,
          name: this.bankinfo.name,
          idcard: this.bankinfo.idcard,
          mobile: this.bankinfo.mobile,
        })
        .subscribe((res: any) => {
          console.log(res);
          resolve(res);
        });
    });
  }

  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();
    }
  }

  addCard() {
    this.isModalOpen = true;
  }
}
