<template>
  <div class="login">
    <!-- <div v-if="isMobile"  class="flexRegis" @click="registerShow">
      需求登记
    </div> -->
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      :class="{ismobile:isMobile}"
    >
      <div class="loginHeader">欢迎登录遁地龙</div>
      <div class="loginType">
        <div class="active">账号密码登录</div>
        <!-- <div v-if="isMobile"  @click="registerShow">
          <span  class="mobilecss" >需求登记</span>
        </div> -->
      </div>
      <el-divider />
      <div class="loginMain">
        <div class="mainTop">
          <div class="title">密码登录</div>

          <div>
            <el-tooltip
              class="box-item"
              effect="dark"
              raw-content
              content=" <div class='openscal'>扫码登录</div> "
              placement="left"
            >
              <img src="../assets/scan.png" />
            </el-tooltip>
          </div>
        </div>

        <el-form-item prop="username" style="height: 50px">
          <el-input
            v-model="loginForm.username"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
          >
          </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: 63%"
            @keyup.enter="handleLogin"
          >
          </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"
          >记住密码</el-checkbox
        >
        <div class="register"  @click="registerShow">需求登记</div>

        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="large"
            type="primary"
            style="width: 100%; height: 50px; border-radius: 30px"
            @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>
        </el-form-item>
      </div>
    </el-form>
    <!-- 注册 -->
    <el-dialog :width="dialogWidth"
      v-model="registerDialog"
      title="需求登记"
      destroy-on-close
      :close-on-click-modal="false"
      class = "regitDialog"
    >
    <el-input
    v-model="registerForm.remark"
    type="textarea"
    size="large"
    :autosize="{ minRows: 5 }"
    auto-complete="off"
    placeholder="可描述详细需求"
  >
  </el-input>

      <el-form
        ref="registerRef"
        class="registerForm"
        :model="registerForm"
        :rules="registerRules"
        style="margin-top: 20px;"
        label-width="80"
      >
      <el-form-item prop="trialScope" label="需求内容">
        <el-select
          style="width: 100%"
          v-model="registerForm.trialScope"
          class="m-2"
          multiple
          placeholder="请选择"
          size="large"
        >
        <el-option key="管线设计" label="管线设计" value="管线设计" />
        <el-option key="运营合作" label="运营合作" value="运营合作" />
        <el-option key="设备合作" label="设备合作" value="设备合作" />
        <el-option key="项目排查" label="项目排查" value="项目排查" />
        <el-option key="其他" label="其他" value="其他" />
        </el-select>
      </el-form-item>
        <el-form-item prop="name" label="姓名">
          <el-input
            v-model="registerForm.name"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>

        <el-form-item prop="mobile" label="手机号">
          <el-input
            v-model="registerForm.mobile"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="company" label="单位">
          <el-input
            v-model="registerForm.company"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="position" label="职务">
          <el-input
            v-model="registerForm.position"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱">
          <el-input
            v-model="registerForm.email"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="请输入"
          >
          </el-input>
        </el-form-item>


      </el-form>
      <el-button
        :loading="loadingRegister"
        size="large"
        type="primary"
        style="width: 100%; height: 50px; border-radius: 30px"
        @click.prevent="handRegister"
      >
        <span v-if="!loadingRegister">需求登记</span>
        <span v-else>登 记 中...</span>
      </el-button>
    </el-dialog>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2023-2123 江苏力鼎环保装备有限公司</span>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { trialApply } from "@index/api/menu";
