<script setup lang="ts">
import {computed, onMounted, ref} from "vue";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import {userStore} from "@/stores/user.ts";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import UserGroupApi from "@/api/userGroup.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";

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

// 保存用户信息
const save = () => {
  MessageUtils.confirmMessageBox('确定更新该用户组信息？', '更新确认').then(() => {
    UserGroupApi.updateUserGroup(props.groupData).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:40vh">
        <el-form :model="groupData" label-position="left" label-width="120px" require-asterisk-position="right" style="width: 95%">
          <el-form-item label="用户组ID:" required>
            <el-text type="primary">{{groupData.userGroup}}</el-text>
          </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-item label="用户组创建时间:">
            <label type="primary">{{FormatterUtils.formatTime(groupData.createTime)}}</label>
          </el-form-item>
          <el-form-item label="上次修改时间:">
            <label type="primary">{{FormatterUtils.formatTime(groupData.updateTime)}}</label>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <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>