import { Component, OnInit } from '@angular/core';
import { IonicModule } from '@ionic/angular';
import { NavComponent } from '../../../app/components/nav/nav.component';
import * as Parse from 'parse';
import { AuthService } from '../../../services/auth.service';
import { DatePipe } from '@angular/common';

@Component({
  selector: 'app-vip',
  templateUrl: './vip.component.html',
  styleUrls: ['./vip.component.scss'],
  standalone: true,
  imports: [IonicModule, NavComponent],
  providers: [DatePipe],
})
export class VipComponent implements OnInit {
  user: any = Parse.User.current();
  vips: Array<Parse.Object> = [];
  profile?: Parse.Object;
  account?: Parse.Object;
  myVip: any;
  currentGoods?: Parse.Object; //当前选择的会员
  tips: Array<string> = [
    ' ：会员承诺书本人自愿参加“心上人”线上高端单身俱乐部活动。遵守言责自负、文责自负的原则，另本人自愿做出如下承诺：一、严格遵守国家法律法规，遵守社会公德，承诺本人无违法犯罪行为。若有隐瞒，后果自负。二、恪守诚信，保证我所提供的报名内容真实、准确、无欺诈成份。否则，愿意接受法律的制裁。',
  ];
  constructor(private authSer: AuthService, private datePipe: DatePipe) {}

  ngOnInit() {
    this.refresh()
  }
  refresh() {
    this.getGoods();
    this.getUserVip();
  }
  //获取当前VIP等级
  async getUserVip() {
    let vip = new Parse.Query('UserVip');
    vip.equalTo('user', Parse.User.current()?.id);
    vip.equalTo('company', this.authSer.company);
    let reqVip = await vip.first();
    if (reqVip && reqVip.id) {
      let myVip = reqVip.toJSON();
      let now = new Date().getTime();
      let lastDate = new Date(myVip?.['expiredAt'].iso).getTime();
      myVip['expiredAt'] = this.datePipe.transform(
        myVip?.['expiredAt'].iso,
        'yyyy-MM-dd'
      );
      if (lastDate >= now) {
        myVip['validity'] = true;
      }
      this.myVip;
    }
  }
  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');
    let res = await goods.find();
    this.vips = res;
    this.currentGoods = this.vips[0];
  }
  //获取用户账户信息
  async getAccount() {
    let queryAccount = new Parse.Query('Account');
    queryAccount.equalTo('user', Parse.User.current()?.id);
    let res = await queryAccount.first();
    this.account = res;
  }
  onchang(data: Parse.Object) {
    this.currentGoods = data;
  }
}
