<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";
  import {useI18n} from "vue-i18n";

  const modelValue = defineModel('modelValue', {required: true, default: false})
  const { t } = useI18n();
  const userStoreInstance = userStore();

  // 登出用户
  const loginOut = () => {
    MessageUtils.confirmMessageBox(t('baseTip.confirmTip') + `${userStoreInstance.getUserInfo().userId}?`, t('baseTip.warning')).then(() => {
      UserApi.logout().then((res) => {
        const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
        if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
          MessageUtils.successMessage(t('baseTip.success'));
        } else {
          MessageUtils.warningMessageBox(response.msg);
        }
      }).catch(() => {}).finally(() => {
        router.push({name:'login'});
      });
    }).catch(() => {})
  };
  // 重启服务器
  const restart = () => {
    MessageUtils.confirmMessageBox(t('baseTip.confirmTip'), t('baseTip.warning')).then(() => {
      DevApi.restartApplication().then((res) => {
        const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
        if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
          MessageUtils.successMessage(t('baseTip.success'));
        } else {
          MessageUtils.warningMessageBox(response.msg);
        }
      })
    })
   }
</script>

<template>
  <el-drawer
      v-model="modelValue"
      :title="t('userSetting.userSetting')"
      direction="rtl"
      size="20%"
      destroy-on-close
      @close="modelValue = false"
  >
    <el-form label-width="150px" label-position="left">
      <el-form-item :label="t('userSetting.userName')">
        <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="t('userSetting.restartServer')">
          <el-button type="danger" @click="restart">{{t('userSetting.clickToRestartServer')}}</el-button>
        </el-form-item>
      </app-developer>
    </el-form>
    <el-button type="warning" style="width: 100%; margin-top: 20px" @click="loginOut">{{t('userSetting.logout')}}</el-button>
  </el-drawer>
</template>

<style scoped>

</style>