<template>
  <div class="login-box">
    <el-form
      ref="ruleFormRef"
      style="max-width: 450px;"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="form-box"
    >
      <h2>用户登录</h2>
      <el-form-item label="账号" prop="username" size="large">
        <el-input
          v-model="ruleForm.username"
          type="username"
          placeholder="请输入账号"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password" size="large">
        <el-input
          v-model="ruleForm.password"
          type="password"
          autocomplete="off"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item style="margin: 0px;">
        <el-button
          class="login-submit"
          type="primary"
          @click="submitForm(ruleFormRef)"
        >
          登录
        </el-button>
        <el-button class="login-reset" @click="resetForm(ruleFormRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { LoginData } from '../type/Login'
import { reactive, ref } from 'vue'
import { useUserStore } from '../store/modules/user'
import type { FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { Login } from '../axios/model/user'
export default {
  setup() {
    const ruleFormRef = ref<FormInstance>()
    const ruleForm = reactive(new LoginData())
    const store = useUserStore()
    const router = useRouter()
    const rules = reactive({
      username: [
        {
          required: true,
          message: '请输入账号',
          trigger: 'blur',
        },
      ],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur',
        },
      ],
    })
    //重置
    const resetForm = (formEl: FormInstance | undefined) => {
      ruleForm.username = ''
      ruleForm.password = ''
      if (!formEl) return
      formEl.resetFields()
    }


    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return
      formEl.validate((valid: boolean) => {
        if (valid) {
          let data = {
            username: ruleForm.username,
            password: ruleForm.password,
          }
          Login(data).then(({ success, message, result }: any) => {
            if (success) {
              router.push('/')
              store.setUserInfo({
                ...result.userInfo,
                token: result.token,
              })
            }
          })
        } else {
          return false
        }
      })
    }

    return { ruleForm, ruleFormRef, rules, resetForm, submitForm }
  },
}
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  padding-top: 1px;
  background: url('../assets/login.jpg') no-repeat;
  background-size: 100%;
  .form-box {
    background: #fff;
    .el-input__inner {
      height: 40px;
    }
    h2 {
      text-align: center;
      margin-bottom: 40px;
      font-weight: bolder;
      font-size: 20px;
    }
    margin: 0px auto;
    margin-top: 250px;
    width: 500px;
    text-align: center;
    border-radius: 20px;
    padding: 40px;
    box-shadow: 0px 1px 5px 0 rgba(0, 0, 0, 0.3);
    .login-submit,
    .login-reset {
      width: 48%;
      height: 40px;
    }
  }
}
</style>
