<!--
 * @Author: your name
 * @Date: 2021-01-11 11:14:26
 * @LastEditTime: 2022-04-28 18:35:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\views\Login.vue
-->
<template>
  <div class="backdrop-layer">
    <div class="head">
      <div class="logo"></div>
      <div class="title">
        三门坡镇农业AI监测平台
      </div>
    </div>
    <div class="ve_container">
      <el-card style="height: 300px" :body-style="{ background: '#ffffff' }">
        <h1>用户登录</h1>
        <transition name="el-fade-in-linear">
          <el-form
              :model="form"
              :rules="rules"
              v-show="!success"
              class="ve_form"
              ref="ref_form"
              :inline="false"
              @keyup.enter="onSubmit"
          >
            <el-form-item prop="userName">
              <el-input
                  size="large"
                  v-model.trim="userName"
                  placeholder="用户名"
              >
                <template #prepend>
                  <el-icon :size="20"><Iphone /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="pwd">
              <el-input
                  size="large"
                  type="password"
                  v-model.trim="pwd"
                  placeholder="密码"
              >
                <template #prepend>
                  <el-icon :size="20"><Key /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <div style="width: 50%;float: left;text-align: left;"><el-checkbox >记住密码</el-checkbox></div>
              <div style="width: 50%;float: left;">
                <el-button
                    class="ve_submit"
                    type="primary"
                    @click="onSubmit"
                >
                  登录
                </el-button>
              </div>
            </el-form-item>
          </el-form>
        </transition>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import {SET_TOKEN, SET_UNAME, SET_USER_INFO} from "@/store/modules/app/type";
import Common from "@/components/Common.vue";
import { ref, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";

// const rules = {
//   phone: [{ required: true, message: "请输入用户名", trigger: "blur" }]
// };

const rules = {
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }]
  ,
  pwd: [{ required: true, message: "请输入验证码", trigger: "blur" }
  ],
};

const store = useStore();
const router = useRouter();
// 定义两个账号
const guestAccount = { "pwd": "游客", "userName": "游客" };
const regularAccount = { "pwd": "qz123456789", "userName": "13797066447" };

const form = reactive({
  "pwd": "",
  "userName": ""
});
const { userName, pwd } = toRefs(form);
const ref_form = ref(null);
const success = ref(false);
sessionStorage.clear();
store.commit(`app/${SET_TOKEN}`, "");
router.options.isAddDynamicMenuRoutes = false;

// 根据时间设置账号
const setAccountByTime = () => {
  const now = new Date();
  const currentHour = now.getHours();

  if (currentHour >= 18 && currentHour < 24) {
    form.pwd = guestAccount.pwd;
    form.userName = guestAccount.userName;
  } else {
    form.pwd = regularAccount.pwd;
    form.userName = regularAccount.userName;
  }
};

// 初始化账号
setAccountByTime();

const onSubmit = () => {
  ref_form.value.validate(async (valid) => {
    if (valid) {
      const res = await VE_API.system.login(form);
      if (res.code == 0) {
        const { token, userName, djiCloudToken } = res.data;
        store.commit(`app/${SET_TOKEN}`, token);
        store.commit(`app/djiCloudToken`, djiCloudToken);
        store.commit(`app/${SET_UNAME}`, userName);
        res.data["token"] = undefined
        res.data["x-auth-token"] = undefined
        res.data["pwd"] = undefined
        store.commit(`app/${SET_USER_INFO}`, JSON.stringify(res.data));
        success.value = true;
        router.push({ name: "warningHome" });
      }
    } else {
      return;
    }
  });
};
</script>

<style lang="scss" scoped>
.ve_container {
  position: absolute;
  z-index: 1;
  width: 400px;
  top: calc(50%);
  left: calc(50% - 250px);
  transform: translateY(-50%);
  transition: all 1s;
  min-height: 273px;
  text-align: center;
  h1 {
    font-size: 24px;
    transition: all 1s;
    font-weight: normal;
    text-align: left;
    margin-bottom: 36px;
  }
  .ve_form {
    .ve_submit {
      width: 100%;
      height: 45px;
      background: #0BC678;
    }
  }
}
.backdrop-layer{
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  background: url(@/assets/login_bg.jpg) no-repeat;
  background-size:100% 100%;
  z-index: 1;
  overflow: hidden;
}
.head{
  position: absolute;
  left: 300px;
  top: 100px;
  display: flex;
  align-items: center;
  padding: 50px;
  .title{
    font-size: 30px;
    font-weight: bold;
    color:#006600;
    float: left;
    line-height: 50px;
    margin-left: 20px;
  }
  .logo{
    width: calc(167px * 0.5);
    height: calc(245px * 0.5);
    background: url(@/assets/img/logo-verticle.png) no-repeat;
    background-size:100% 100%;
    float: left;
    z-index: 1000;
  }
}
</style>
