<template>
  <div class="login">
    <div class="mo">
      <el-image
        style="width: 400px; height: 158px; margin-bottom: 10em"
        :src="logo"
        fit="fill"
      >
        <template #error>
          <div class="image-slot">
            <el-icon><Picture /></el-icon>
          </div>
        </template>
      </el-image>
      <el-form
        ref="formEle"
        :model="form"
        size="large"
        class="from"
        :rules="rules"
      >
        <el-form-item label="账号" prop="name">
          <el-input
            placeholder="请输入账号"
            @keyup.enter="onSubmit"
            v-model="form.name"
          />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input
            @keyup.enter="onSubmit"
            placeholder="请输入密码"
            type="password"
            v-model="form.pwd"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit"> 登 录 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import sha256 from 'sha256';
import { useRouter, useRoute } from 'vue-router';
import { login, getInfo } from '../../api/index';
import logo from '../../assets/img/logo.png';
const router = useRouter();
const route = useRoute();
const formEle = ref(null);
const form = reactive({
  name: '',
  pwd: '',
});
const rules = {
  name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
};

document.title = '陕西广电融媒体集团';

async function onSubmit() {
  await formEle.value.validate((valid, fields) => {
    if (!valid) return console.log('error submit!', fields);
    login({
      data: {
        loginName: form.name,
        loginPassword: sha256(form.pwd),
      },
    }).then(r => {
      localStorage.setItem('token', r.token);
      getInfo({}).then(res => {
        let rule = res && res.userRole ? res.userRole.menus || [] : [];
        localStorage.setItem('menu', JSON.stringify(rule));
        router.replace({
          path: rule[0] ? rule[0].path : '',
          query: {
            ...route.query,
          },
        });
      });
    });
  });
}
</script>

<style scoped>
.login * {
  box-sizing: border-box;
}
.login {
  width: 100%;
  height: 100%;
  font-weight: 500;
  font-size: 12px;
  background-image: url(../../assets/img/0.jpg);
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

.login .mo {
  float: right;
  height: 100%;
  width: 35%;
  max-width: 500px;
  text-align: center;
  background: #fff;
  padding: 5.5em 1.5em 1.5em 1.5em;
}

.login .el-form-item:last-child {
  margin: 0;
}
</style>

<style>
.login .el-form-item__label {
  height: 55px;
  color: #333;
  line-height: 55px;
}

.login .el-form-item__content .el-button {
  width: 100%;
}
</style>
