import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import * as Parse from 'parse';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
// import { Wechat } from '@ionic-native/wechat/ngx';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { PayCompComponent } from '../../../app/components/pay-comp/pay-comp.component';
import {
  ionicStandaloneModules,
  LoadingController,
  ToastController,
} from '../../ionic-standalone.modules';
@Component({
  selector: 'app-recharge',
  templateUrl: './recharge.component.html',
  styleUrls: ['./recharge.component.scss'],
  standalone: true,
  imports: [
    ...ionicStandaloneModules,
    NavComponent,
    FormsModule,
    PayCompComponent,
  ],
})
export class RechargeComponent implements OnInit {
  @Input('isComp') isComp: boolean = false; // 是否作为组件显示
  @Output() complete: EventEmitter<any> = new EventEmitter<any>(); //完成触发

  @ViewChild('paycomp') paycomp!: PayCompComponent;
  options: Array<number> = [30, 50, 100, 200, 500, 1000, 2000, 5000];
  price: any = 0;
  loading: any;
  company = localStorage.getItem('company');
  account?: Parse.Object;
  showPay: boolean = false;
  tradeNo: string = ''; //支付单号
  orderId?: string;
  constructor(
    private toastController: ToastController,
    public loadingController: LoadingController,
    private router: Router
  ) {}
  ngOnInit() {
    this.getAccount();
  }

  back() {
    history.back();
  }
  async getAccount() {
    let Account = new Parse.Query('Account');
    Account.include('user');
    Account.equalTo('user', Parse.User.current()?.id);
    this.account = await Account.first();
    console.log(this.account);
  }

  onChange(val: number) {
    console.log(val);
    this.price = val;
  }
  onChangeInput() {
    this.price = this.price < 1 ? 1 : Math.floor(this.price);
    console.log(this.price);
  }
  async onShowPay() {
    if (this.price <= 0) return;
    if (this.price <= 0 || isNaN(this.price)) {
      return this.presentToast(
        `充值错误，请重新确认充值金额！`,
        1500,
        'danger'
      );
    }
    if (this.price > 100000) {
      return this.presentToast('单笔充值最多不超过100000元', 1500, 'danger');
    }
    this.price = parseInt(this.price);
    this.showPay = true;
    this.paycomp.isOpen = true;
  }
  async presentToast(title: string, time: number, color: string) {
    const toast = await this.toastController.create({
      message: title,
      duration: time,
      color: color,
    });
    toast.present();
  }
  onComplete(evet: any) {
    console.log(evet);
    this.tradeNo = evet.tradeNo;
    if(evet?.code) this.complete.emit(true);
  }
}
