<template>
  <div style="width: 100vw" class="login">
    <div style="position: absolute"></div>

    <div style="position: relative; top: -37vh; left: 25vw">
      <img
        style="
          width: 100%;
          height: 100%;
          min-width: 900px;
          display: flex;
          background-size: cover;
          justify-content: center;
          align-items: center;
        "
        src="../assets/images/title.png"
        alt=""
      />
    </div>
    <div style="margin-right: 20vw; margin-top: 12vh">
      <el-form
        ref="loginRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        style="
          width: 500px;
          height: 45vh;
          min-width: 450px;
          max-height: 410px;
          min-height: 410px;
        "
      >
        <h3 class="title">登 录</h3>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            size="large"
            style="font-size: 16px"
            auto-complete="off"
            placeholder="账号"
          >
            <!-- <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template> -->
            <template #prefix>
              <img
                style="width: 19px; height: 20px"
                src="../assets/images/user.png"
                alt=""
              />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            size="large"
            style="font-size: 16px"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <img
                style="width: 17px; height: 20px"
                src="../assets/images/lock.png"
                alt=""
              />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            size="large"
            auto-complete="off"
            placeholder="验证码"
            style="width: 67%; font-size: 16px"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <img
                style="width: 17px; height: 20px"
                src="../assets/images/lock.png"
                alt=""
              />
            </template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <!-- <el-checkbox
          v-model="loginForm.rememberMe"
          style="
            margin: 0px 0px 25px 0px;
            font-size: 23px;
            color: #6098fe;
            font-weight: 500;
          "
          >记住密码</el-checkbox
        > -->

        <el-check-tag
          checked
          class="ml-2"
          color="red"
          type="danger"
          @click="openAppDown"
          >协同办公系统使用说明</el-check-tag
        >
        <div style="width: 200px; margin-top: 15px; padding-left: 1.5vw">
          <el-button
            :loading="loading"
            size="large"
            class="login-btn"
            @click.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'"
              >立即注册</router-link
            >
          </div>
        </div>
      </el-form>
    </div>
    <el-dialog
      style=""
      v-model="appdialog"
      title="协同办公系统使用说明"
      width="52%"
    >
      <div style="padding: 15px 35px; height: 720px; overflow: auto">
        <el-text style="font-size: 20px">
          <div
            style="
              color: black;
              line-height: 45px;
              height: 45px;
              font-weight: 700;
            "
          >
            一、Windows操作系统
          </div>
          <div style="font-size: 18px; color: black; line-height: 30px">
            <!-- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -->
            Windows操作系统插件下载：
            <el-link
              style="font-size: 18px; color: #0068b6"
              type="primary"
              @click="openOfficeDown"
              ><el-icon><Link /></el-icon> 下载</el-link
            >
          </div>
        </el-text>
        <el-text class="mx-1" size="large" style="font-size: 20px">
          <div
            style="
              color: black;
              line-height: 45px;
              height: 45px;
              font-weight: 700;
            "
          >
            二、国产化操作系统
          </div>
          <div style="font-size: 18px; color: black; line-height: 30px">
            <!-- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -->
            国产化操作系统插件下载：
            <!-- <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -->
            <el-link
              type="primary"
              style="font-size: 18px; color: #0068b6"
              @click="openQiLinOfficeDown"
            >
              <el-icon><Link /></el-icon>下载</el-link
            >
          </div>
        </el-text>
        <el-text class="mx-1" size="large" style="font-size: 20px">
          <div
            style="
              color: black;
              line-height: 45px;
              height: 45px;
              font-weight: 700;
            "
          >
            三、安卓手机App
          </div>
          <div style="font-size: 18px; color: black; line-height: 30px">
            请用手机自带浏览器扫描二维码进行下载
          </div>
          <div style="margin-top: 10px">
            <el-image
              style="height: 200px; width: 200px"
              :src="app_url"
              :fit="fit"
            />
          </div>
        </el-text>
        <el-text class="mx-1" size="large" style="font-size: 20px">
          <div
            style="
              color: black;
              line-height: 45px;
              height: 45px;
              font-weight: 700;
            "
          >
            四、操作手册
          </div>
          <div style="font-size: 18px; color: black; line-height: 30px">
            协同办公系统用户操作手册下载：<el-link
              type="primary"
              style="font-size: 18px; color: #0068b6"
              @click="openHandle"
            >
              <el-icon><Link /></el-icon>下载</el-link
            >
          </div>
          <div style="text-align: center; margin-top: 10px"></div>
        </el-text>
      </div>
    </el-dialog>
    <!--  底部  -->
    <div class="el-login-footer">
      <!-- <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span> -->
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";
// import url from "../assets/images/未标题-1.png";
const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});
// const url = url;
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const app_url = ref(
  "http://121.28.24.104:13898/app/files/%E5%8D%AB%E8%AE%A1%E5%A7%94%E5%8D%8F%E5%90%8C%E5%8A%9E%E5%85%AC%E7%B3%BB%E7%BB%9FAPP.png"
);
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);
const appdialog = ref(false);

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);
function openAppDown() {
  appdialog.value = true;
}
function openOfficeDown() {
  //
  window.location.href = "http://192.168.3.213:9000/public/Windows系统插件.rar";
}
function openQiLinOfficeDown() {
  //
  window.location.href =
    "http://192.168.3.213:9000/public/国产化操作系统插件.zip";
}
function openHandle() {
  window.location.href =
    "http://121.28.24.104:13898/app/files/OA%E5%8D%8F%E5%90%8C%E5%8A%9E%E5%85%AC%E7%94%A8%E6%88%B7%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C.doc";
}
function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          router.push({ path: redirect.value || "/" });
        })
        .catch((error) => {
          console.log(error);

          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCode();
getCookie();
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #3cadf4;
  font-size: 28px;
}
.login-btn {
  min-width: 400px;
  height: 65px;
  // align-content: center;
  max-width: 82%;
  background-image: url("../assets/images/button.png");
  background-size: cover;
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  border: none;
  margin-top: 15px;
}
.login-form {
  border-radius: 15px;
  background: #ffffff;
  width: 450px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
::v-deep .el-checkbox__label {
  display: inline-block;
  padding-left: 8px;
  line-height: 1;
  font-size: 18px;
  font-weight: 700;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
::v-deep .el-dialog__title {
  line-height: var(--el-dialog-font-line-height);
  font-size: 25px !important;
  color: var(--el-text-color-primary);
  padding-left: 10px;
  padding-top: 50px !important;
}
::v-deep .el-dialog__body {
  padding: 10px 20px;
  color: var(--el-text-color-regular);
  font-size: var(--el-dialog-content-font-size);
  word-break: break-all;
}
</style>
