<template>
    <div class="content">
        <div class="common">
            【{{ data.npcName }}】
        </div>
        <div class="common">
            {{ data.story }}
        </div>
    </div>
    <div class="content" v-if="data.code == 'Default'">
        <div class="item" v-for="item in data.bus">
            ◈<Abar :href="item.url">{{ item.name }}</Abar>
        </div>
    </div>
    <div class="content" v-if="data.code == 'Event'">
        <div class="item" v-for="item in data.bus">
            ◈<Abutton @click="HandleEvent(item)">{{ item.name }}</Abutton>
        </div>
    </div>
    <div class="content" v-if="data.code == 'Task'">
        <div class="common" v-if="task.length > 0">
            当前任务:<br>
            <div class="item" v-for="item in task">
                <img src="/images/site/tg.gif" v-if="item.state == 0" />
                <img src="/images/site/tr.gif" v-if="item.state == 1" />
                <Abar :href='"/task/info?task=" + item.itemId'>[{{ item.code }}]{{ item.name }}</Abar>
            </div>
        </div>
        <div>
            <div class="item" v-for="item in data.bus">
                ◈<Abar :href="item.url">{{ item.name }}</Abar>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">

definePageMeta({
    layout: layout.default,
    middleware: 'page-loading'
})
const data = ref<any>({});
const task = ref<Array<any>>([]);
onMounted(async () => {
    try {
        await BindData();
    }
    finally {
        PageLoading.Close();
    }
})


const BindData = async (): Promise<void> => {
    let npc = PageExtend.QueryString("npc");
    let result = await MapService.GetNpcInfo(Number(npc));
    if (result.code == 0) {
        data.value = result.data.data;
        task.value = result.data.task;
        LocalStorageHelper.SetOnNpc(npc);//存储当前进的npc信息            
    }
    else {
        MessageExtend.ShowDialogEvent("提示", result.msg, () => {
            PageExtend.Redirect("/map");
        });
    }
};

const HandleEvent = async (item: any) => {
    let npc = PageExtend.QueryString("npc");
    MessageExtend.LoadingToast("处理中...");
    let result = await MapService.HandleNpcEvent(Number(npc), item.code, item.parms)
    MessageExtend.LoadingClose();
    if (result.code == 0) {
        MessageExtend.Notify(result.msg, "success");
        if (result.data == 0) {
            PageExtend.RedirectTo("/map");
        }
    }
    else {
        MessageExtend.ShowDialog("提示", result.msg);
    }
}

</script>