<script setup lang="ts">

import {computed, ref} from "vue";
import type {IAppTableUserGroupVo} from "@/model/table/IAppTableUserGroupVo.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import {userStore} from "@/stores/user.ts";
import StringUtils from "@/utils/StringUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import UserGroupApi from "@/api/userGroup.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const groupData = ref<IAppTableUserGroupVo>({groupLevel: 1});
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter();
const userStoreInstance = userStore();
const maxGroupLevel = computed(() => userStoreInstance.getUserInfo().groupLevel);
const close = () => {
  modelValue.value = false;
};

// 添加用户组
const addUserGroup = () => {
  if(!groupData.value.userGroup || StringUtils.isNullOrEmpty(groupData.value.userGroup)
      || !groupData.value.groupName || StringUtils.isNullOrEmpty(groupData.value.groupName)
      || !groupData.value.groupLevel || !groupData.value.groupStatus) {
    MessageUtils.warningMessageBox('存在必填的项未填');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加该用户组？', '添加确认').then(() => {
    UserGroupApi.addUserGroup(groupData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      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:30vh">
        <el-form :model="groupData" label-position="left" label-width="100px" require-asterisk-position="right" style="width: 95%">
          <el-form-item label="用户组ID:" required>
            <el-input v-model="groupData.userGroup" placeholder="请填写用户组ID，该ID必须唯一">
              <template #prepend>ug-</template>
              <template #append><el-button @click="groupData.userGroup = new Date().getTime().toString()">随机一个</el-button></template>
            </el-input>
          </el-form-item>
          <el-form-item label="用户组名称:" required>
            <el-input v-model="groupData.groupName" placeholder="请填写用户组名称"></el-input>
          </el-form-item>
          <el-form-item label="用户组状态:" required>
            <el-select v-model="groupData.groupStatus" placeholder="请选择用户组状态">
              <el-option v-for="item in trueFalseTagStyleFormatter.canUseTagStyle" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户组级别:" required>
            <el-input-number v-model="groupData.groupLevel" :min="1" :max="maxGroupLevel"></el-input-number>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="addUserGroup">添加用户组</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>