<script setup>
import { apiSystemCommon, apiSystemRole } from '@/api/system'
import md5 from 'js-md5'
import { Base64 } from 'js-base64'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const ruleFormRef = ref(null)
const formRules = {
  account: [{ required: true, message: '请输入用户名', trigger: 'blur' },],
  pwd: [{ required: true, message: '请输入密码', trigger: 'blur' },]
}
const formState = reactive({
  account: '',
  pwd: '',
  checked: false
})
async function submitForm(formEl) {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      const t = new Date().getTime()
      const md5key = 'MiQM9YUdf89T2uIH'
      apiSystemCommon.login({
        Username: formState.account,
        password: md5(md5(formState.pwd) + md5key + `${t}`),
        ReqTime: `${t}`,
      }).then(res => {
        if (res.Ret === 200) {
          if (formState.checked) {
            localStorage.setItem('account', Base64.encode(formState.account))
            localStorage.setItem('pwd', Base64.encode(formState.pwd))
          } else {
            localStorage.removeItem('account')
            localStorage.removeItem('pwd')
          }
          localStorage.setItem('token', res.Data.Authorization)
          localStorage.setItem('userName', res.Data.SysRealName)
          getMenuList()
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

// 获取菜单跳转到第一个菜单
function getMenuList() {
  apiSystemRole.menuData().then(res => {
    if (res.Ret === 200) {
      const arr = res.Data || []
      if(arr.length===0){
        ElMessage.warning('您暂无任何菜单权限请联系管理员')
        return
      }
      let path = ''
      if (arr[0]?.Children[0]) {
        path = arr[0].Children[0].Path
      } else {
        path = arr[0].Path
      }
      router.replace(path)
    }
  })
}

//获取用户记住的账号密码 如果有
function getRememberedInfo() {
  const account = localStorage.getItem('account') || null
  const pwd = localStorage.getItem('pwd') || null
  if (account) {
    formState.account = Base64.decode(account)
    formState.pwd = Base64.decode(pwd)
    formState.checked = true
  }
}
getRememberedInfo()

onMounted(() => {
  document.onkeydown = (e) => {
    let keyval = window.event.keyCode;
    if (keyval === 13) {
      submitForm(ruleFormRef.value)
    }
  };
})


</script>

<template>
  <div class="flex bg-white login-page">
    <img class="bg-img" src="@/assets/imgs/login_bg.png" alt="" />
    <div class="form-wrap">
      <h3 class="title">运营管理系统</h3>
      <el-form
        ref="ruleFormRef"
        :model="formState"
        :rules="formRules"
        class="form-box"
      >
        <el-form-item prop="account">
          <el-input v-model="formState.account" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            v-model="formState.pwd"
            placeholder="请输入密码"
            type="password"
          />
        </el-form-item>
        <el-form-item class="remember-cont" prop="checked">
          <el-checkbox v-model="formState.checked" class="remember">
            记住账号密码
            <el-tooltip effect="dark" content="有效期60天" placement="top">
              <el-icon color="#333"><i-ep-QuestionFilled /></el-icon>
            </el-tooltip>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; margin-top: 50px"
            type="primary"
            @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100%;
  align-items: center;
  padding: 30px;
  .bg-img {
    max-width: 800px;
  }
  .form-wrap {
    flex: 1;
    .title {
      text-align: center;
      font-size: 38px;
    }
    .form-box {
      margin: 0 auto;
      width: 400px;
    }
  }
}
</style>