<!-- eslint-disable no-unused-vars -->
<!--
 * @Author: Kane
 * @Date: 2022-12-14 15:23:54
 * @LastEditors: Kane
 * @LastEditTime: 2023-02-03 15:46:55
 * @FilePath: \IT工具综合平台\src\views\account\Login32.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': ui.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="ui.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">
            {{ ui.current_menu === "login" ? "登录" : "注册" }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, onBeforeMount, onMounted } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { Login } from "@/utils/api/info/account";
import { ElMessage } from "element-plus";
//import router from "../../router/index";

export default {
  name: "loginPage",
  setup()
  {
    const store = useStore();
    const router = useRouter();

    const loginForm = reactive({
      username: "",
      password: "",
      confirm_password: "",
      validateCode: "",
    });

    const tab_menu = reactive(
      [
        { type: "login", label: "登录" },
        { type: "regiester", label: "注册" },
      ]);

    const ui = reactive(
      {
        current_menu: "",
        staffInfo: null,
        submit_btn_disable: false,
        submit_btn_loading: false,
      });

    const onToggleMenu = (type) =>
    {
      ui.current_menu = type;
      console.log(process.env.VUE_APP_API_URL_LOGIN);
    };

    const getValidateCode = () =>
    {
      ElMessage({
        message: "测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字测试文字",
        center: true,
        type: "error",
      });
    };
    /**
     * 登录
     */
    const login = () =>
    {
      if (loginForm.username.length === 0 || loginForm.password === 0)
      {
        ElMessage({
          message: "请填写您的P13账号和密码！",
          type: "error",
        });

        return;
      }

      ui.submit_btn_disable = true;
      ui.submit_btn_loading = true;

      const userInfo = {
        p13account: loginForm.username,
        password: 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,
            });

            ui.staffInfo = data.staffInfo;

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

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

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

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

          ui.submit_btn_disable = false;
          ui.submit_btn_loading = false;
        });
    };

    //将获取到的用户信息和token保存到vuex和localStorage
    const saveUserInfo = (userInfo) =>
    {
      //保存到vuex
      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);
    };

    onBeforeMount(() =>
    {
      //初始化菜单选项
      ui.current_menu = tab_menu[0].type;
    });

    onMounted(() =>
    {
      //清理状态
      store.state.app.userInfo = null;
    });

    return {
      //数据
      ui,
      loginForm,
      tab_menu,
      //方法
      onToggleMenu,
      saveUserInfo,
      login,
      getValidateCode,
    };
  },
};
</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>