<template>
    <DialogPro v-model="open" :title="$t('app.checkTitle')" size="large">
        <el-row>
            <el-col :span="20" :offset="2" v-if="open">
                <el-alert
                    type="error"
                    :description="$t('app.deleteHelper', [$t('runtime.runtime')])"
                    center
                    show-icon
                    :closable="false"
                />
                <br />
                <el-descriptions border :column="1" class="mt-5">
                    <el-descriptions-item
                        v-for="(item, key) in map"
                        :key="key"
                        label-class-name="check-label"
                        class-name="check-content"
                        min-width="60px"
                    >
                        <template #label>
                            <a href="javascript:void(0);" class="check-label-a" @click="toPage(item[0])">
                                {{ $t(item[0] === 'website' ? 'menu.website' : 'app.' + item[0]) }}
                            </a>
                        </template>
                        <span class="resources">
                            {{ map.get(item[0]).toString() }}
                        </span>
                    </el-descriptions-item>
                </el-descriptions>
                <div v-if="installData.key === 'openresty'" class="mt-5">
                    <el-checkbox v-model="forceDelete" label="true">{{ $t('app.forceDelete') }}</el-checkbox>
                    <ErrPrompt :title="$t('app.openrestyDeleteHelper')" />
                </div>
            </el-col>
        </el-row>

        <template #footer v-if="forceDelete">
            <span class="dialog-footer">
                <el-button @click="open = false">
                    {{ $t('commons.button.cancel') }}
                </el-button>
                <el-button v-permission type="primary" @click="onConfirm">
                    {{ $t('commons.button.confirm') }}
                </el-button>
            </span>
        </template>
    </DialogPro>
</template>
<script lang="ts" setup>
import { App } from '@/api/interface/app';
import i18n from '@/lang';
import { MsgSuccess } from '@/utils/message';
import { newUUID } from '@/utils/id';
import { ref } from 'vue';
import { DeleteRuntime } from '@/api/modules/runtime';
import { routerToName } from '@/utils/router';
import ErrPrompt from '@/components/error-prompt/index.vue';

interface CheckRrops {
    items: App.AppInstallResource[];
    installID: number;
    key: string;
}
const installData = ref<CheckRrops>({
    items: [],
    installID: 0,
    key: '',
});
const open = ref(false);
const map = new Map();
const forceDelete = ref(false);
const em = defineEmits(['close', 'task']);

const acceptParams = (props: CheckRrops) => {
    map.clear();
    forceDelete.value = false;
    installData.value.installID = props.installID;
    installData.value.key = props.key;
    installData.value.items = [];
    installData.value.items = props.items;
    installData.value.items.forEach((item) => {
        if (map.has(item.type)) {
            const array = map.get(item.type);
            array.push(item.name);
            map.set(item.type, array);
        } else {
            map.set(item.type, [item.name]);
        }
    });
    open.value = true;
};

const toPage = (key: string) => {
    if (key === 'website') {
        routerToName('Website');
    }
};

const onConfirm = () => {
    ElMessageBox.confirm(
        i18n.global.t('app.operatorHelper', [i18n.global.t('commons.button.delete')]),
        i18n.global.t('commons.button.delete'),
        {
            confirmButtonText: i18n.global.t('commons.button.confirm'),
            cancelButtonText: i18n.global.t('commons.button.cancel'),
            type: 'info',
        },
    ).then(() => {
        const taskID = newUUID();
        const params = {
            id: installData.value.installID.valueOf(),
            forceDelete: true,
            taskID: taskID,
        };
        DeleteRuntime(params).then(() => {
            MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
            open.value = false;
            em('task', taskID);
            em('close', open.value);
        });
    });
};

defineExpose({
    acceptParams,
});
</script>

<style scoped>
.resources {
    word-break: break-all;
}
</style>
