<template>
  <div class="login-box">
    <el-form
      ref="ruleFormRef"
      :model="data.ruleForm"
      status-icon
      :rules="rules"
      label-width="70px"
      class="demo-ruleForm"
    >
      <h2>算法集后台管理系统</h2>
      <el-form-item label="账号：" prop="username">
        <el-input
          v-model="data.ruleForm.username"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input
          v-model="data.ruleForm.password"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="loginbtn"
          type="primary"
          @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
        <el-button class="loginbtn" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, toRef, ref } from "vue";
import { LoginData } from "../type/login";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { login, getAllAlgorithm } from "../request/api";
// import { console } from "@vue/runtime-core";
import { useRouter } from "vue-router";
import { useStore } from "@/stores/counter";

const ruleFormRef = ref<FormInstance>();

// const data = reactive({
//   ruleForm:{
//     username:"",
//     password:"",
//   }
// })
// let ruleForm = toRef(data,'ruleForm')
const data = reactive(new LoginData());
const router = useRouter();
const rules = reactive({
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 3, max: 8, message: "账号长度在3-8之间", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 3, max: 12, message: "密码长度为3-12之间", trigger: "blur" },
  ],
});
// 重置
const resetForm = () => {
  (data.ruleForm.password = ""), (data.ruleForm.username = "");
};
// 发送请求
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  // 验证表单内容
  formEl.validate((valid) => {
    if (valid) {
      const store = useStore();
      // console.log("submit!");
      // login(data.ruleForm).then((res) => {
      //   // console.log(res);
      // });
      login(data.ruleForm).then((res) => {
        if (res == null) {
          console.log("00");
          ElMessage.error("服务器未响应");
          return false;
        }
        if (res.data.status == 200) {
          store.auth.user = res.data.message;
          // sessionStorage.setItem("uesrname", res.data.message);

          // console.log("打印存储值");

          // console.log(sessionStorage.getItem("uesrname"));
          console.log(22);

          router.push("/home");
          console.log("跳转成功");
          ElMessage.success(res.data.message);
          getAllAlgorithm().then((res) => {
            console.log("开始打印算法列表");
            console.log(res);
          });
        } else {
          ElMessage.error(res.data.message);
        }

        // console.log(res);
        // console.log("fff");
        // 跳转
      });
    } else {
      console.log("error submit!");
      return false;
    }
  });
  // console.log(formEl);
};
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  background: url("../assets/bg.jpg");
  position: fixed;
  top: 0px;
  left: 0px;
  background-size: 100% 100%;
  padding: 1px;
  text-align: center;
  .demo-ruleForm {
    width: 500px;

    margin: 200px auto;
    background-color: aliceblue;
    padding: 50px;
    border-radius: 30px;
  }
  .loginbtn {
    width: 45%;
  }
  h2 {
    margin-bottom: 20px;
  }
}
</style>
