<!-- eslint-disable no-unused-vars -->
<!--
 * @Author: Kane
 * @Date: 2022-12-14 15:23:54
 * @LastEditors: Kane
 * @LastEditTime: 2023-01-18 22:24:21
 * @FilePath: \admin_system\src\views\account\Login.vue
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
-->
<template>
  <div id="login">
    <div class="form-warp">
      <ul class="menu-tab">
        <li :class="{ 'current': current_menu === item.type }" @click="onToggleMenu(item.type)" v-for="item in tab_menu"
          :key="item.type">{{ item.label }}
        </li>
      </ul>
      <!-- <el-form ref="form" :model="form"> -->
      <el-form ref="form">
        <el-form-item>
          <label class="form-label">用户名</label>
          <el-input type="text" v-model.lazy.trim="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item>
          <label class="form-label">密码</label>
          <el-input type="password" v-model.lazy.trim="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item v-show="current_menu === tab_menu[1].type">
          <label class="form-label">确认密码</label>
          <el-input type="password" disabled v-model.lazy.trim="loginForm.confirm_password"></el-input>
        </el-form-item>
        <el-form-item>
          <label class="form-label">验证码</label>
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input type="text" disabled></el-input>
            </el-col>
            <el-col :span="10">
              <el-button type="danger" disabled class="el-button-block" @click="getValidateCode()">获取验证码</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="el-button-block" @click="login" :disabled="submit_btn_disable"
            :loading="submit_btn_loading">
            {{ current_menu === "login" ? "登录" : "注册" }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Login } from "@/utils/api/info/account";
import { ElMessage } from "element-plus";
import router from "../../router/index";

export default {
  name: "loginPage",
  data()
  {
    return {
      loginForm: {
        username: "",
        password: "",
        confirm_password: "",
        validateCode: "",
      },
      tab_menu: [
        { type: "login", label: "登录" },
        { type: "regiester", label: "注册" },
      ],
      current_menu: "",
      staffInfo: null,
      submit_btn_disable: false,
      submit_btn_loading: false,
    };
  },
  methods: {
    onToggleMenu(type)
    {
      this.current_menu = type;
      console.log(process.env.VUE_APP_API_URL_LOGIN);
    },
    getValidateCode()
    {
      ElMessage({
        message: "测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字",
        center: true,
        type: "error",
      });
    },
    /**
     * 登录
     */
    login()
    {
      if (this.loginForm.username.length === 0 || this.loginForm.password === 0)
      {
        ElMessage({
          message: "请填写您的P13账号和密码！",
          type: "error",
        });

        return;
      }

      this.submit_btn_disable = true;
      this.submit_btn_loading = true;

      const userInfo = {
        p13account: this.loginForm.username,
        password: this.loginForm.password,
      };

      Login(userInfo)
        .then((response) =>
        {
          //成功获取到返回值时的响应函数，要判断返回值的成功标志
          //验证成功，将获取到的token和用户信息保存到vuex和localStoreage
          //然后router.push，进行路由跳转到控制台
          const data = response.data;

          //判断是否成功，显示提示信息
          if (data.success === true)
          {
            ElMessage({
              message: data.message,
              type: "success",
              center: true,
            });

            this.staffInfo = data.staffInfo;

            //保存用户信息和token
            this.saveUserInfo(data);

            //验证成功，跳转路由
            router.push("/home");
          }
          else
          {
            //验证失败
            ElMessage({
              message: data.message,
              type: "error",
              center: true,
            });

            this.submit_btn_disable = false;
            this.submit_btn_loading = false;
          }
        })
        .catch((error) =>
        {
          //没有获取到响应数据
          console.log(error);

          ElMessage({
            message: error.message,
            type: "error",
            center: true,
          });

          this.submit_btn_disable = false;
          this.submit_btn_loading = false;
        });
    },
    saveUserInfo(userInfo) //将获取到的用户信息和token保存到vuex和localStorage
    {
      //保存到vuex
      this.$store.commit("app/SET_USERINFO", userInfo);

      //保存到localStorage
      const token = userInfo.token;
      const userInfoJson = JSON.stringify(userInfo);

      window.localStorage.setItem("token", token);
      window.localStorage.setItem("user_info", userInfoJson);
    }
  },
  created()
  {
    //初始化菜单选项
    this.current_menu = this.tab_menu[0].type;
  },
  mounted()
  {
    //清理状态
    this.$store.state.app.userInfo = null;
  },
};
</script>

<style scoped>
#login {
  height: 100vh;
  background-color: #344a5f;
  padding-top: 50px;
}

.form-warp {
  width: 320px;
  padding: 30px;
  margin: auto;
  background-color: #ffffff10;
  border-radius: 5px;
}

/*.menu-tab {
  text-align: center;
  margin-bottom: 15px;

  li {
    display: inline-block;
    padding: 10px 24px;
    margin: 0 10px;
    color: #fff;
    font-size: 16px;
    border-radius: 5px;
    cursor: pointer;

    &.current {
      background-color: rgba(0, 0, 0, 0.1);
    }
  }
}*/
.menu-tab {
  text-align: center;
  margin-bottom: 15px;
}

.menu-tab li {
  display: inline-block;
  padding: 10px 24px;
  margin: 0 10px;
  color: #fff;
  font-size: 14px;
  border-radius: 5px;
  cursor: pointer;
}

.menu-tab .current {
  background-color: rgba(0, 0, 0, 0.1);
}

.form-label {
  display: block;
  color: #fff;
  font-size: 14px;
}

.el-button-block {
  width: 100%;
}
</style>