<script setup lang="ts">
  import LanguageSwitch from "@/components/custom/LanguageSwitch.vue";
  import {userStore} from "@/stores/user.ts";
  import MessageUtils from "@/utils/MessageUtils.ts";
  import UserApi from "@/api/user.ts";
  import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
  import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
  import router from "@/router";
  import AppDeveloper from "@/components/manage/AppDeveloper.vue";
  import DevApi from "@/api/dev.ts";

  const modelValue = defineModel('modelValue', {required: true, default: false})

  const userStoreInstance = userStore();

  // 登出用户
  const loginOut = () => {
    MessageUtils.confirmMessageBox(`确定退出账号：${userStoreInstance.getUserInfo().userId}?`, "登出确认").then(() => {
      UserApi.loginOut(userStoreInstance.getUserInfo().userId ?? '').then((res) => {
        const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
        if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
          MessageUtils.successMessage("登出成功");
          router.push({name:'login'});
        } else {
          MessageUtils.warningMessageBox(response.msg);
        }
      }).catch(() => {});
    }).catch(() => {})
  };
  // 重启服务器
  const restart = () => {
    MessageUtils.confirmMessageBox(`确定重启服务?`, "重启确认").then(() => {
      DevApi.restartApplication().then((res) => {
        const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
        if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
          MessageUtils.successMessage("重启成功");
        } else {
          MessageUtils.warningMessageBox(response.msg);
        }
      })
    })
   }
</script>

<template>
  <el-drawer
      v-model="modelValue"
      title="用户信息及设置"
      direction="rtl"
      size="20%"
      destroy-on-close
      :close-on-click-modal="false"
      @close="modelValue = false"
  >
    <el-form label-width="150px" label-position="left">
      <el-form-item label="当前用户：">
        <el-text type="success">{{userStoreInstance.getUserInfo().userName}}</el-text>
      </el-form-item>
      <el-form-item label="语言（language）：">
        <LanguageSwitch style="width: 200px"/>
      </el-form-item>
      <app-developer>
        <el-form-item label="重启服务：">
          <el-button type="danger" @click="restart">点击重启</el-button>
        </el-form-item>
      </app-developer>
    </el-form>
    <el-button type="warning" style="width: 100%; margin-top: 20px" @click="loginOut">退出账号</el-button>
  </el-drawer>
</template>

<style scoped>

</style>