<template>
    <el-dialog v-model="removeVisible" title="退出团队/企业" style="width:400px;" @close="closed">
      <main>
        <el-alert type="warning" show-icon :closable="false">
            <template #title>
                <span class="text-normal text-dark">你正在退出<span class="text-weight-bolder">{{ userInfo.company }}</span>，退出后，你将无法访问本团队/企业，其在「我的空间」中的所有内容将被清空，且不可恢复，请谨慎操作</span>
            </template>
        </el-alert>
        <div class="text-dark q-my-md">如确认退出团队/企业并清空内容，请输入「退出并清空」</div>
        <el-input v-model="removeTokenStr" size="large" class="full-width" input-style="font-family:pingfangr" placeholder="请输入：退出并清空" />
      </main>
      <template #footer>
        <el-button @click="closed" size="large" class="ppt-button" :disabled="loading">我再想想</el-button>
        <el-button :type="removeTokenStr!='退出并清空'?'info':'primary'" size="large" @click="exitEnterpriseHandler" :loading="loading" :disabled="removeTokenStr!='退出并清空' || loading" class="ppt-button">退出并清空</el-button>
      </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ApiResult } from "@/configs/axios";
import UserServices from "@/services/UserService";
import { useUserStore } from "@/store";
import { ElMessage } from "element-plus";
import { storeToRefs } from "pinia";
import { ref } from "vue";

const emit = defineEmits<{
  (event: 'close'): void
}>()

const loading = ref(false)
const useUser = useUserStore()
const {userInfo} = storeToRefs(useUser)
const removeVisible = ref(true)
const removeTokenStr = ref('')

const exitEnterpriseHandler = async ()=>{
    if(loading.value || removeTokenStr.value!='退出并清空') return
    loading.value = true

    const response = await UserServices.UserExitTenantAsync()

    if(response.data.resultCode == ApiResult.SUCCESS){
        ElMessage.success({message:`成功退出了「${userInfo.value.company}」`})
        useUser.setNewUserInfo(response.data.data)
        setTimeout(() => {
            location.reload()
        }, 1000);
    }else{
        ElMessage.error({message:response.data.message})
        loading.value=false
    }
    // rejectHandler()
}
const closed = ()=>emit('close')
</script>

<style>

</style>