import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalController, ToastController } from '@ionic/angular';
import { NavComponent } from '../../../app/components/nav/nav.component';
import { FormsModule } from '@angular/forms';
import * as Parse from 'parse';
import { AuthService } from '../../../services/auth.service';
import { DatePipe, CommonModule } from '@angular/common';
import { AgreementComponent } from '../../login/agreement/agreement.component';
import { PayCompComponent } from '../../../app/components/pay-comp/pay-comp.component';
import { AccountService } from '../../../services/account.service';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
import { Router } from '@angular/router';
import { AvatarComponent } from '../../../app/components/avatar/avatar.component';

@Component({
  selector: 'app-vip',
  templateUrl: './vip.component.html',
  styleUrls: ['./vip.component.scss'],
  standalone: true,
  imports: [
    ...ionicStandaloneModules,
    NavComponent,
    CommonModule,
    FormsModule,
    PayCompComponent,
    AvatarComponent
  ],
  providers: [DatePipe],
})
export class VipComponent implements OnInit {
  @ViewChild('paycomp') paycomp!: PayCompComponent;

  user: any = Parse.User.current();
  profile?: Parse.Object;
  goodsList: Array<Parse.Object> = [];
  myVip: any;
  registerAgreement: any;
  isCheck: boolean = false;
  currentGoods?: Parse.Object; //当前选择的会员
  showPay: boolean = false;
  price: number = 0;
  tradeNo: string = ''; //支付单号
  orderId?: string;
  loading: boolean = true;
  constructor(
    private modalController: ModalController,
    private toastController: ToastController,
    private authSer: AuthService,
    private accServ: AccountService,
    private router: Router,
    ) {}

  ngOnInit() {
    this.refresh();
  }
  async refresh() {
    await this.getGoods();
    await this.getUserVip();
    this.getAgreement();
    this.loading = false;
  }
  //获取当前VIP等级
  async getUserVip() {
    this.myVip = await this.accServ.getVip()
  }
  async getGoods() {
    let goods = new Parse.Query('ShopGoods');
    goods.equalTo('company', this.authSer.company);
    goods.equalTo('type', 'vip');
    goods.equalTo('status', true);
    goods.ascending('top');
    goods.notEqualTo('isDeleted', true);
    goods.include('services');
    this.goodsList = await goods.find();
    this.currentGoods = this.goodsList[0];
    this.price = this.currentGoods?.get('price');
  }
  getAgreement() {
    let Agreement = new Parse.Query('ContractAgreement');
    Agreement.equalTo('company', this.authSer.company);
    Agreement.equalTo('type', 'service');
    Agreement.first().then((res) => {
      console.log(res);
      this.registerAgreement = res;
    });
  }
  async showAgreement() {
    const modal = await this.modalController.create({
      component: AgreementComponent,
      cssClass: 'my-custom-class',
      componentProps: {
        agreement: this.registerAgreement,
      },
    });
    return await modal.present();
  }
  onchang(data: Parse.Object) {
    this.currentGoods = data;
    this.price = this.currentGoods?.get('price');
  }
  onComplete(evet: any) {
    console.log(evet);
    // this.showPay = false;
    this.tradeNo = evet.tradeNo;
    this.getUserVip()
  }
  async openpay() {
    this.showPay = true;
    this.paycomp.isOpen = true;
  }
  toUrl(url: string){
    this.router.navigate([url]);
  }
}
