<script setup lang="ts">

import {onActivated, onMounted, ref} from "vue";
import UserTagStyleFormatter from "@/plugin/formatter/UserTagStyleFormatter.ts";
import type {IAppTableUserGroupVo} from "@/model/table/IAppTableUserGroupVo.ts";
import UserGroupApi from "@/api/userGroup.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import type IAppUserPo from "@/interface/page/user/IAppUserPo.ts";
import UserApi from "@/api/user.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const userTagStyleFormatter = new UserTagStyleFormatter(); // 表数据格式化
const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
};
const userData = ref<IAppUserPo>({}); // 新用户的数据
const userGroupList = ref<IAppTableUserGroupVo[]>([]); // 用户组

onMounted(() => {
  userData.value = {}; // 重置所有的新数据
  loadUserGroup(); // 查询用户组信息
})

// 查询用户组
const loadUserGroup = () => {
  userGroupList.value = [];
  UserGroupApi.queryMinLevelUserGroup().then(res => {
    const response = JSON.parse(JSON.stringify(res.data)) as AppServeDataResponse<IAppTableUserGroupVo[]>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
      userGroupList.value = response.data ?? [];
    } else {
      MessageUtils.warningMessageBox(response.msg);
    }
  }).catch(() => {})
}
// 添加用户
const addUser = () => {
  MessageUtils.confirmMessageBox(`确定添加用户：${userData.value.userId} 的账户资料？`, "操作确认").then(() => {
    UserApi.createUser(userData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("创建用户成功");
        userData.value = {}; // 重置所有的新数据
        emit("reLoadingTableData"); // 重新加载用户表数据
        close(); // 关闭此窗口
      } else {
        MessageUtils.warningMessageBox(response.msg);
      }
    }).catch(() => {});
  }).catch(() => {})
}


</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="请填写要添加用户的详细信息" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="userData" label-position="left" label-width="100px" require-asterisk-position="right" style="width: 95%">
          <el-form-item label="用户ID:" required>
            <el-input v-model="userData.userId" placeholder="请填写用户ID，该ID是唯一登录账号"></el-input>
          </el-form-item>
          <el-form-item label="用户名称:" required>
            <el-input v-model="userData.userName" placeholder="请填写用户名称"></el-input>
          </el-form-item>
          <el-form-item label="密码:" required>
            <el-input v-model="userData.userPwd" placeholder="输入用户初始密码" show-password></el-input>
          </el-form-item>
          <el-form-item label="用户状态:" required>
            <el-select v-model="userData.userStatus" placeholder="请选择用户状态">
              <el-option v-for="item in userTagStyleFormatter.userStatusTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户级别:" required>
            <el-select v-model="userData.userLevel" placeholder="请选择用户级别">
              <el-option v-for="item in userTagStyleFormatter.userLevelTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户组:" required>
            <el-select v-model="userData.userGroup" placeholder="请选择用户组">
              <template #header>
                <el-button type="primary" size="small" @click="loadUserGroup">加载/刷新 用户组</el-button>
              </template>
              <el-option v-for="item in userGroupList" :key="item.userGroup" :label="item.groupName" :value="item.userGroup"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别:" required>
            <el-select v-model="userData.sex" placeholder="请选择用户性别">
              <el-option v-for="item in userTagStyleFormatter.userSexTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="头像:">
            <el-input v-model="userData.photo" placeholder="点击上传头像（暂不支持）" disabled></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="addUser">添加用户</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>