<!--用户详情-->
<script setup lang="ts">

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

const userTagStyleFormatter = new UserTagStyleFormatter(); // 表数据格式化
const newUserInfo = ref<IUpdateUserData>({}); // 用户的需要修改的新信息

const modelValue = defineModel('modelValue', {required: true, default: false});
const props = defineProps(['formData']);
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
};
const formData = computed(() => props.formData);
const userGroupList = ref<IAppTableUserGroupVo[]>([]); // 用户组

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

// 删除用户
const deleteUser = () => {
  MessageUtils.confirmMessageBox(`确定删除用户：${formData.value.userId} 的账户资料？`, "高风险操作确认").then(() => {
    UserApi.deleteUser(formData.value.userId).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("删除成功");
        emit("reLoadingTableData"); // 重新加载用户表数据
        close(); // 关闭此窗口
      } else {
        MessageUtils.warningMessageBox(response.msg);
      }
    }).catch(() => {});() => {}
  }).catch(() => {})
};
// 保存修改
const save = () => {
  newUserInfo.value.userId = formData.value.userId;
  newUserInfo.value.userName = formData.value.userName;
  if(newUserInfo.value.userPwd && newUserInfo.value.userPwd !== "" ) {
    newUserInfo.value.userPwd = StringUtils.getPwdMd5(newUserInfo.value.userPwd);
  }
  MessageUtils.confirmMessageBox(`确定修改用户：${formData.value.userId} 的基础资料？`, "修改确认").then(() => {
    UserApi.updateUser(newUserInfo.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("修改成功");
        emit("reLoadingTableData"); // 重新加载用户表数据
        close(); // 关闭此窗口
      } else {
        MessageUtils.warningMessageBox(response.msg);
      }
    }).catch(() => {});() => {}
  }).catch(() => {})
};
// 登出用户
const loginOut = () => {
  MessageUtils.confirmMessageBox(`确定登出用户：${formData.value.userId}?`, "登出确认").then(() => {
    UserApi.loginOut(formData.value.userId).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("登出成功");
        emit("reLoadingTableData"); // 更新用户表数据
        close(); // 关闭此窗口
      } else {
        MessageUtils.warningMessageBox(response.msg);
      }
    }).catch(() => {});
  }).catch(() => {})
}
// 查询用户组
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(() => {});
}

</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="用户详细信息" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right" style="width: 95%">
          <el-form-item label="用户ID:" required>
            <el-text>{{formData.userId}}</el-text>
          </el-form-item>
          <el-form-item label="用户名称:" required>
            <el-input v-model="formData.userName" placeholder="请填写用户名称"></el-input>
          </el-form-item>
          <el-form-item label="用户状态:" required>
            <el-row style="width:100%">
              <el-col :span="12">
                <el-tag size="small" :type="userTagStyleFormatter.userStatusTagStyleFormatter(formData.userStatus).type">
                  {{userTagStyleFormatter.userStatusTagStyleFormatter(formData.userStatus).label}}</el-tag>
              </el-col>
              <el-col :span="4" :offset="2">修改状态为：</el-col>
              <el-col :span="6">
                <el-select v-model="newUserInfo.userStatus" placeholder="请选择用户状态">
                  <el-option v-for="item in userTagStyleFormatter.userStatusTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="在线状态:">
            <el-row style="width:100%">
              <el-col :span="12">
                <el-tag size="small" :type="userTagStyleFormatter.userOnlineTagStyleFormatter(formData.online).type">
                  {{userTagStyleFormatter.userOnlineTagStyleFormatter(formData.online).label}}</el-tag>
              </el-col>
              <el-col :span="4" :offset="2">
                <el-button type="warning" @click="loginOut">强制下线</el-button>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="用户级别:" required>
            <el-row style="width:100%">
              <el-col :span="12">
                <el-tag size="small" :type="userTagStyleFormatter.userLevelTagStyleFormatter(formData.userLevel).type">
                  {{userTagStyleFormatter.userLevelTagStyleFormatter(formData.userLevel).label}}</el-tag>
              </el-col>
              <el-col :span="4" :offset="2">修改用户级别为：</el-col>
              <el-col :span="6">
                <el-select v-model="newUserInfo.userLevel" placeholder="请选择用户级别">
                  <el-option v-for="item in userTagStyleFormatter.userLevelTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="用户创建时间:">
            <el-text>{{FormatterUtils.formatTime(formData.createTime)}}</el-text>
          </el-form-item>
          <el-form-item label="用户组:">
            <el-row style="width:100%">
              <el-col :span="12">
                <el-text>{{formData.userGroup}}</el-text>
              </el-col>
              <el-col :span="4" :offset="2">修改用户组为：</el-col>
              <el-col :span="6">
                <el-select v-model="newUserInfo.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-col>
            </el-row>
          </el-form-item>
          <el-form-item label="性别:">
            <el-row style="width:100%">
              <el-col :span="12">
                <el-tag size="small" :type="userTagStyleFormatter.userSexTagStyleFormatter(formData.sex).type">
                  {{userTagStyleFormatter.userSexTagStyleFormatter(formData.sex).label}}</el-tag>
              </el-col>
              <el-col :span="4" :offset="2">修改用户性别为：</el-col>
              <el-col :span="6">
                <el-select v-model="newUserInfo.sex" placeholder="请选择用户性别">
                  <el-option v-for="item in userTagStyleFormatter.userSexTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="头像:">
            <el-input v-model="formData.photo" placeholder="点击上传头像（暂不支持）" disabled></el-input>
          </el-form-item>
          <el-form-item label="上次登录时间:">
            <el-text>{{FormatterUtils.formatTime(formData.lastLoginTime)}}</el-text>
          </el-form-item>
          <el-form-item label="修改密码:">
            <el-input v-model="newUserInfo.userPwd" placeholder="输入用户新密码" show-password></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="warning" @click="deleteUser">删除用户(!)</el-button>

          <el-button type="primary" @click="save">保存修改</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>