<template>
  <div class="login-container">
    <el-form class="login-form" ref="loginFromRef" :model="loginForm" :rules="loginRules" @submit.native.prevent>
      <div class="title-container">
        <h3 class="title">用户登录</h3>
      </div>
      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon="user" />
        </span>
        <el-input placeholder="请输入账号" name="userName" type="text" v-model="loginForm.userName" />
      </el-form-item>

      <el-form-item prop="password1">
        <span class="svg-container">
          <svg-icon icon="password" />
        </span>
        <el-input placeholder="请输入密码" name="password" :type="passwordType" v-model="loginForm.password"/>
        <span class="show-pwd">
          <svg-icon @click="onChangePwdType" :icon="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>

      <el-form-item v-if="isShowKaptCha" prop="kaptcha">
        <span class="svg-container"><svg-icon icon="kaptcha" /></span>
        <el-input @keydown.enter="handleLogin()" v-model="loginForm.kaptcha" placeholder="请输入验证码" name="kaptcha" type="text"/>
        <span @click="getKaptchaImg">
          <el-image class="show-kaptcha" :src="kaptchaUrl" fit="cover">
            <template #error>
              <div class="image-slot"><svg-icon icon="errorImg" /></div>
            </template>
          </el-image>
        </span>
      </el-form-item>

      <!---登录按钮-->
      <el-form-item style="height: 49px" v-if="!loginDialog">
        <el-button @click="handleLogin" type="primary" style="width: 520px; height: 49px" :loading="loading" native-type="submit">登录</el-button>
      </el-form-item>

      <el-form-item class="choosedept-wrap" v-if="loginDialog">
        <el-select v-model="deptId" @change="refreshToken" placeholder="请选择部门">
          <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login",
};
</script>
<script setup>
import { setItem } from "@/utils/storage";
import { ElNotification } from "element-plus";
import * as MOSTY from "@/components/MyComponents/index";
import { getKaptcha } from "@/api/sys";
import { onMounted, ref, onUnmounted } from "vue";
import { validatePassword } from "./rules";
import { useStore } from "vuex";

import { useRouter, onBeforeRouteLeave } from "vue-router";
const store = useStore();
const kaptchaUrl = ref("");
// 数据源
const loginForm = ref({
  userName: "",
  password: "",
  kaptcha: ""
});
const loginDialog = ref(false);
const deptList = ref([]);
const deptId = ref("");
const authorization = ref("");
const isShowKaptCha = ref(false);

// 验证规则
const loginRules = ref({
  userName: [{ required: true, trigger: "blur",  message: "用户名为必填项" }],
  password: [{ required: true, trigger: "blur", validator: validatePassword() }],
  kaptcha: [{ required: true, trigger: "blur",  message: "验证码为必填项" }]
});

const handleClose = () => {};
const refreshToken = (e) => {
  store.dispatch("user/refreshToken", { deptId: e, jwtToken: authorization.value}).then((res) => {
    loading.value = false;
    store.commit("user/setDeptId", e);
    // window.location.href = '/'// 登录后操作
    router.push("/");
  }).catch(() => {
    loading.value = false;
  });
};
// 处理密码框文本显示状态
const passwordType = ref("password");
const onChangePwdType = () => {
  if (passwordType.value === "password") {
    passwordType.value = "text";
  } else {
    passwordType.value = "password";
  }
};

// 登录动作处理
const loading = ref(false);
const loginFromRef = ref(null);
const router = useRouter();
const handleLogin = () => {
  loginFromRef.value.validate((valid) => {
    if (!valid) return false;
    loading.value = true;
    store.dispatch("user/login", loginForm.value).then((res) => {
      loading.value = false;
      // 登录后操作
      if (res.deptList.length === 1) {
        window.location.href = '/'
      } else {
        deptList.value = [...res.deptList];
        loginDialog.value = true;
        authorization.value = res.jwtToken;
        ElNotification({title: "提示",message: "请选择部门",duration: 3000});
      }
    }).catch(() => {
      loading.value = false;
    });
  });
};
const logout = () => {
  store.dispatch("user/logout");
};
onMounted(() => {
 
});
const getKaptchaImg = () => {
  const res =`${process.env.VUE_APP_GATEWAY_BASE_URL}/mosty-api/mosty-base/kaptcha?date=` + new Date();
  kaptchaUrl.value = res;
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    &::v-deep .el-input__inner {
      -webkit-box-shadow: 0 0 0 1000px #283443 inset;
      -webkit-text-fill-color: #ffffff !important;
    }

    &::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    &::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
  }

  .tips {
    font-size: 16px;
    line-height: 28px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    ::v-deep .lang-select {
      position: absolute;
      top: 4px;
      right: 0;
      background-color: white;
      font-size: 22px;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .show-kaptcha {
    width: 80px;
    height: 40px;
    overflow: hidden;
    position: absolute;
    right: 6px;
    top: 3px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
  }

  .choosedept-wrap {
    .el-form-item__content {
      width: 100%;

      .el-select {
        width: 100%;
      }
    }

    ::v-deep .el-input {
      width: 100%;
    }
  }

  // .el-dialog{
  //   background: #293444;
  //   color: white;
  //   .el-dialog__title{
  //     color: #fff;
  //   }
  //     .el-select{
  //       width: 300px;
  //       padding-bottom: 40px;
  //   }
  // }
}
</style>
