<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">经销商设备管理中心</h3>
      <el-form-item prop="tel">
        <el-input v-model="loginForm.tel" type="text" size="large" auto-complete="off" placeholder="手机号">
          <template #prefix><svg-icon icon-class="tel" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <div class="login-tel-code">
          <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="短信验证码" style="width: 64%" @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="telCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <div class="login-code">
            <el-button v-if="isTelCodeShow" disabled>{{ countdown }}后重新获取</el-button>
            <el-button v-else type="primary" @click="sendTelCode">发送验证码</el-button>
          </div>
        </div>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { getTelCode } from "@/api/login"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from "@/store/modules/user"
import { deepClone } from "@/utils"

const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()

const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  tel: "",
  code: "",
  uuid: "",
})

const loginRules = {
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
}

const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)
// 短信验证码倒计时
let countdown = ref(0)
const isTelCodeShow = ref(false)
let countdownTimer = null

watch(
  route,
  newRoute => {
    redirect.value = newRoute.query && newRoute.query.redirect
  },
  { immediate: true }
)

function sendTelCode() {
  if (!loginForm.value.tel) {
    proxy.$modal.msgWarning("请输入手机号")
    return
  }
  getTelCode({ tel: loginForm.value.tel }).then(res => {
    if (res.code === 200) {
      proxy.$modal.msgSuccess("验证码发送完成，请查看短信")
      countdown.value = 60
      isTelCodeShow.value = true
      startCountdown()
    }
  })
}

// 倒计时
function startCountdown() {
  if (countdown.value > 0 && countdown.value <= 60) {
    countdownTimer = setTimeout(() => {
      countdown.value--
      startCountdown()
    }, 1000)
  } else {
    isTelCodeShow.value = false
    if (countdownTimer) {
      clearTimeout(countdownTimer)
      countdownTimer = null
    }
  }
}

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      // 调用action的登录方法
      userStore
        .telLogin(loginForm.value)
        .then(() => {
          router.push({ path: "/screen", query: {} })
        })
        .catch(() => {
          loading.value = false
        })
    }
  })
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-screen-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 20px auto;
  text-align: center;
  color: #ffffff;
}

.login-form {
  border-radius: 6px;
  background-image: url("../assets/images/login-card-bg.png");
  background-size: cover;
  width: 400px;
  padding: 25px 25px 13px 25px;
  .el-input {
    height: 40px;
    --el-input-border-color: #409eff;
    input {
      height: 40px;
    }
  }

  :deep(.el-input__wrapper) {
    background-color: transparent;
  }
  :deep(.el-input__inner) {
    color: #ffffff;
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-tel-code {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;

  .login-code {
    .send-code {
      color: #a9abb1;
      border: 1px solid #a9abb1;
    }
    .el-button {
      height: 38px;
    }
  }
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
