import { Component } from '@angular/core';
import { AlertController, NavController } from '@ionic/angular';
import Parse from 'parse';
Parse.initialize("dev");
Parse.serverURL = 'http://web2023.fmode.cn:9999/parse';

@Component({
  selector: 'app-page-login',
  templateUrl: './page-login.component.html',
  styleUrls: ['./page-login.component.scss']
})
export class PageLoginComponent {

  userName: string = "";
  password: string = "";

  constructor(
    private navCtrl: NavController,
    private alertController: AlertController
  ) { }

  async login() {
    let user1;
    try {

      // user1 = await Parse.User.logIn(this.userName,this.password)
      const query = new Parse.Query('LcmUser');
      query.equalTo('username', this.userName);
      query.equalTo('password', this.password);
      user1 = await query.first();

      // 登录成功后保存用户信息到本地存储
    if (user1) {
      localStorage.setItem('currentUser', JSON.stringify(user1.toJSON()));
    }

    } catch (error: any) {
      let message: string = "";
      // 新增提示词详情，根据查询错误信息设置不同的中文提示信息
      if (error?.message.indexOf("is required") > -1) {
        message = "必须输入账号或邮箱";
      }
      if (error?.message.indexOf("Invalid username") > -1) {
        message = "账号或密码错误，请检查";
      }
      this.presentAlert({
        header: "登录失败",
        subHeader: "状态码:" + error.code,
        message: message || error.message
      });
    }

    console.log(user1);
    if (user1?.id) {
      this.navCtrl.back();

    }
  }

  // async login() {
  //   let user;
  //   try {
  //       const query = new Parse.Query('LcmUser');
  //       query.equalTo('username', this.userName);
  //       query.equalTo('password', this.password);
  //       user = await query.first();
  //   } catch (error: any) {
  //       let message: string = "";
  //       if (error?.message.indexOf("is required") > -1) {
  //           message = "必须输入账号或邮箱";
  //       }
  //       if (error?.message.indexOf("Invalid username") > -1) {
  //           message = "账号或密码错误，请检查";
  //       }
  //       this.presentAlert({
  //           header: "登录失败",
  //           subHeader: "状态码:" + error.code,
  //           message: message || error.message
  //       });
  //   }

  //   console.log(user);
  //   if (user?.id) {
  //       this.navCtrl.back();
  //   }
  // }

  async register() {
    let LcmUser = Parse.Object.extend("LcmUser");
    let user = new LcmUser();
    user.set("username", this.userName);
    user.set("password", this.password);

    try {
      let result = await user.save();
      console.log(result);
      if (result.id) {
        this.navCtrl.back();
      }
    } catch (error: any) {
      let message: string = "注册失败，请重试";
      this.presentAlert({
        header: "注册失败",
        subHeader: "错误信息",
        message: message
      });
    }
  }

  async presentAlert(options: { header: string, subHeader: string, message: string }) {
    const alert = await this.alertController.create({
      header: options?.header,
      subHeader: options?.subHeader,
      message: options?.message,
      buttons: ['好的'],
    });

    await alert.present();
  }

  back() {
    this.navCtrl.back();
  }

}
