import {Component} from '@angular/core';
import {UserService} from "../service-user/user.service";
import {Router} from "@angular/router";
import {AlertController} from "@ionic/angular";
import {HttpClient} from "@angular/common/http";


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


  constructor(
    private userServ: UserService,
    private router: Router,
    private alertCtrl: AlertController,
  ) {
  }

  userData: any = {
    account: "",
    password: ""
  }

  goBack() {
    // let path = localStorage.getItem("REDIRECT_URL") || "/home/mine"
    console.log(1)
    this.router.navigate(['/home/mine'], {fragment: 'setting'});
    // this.router.navigate([path])
  }

  passwordVisible = false;

  async login() {
    console.log(this.userData)
    try {
      let isLogin = await this.userServ.checkUserByAccount(this.userData.account, this.userData.password)
      console.log(isLogin)
      if (isLogin) {
        this.goBack();
      } else {
        await this.alertError(isLogin?.toString())
      }
    } catch (err) {
      let msg: any = err
      await this.alertError(msg)
    }
  }

  async alertError(err: string | undefined) {
    const alert = await this.alertCtrl.create({
      header: '登陆失败',
      subHeader: '请检查用户名密码',
      message: err,
      buttons: ['好的'],
    });

    await alert.present();
  }


}
