<template>
  <div style="margin-top: 50px;">
    <el-row :gutter="20">
      <el-col :span="6" :offset="9">
        <el-form ref="registerFrm" :model="state.register" :rules="state.rules" :label-width="120">
          <el-form-item label="授权码" prop="authCode">
            <el-input v-model="state.register.authCode" placeholder="请输入授权码"></el-input>
          </el-form-item>
          <el-form-item label="用户姓名" prop="userName">
            <el-input v-model="state.register.userName" placeholder="请输入用户姓名"></el-input>
          </el-form-item>
          <el-form-item label="登录账号" prop="loginId">
            <el-input v-model="state.register.loginId" placeholder="请输入登录账号"></el-input>
          </el-form-item>
          <el-form-item label="登录密码" prop="loginPwd">
            <el-input v-model="state.register.loginPwd" type="password" show-password placeholder="请输入登录密码"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="loginPwd2">
            <el-input v-model="state.register.loginPwd2" type="password" show-password placeholder="请再一次输入登录密码">
            </el-input>
          </el-form-item>
          <el-form-item style="text-align: center;width: 100%;">
            <el-button type="primary" style="min-width: 330px;" @click="handleRegister" size="large">用 户 注 册</el-button>
            <div class="login-reg">
              已有账号? <router-link :to="{ path: '/Login' }">返回登陆</router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs, ref, onMounted } from "vue";
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import { CustomUserDto, Dialog, PageResult } from '@/types';
import { useRouter } from "vue-router";
import CustomUserAction from '@/api/customermanagement/customuser';

const state = reactive({
  register: {} as CustomUserDto,
  rules: {
    authCode: [
      { required: true, message: '请输入授权码', trigger: "blur" },
    ],
    loginId: [
      { required: true, message: '请输入账号名', trigger: "blur" },
      { min: 3, max: 15, message: "登录账号长度不合法", trigger: "blur" },
    ],
    userName: [
      { required: true, message: '请输入用户姓名', trigger: "blur" },
      { min: 3, max: 15, message: "用户姓名长度不合法", trigger: "blur" },
    ],
    loginPwd: [
      { required: true, message: '请输入密码', trigger: "blur" },
      { min: 3, max: 15, message: "登录密码长度不合法", trigger: "blur" },
    ],
    loginPwd2: [
      { required: true, message: '请再次输入密码', trigger: "blur" },
      { min: 3, max: 15, message: "登录密码长度不合法", trigger: "blur" },
    ],
  }
});

const {
  register
} = toRefs(state);

const router = useRouter();
const registerFrm: any = ref(null);
const handleRegister = () => {
  registerFrm.value.validate((valid: any) => {
    if (valid) {
      if (state.register.loginPwd !== state.register.loginPwd2) {
        ElMessage.error("两次密码不一致！！！");
        return;
      }

      CustomUserAction.RegistUser(state.register).then(result => {
        if (result.status === 200) {
          const { res, flag, error } = result.data;
          if (flag === "1") {
            ElMessage.success("注册成功！！！");
            router.push({
              path: "/Login",
            });
          }
          else {
            ElMessage.error(error);
          }
        }
      }).catch(error => {
        //请求失败时的操作
        ElMessage.error(error);
      })
    }
  })
}
</script>

<style lang="scss" scoped>
.login-reg {
  margin-top: 10px;
  margin-left: 205px;
  font-size: 14px;
  color: var(--el-text-color-primary);
}
</style>