<template>
    <div>
        <DialogPro v-model="open" :title="$t('app.checkTitle')" size="small">
            <el-alert :closable="false" :title="$t('setting.systemIPWarning')" type="info">
                <el-link icon="Position" @click="jumpToPath(router, '/settings/panel')" type="primary">
                    {{ $t('firewall.quickJump') }}
                </el-link>
            </el-alert>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="open = false">{{ $t('commons.button.cancel') }}</el-button>
                </span>
            </template>
        </DialogPro>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { getAgentSettingInfo } from '@/api/modules/setting';
import i18n from '@/lang';
import { MsgError, MsgWarning } from '@/utils/message';
import { jumpToPath } from '@/utils/router';
import { useRouter } from 'vue-router';
import { useGlobalStore } from '@/composables/useGlobalStore';
const { currentNodeAddr, isMaster } = useGlobalStore();
const router = useRouter();

const open = ref();

interface DialogProps {
    port: any;
    ip: string;
    protocol: string;
    path?: string;
    query?: string;
    hash?: string;
}

const acceptParams = async (params: DialogProps): Promise<void> => {
    if (Number(params.port) === 0) {
        MsgError(i18n.global.t('commons.msg.errPort'));
        return;
    }
    let protocol = params.protocol === 'https' ? 'https' : 'http';
    const res = await getAgentSettingInfo();
    if (!res.data.systemIP) {
        if (!isMaster.value || currentNodeAddr.value != '127.0.0.1') {
            res.data.systemIP = currentNodeAddr.value;
        } else {
            open.value = true;
            return;
        }
    }
    const buildUrl = (host: string) => {
        let url = `${protocol}://${host}:${params.port}`;
        if (params.path) {
            url += params.path.startsWith('/') ? params.path : `/${params.path}`;
        }
        if (params.query) {
            url += params.query.startsWith('?') ? params.query : `?${params.query}`;
        }
        if (params.hash) {
            url += params.hash.startsWith('#') ? params.hash : `#${params.hash}`;
        }
        return url;
    };
    if (res.data.systemIP.indexOf(':') === -1) {
        if (params.ip && params.ip === 'ipv6') {
            MsgWarning(i18n.global.t('setting.systemIPWarning1', ['IPv4']));
            return;
        }
        window.open(buildUrl(res.data.systemIP), '_blank');
    } else {
        if (params.ip && params.ip === 'ipv4') {
            MsgWarning(i18n.global.t('setting.systemIPWarning1', ['IPv6']));
            return;
        }
        window.open(buildUrl(`[${res.data.systemIP}]`), '_blank');
    }
};

defineExpose({ acceptParams });
</script>