const { proxy } = getCurrentInstance();
import {validEmail} from '@/utils/validate'
const router = useRouter();
import Cookies from "js-cookie";
import useUserStore from "@index/store/modules/user";
const userStore = useUserStore();
const registerDialog = ref(false);
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});
const registerForm = ref({
  name: "",
  mobile: "",
  email: "",
  company: "",
  position: "",
  trialScope: [],
  remark: "",
});
const dialogWidth = ref('')
const isMobile = ref(false)
onMounted(() => {
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

  if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
    dialogWidth.value = '95%'
    registerDialog.value = true;
    isMobile.value = true
  } else {
    dialogWidth.value = '30%'
    isMobile.value = false
  }
})
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};
const checkmobile = (rule,value,callback)=>{
  if(registerForm.value.mobile.length==11){
     return callback()
   }
   callback(new Error("请输入正确的11位手机号"))
}
const checkemail = (rule,value,callback)=>{
  if(validEmail(registerForm.value.email)){
     return callback()
   }
   callback(new Error("请输入正确的邮箱"))
}
const registerRules = {
  name: [{ required: true, trigger: "blur", message: "请输入" }],
  mobile: [{ required: true, trigger: "blur", validator:checkmobile}],
  email: [{ required: true, trigger: "change", validator:checkemail }],
  company: [{ required: true, trigger: "blur", message: "请输入" }],
  position: [{ required: true, trigger: "blur", message: "请输入" }],
  trialScope: [{ required: true, trigger: "change", message: "请选择" }],
};
function registerShow() {
  registerDialog.value = true;
  registerForm.value = {
    name: "",
    mobile: "",
    email: "",
    company: "",
    position: "",
    trialScope: [],
    remark: "",
  };
}
const codeUrl = ref("");
const loading = ref(false);
const loadingRegister = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);
function handRegister() {
  if(registerForm.value.remark == ""){
    proxy.$modal.msgWarning("请填写需求");
    return;
  }
  proxy.$refs.registerRef.validate((valid) => {
    if (valid) {
      loadingRegister.value = true;
      let data =JSON.parse(JSON.stringify(registerForm.value))
      data.trialScope = data.trialScope.toString()
      trialApply(data).then((res) => {
        loadingRegister.value = false;
        proxy.$modal.msgSuccess("提交申请成功");
        registerDialog.value = false;
      });
    }
  });
}
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(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}
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),
  };
}
function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.data.img;
      loginForm.value.uuid = res.data.uuid;
    }
  });
}
getCode();
getCookie();
</script>

<style lang="scss" scoped>
.login {
  display: flex;

  align-items: center;
  height: 100%;
  justify-content: end;

  background-image: url("../assets/bg.jpg");
  background-size: cover;
}
:deep(.el-form-item--default .el-form-item__error) {
  padding-top: 8px;
}
:deep(.registerForm .el-form-item--default .el-form-item__error) {
  padding-top: 4px;
}
:deep(.regitDialog .el-dialog__body){
  padding-top: 10px;
}
.el-divider--horizontal {
  margin: 0;
}
.loginType {
  height: 70px;
  line-height: 70px;
  font-weight: bold;
  font-size: 18px;

  color: #707070;
  display: flex;
  justify-content: space-between;
  padding: 0 15%;
  div {
    padding: 0 15px;
  }
  .active {
    color: #409eff;
    border-bottom: 2px solid #409eff;
  }
}
.loginMain {
  padding: 30px 13%;
  .mainTop {
    display: flex;
    justify-content: space-between;

    .title {
      color: #666;
      font-weight: bold;

      font-size: 20px;
    }
    img {
      width: 60px;
      cursor: pointer;
    }
  }
}
.openscal {
  color: red;
}
.ismobile{
  margin-right: 0 !important;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 600px;
  margin-right: 15%;

  box-shadow: 0px 0px 20px #999;

  .el-input {
    margin-top: 5px;
    height: 50px;

    input {
      height: 50px;
    }
  }
  .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;
  }
}
.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: 46px;
  width: 165px;
  padding-left: 12px;
}
.loginHeader {
  text-align: center;
  font-size: 28px;
  margin-top: 30px;
  color: #666;
  font-weight: bold;
}
.register {

  line-height: 1;
  min-height: 40px;
  white-space: nowrap;
  cursor: pointer;
  background: #fff;
  border: 1px solid #dcdfe6;
  border-color: #dcdfe6;
  color: #606266;

  text-align: center;

  outline: none;
  margin: 0;
  transition: 0.1s;
  font-weight: 500;

  padding: 12px 20px;
  font-size: 14px;
  border-radius: 2.5px;


  float: right;
  border: 1px solid #1890ff;
  color: #1890ff;
  border-radius: 2.5px;
}
.flexRegis{
  position: absolute;
  right: 0;
  color:#fff;
}
.mobilecss{
  padding: 5px 10px;
  border: 1px solid #1890ff;
  border-radius: 3px;
}
</style>
