import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import * as Parse from 'parse';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { AiChatService } from '../../../services/aichart.service';
import {
  ionicStandaloneModules,
  ToastController,
} from '../../ionic-standalone.modules';
@Component({
  selector: 'app-withdrawal',
  templateUrl: './withdrawal.component.html',
  styleUrls: ['./withdrawal.component.scss'],
  standalone: true,
  imports: [...ionicStandaloneModules, NavComponent, CommonModule, FormsModule],
})
export class WithdrawalComponent implements OnInit {
  constructor(
    private toastController: ToastController,
    private aiServ: AiChatService
  ) {}
  price: number = 0;
  balance: number = 0;
  bankinfo: {
    bankname: string;
    name: string;
    mobile: string;
    bankcard: string;
  } = {
    bankname: '',
    name: '',
    mobile: '',
    bankcard: '',
  };
  data: any = [
    { value: 0, name: '银行卡' },
    { value: 1, name: '微信' },
  ];
  value: Number = 0;
  account?: Parse.Object;
  ngOnInit() {
    this.getAccount();
  }
  async getAccount() {
    let id = Parse.User.current()?.id;
    let query = new Parse.Query('Account');
    query.equalTo('user', id);
    query.equalTo('company', this.aiServ.company);
    this.account = await query.first();
    if (this.account?.get('bank')) {
      this.bankinfo = this.account?.get('bank')[0];
    }
    console.log(this.account);
    let anthorWallet = await this.aiServ.getAuthorWallet(id!);
    this.balance = anthorWallet.balance;
  }
  async withdrawal() {
    if (
      !this.bankinfo.bankname ||
      !this.bankinfo.bankcard ||
      !this.bankinfo.mobile
    ) {
      this.toast('请先绑定银行卡', 1500);
      return;
    }
    if (!this.price || this.price < 0) {
      this.toast('请输入正确金额', 1500);
      return;
    }
    if (this.balance < this.price) {
      this.toast('提现金额不得超过余额！', 1500);
      return;
    }
    if (this.price < 100) {
      this.toast('最低提现金额为100', 1500);
      return;
    }
    let UserAgentWithdraw = Parse.Object.extend('UserAgentWithdraw');
    let userAgentWithdraw = new UserAgentWithdraw();
    userAgentWithdraw.set('count', Number(this.data.price));
    userAgentWithdraw.set('type', 'bank');
    userAgentWithdraw.set('name', this.bankinfo.name);
    userAgentWithdraw.set('count', this.price);
    userAgentWithdraw.set('status', '100');
    userAgentWithdraw.set('company', {
      __type: 'Pointer',
      className: 'Company',
      objectId: this.aiServ.company,
    });
    userAgentWithdraw.set('user', {
      __type: 'Pointer',
      className: '_User',
      objectId: Parse.User.current()?.id,
    });
    userAgentWithdraw.set('bank', this.bankinfo);
    await userAgentWithdraw.save();
    this.toast('申请成功', 1500);
    history.back();
  }

  async toast(title: string, time: number, color?: string) {
    const toast = await this.toastController.create({
      message: title,
      color: color ?? 'warning',
      duration: time || 1500,
    });
    toast.present();
  }
}
