<template>
    <div class="content">
        【打造装备】<Abutton @click="showPaper = true">选择</Abutton> <br>
    </div>
    <div class="content">
        <div v-if="useGoods.goodsId != undefined">

            <div class="common">
                ➥当前选择:{{ useGoods.goodsName }}
            </div>
            <div class="common">
                <strong>打造所需</strong>：<br>
                <GamePropVerify :data="verifyData.needs"></GamePropVerify>
            </div>
            <div class="common" v-if="verifyData.result == true">
                [<Abutton @click="MakeEqu">立即打造</Abutton>]
            </div>
        </div>
        <div v-else>
            暂未选择打造图.
        </div>
    </div>
    <div class="clear"></div>
    <div class="content" style="font-size: 15px;">
        说明:<br>
        1.使用打造图后，随机获得一件装备，装备等级按照打造图纸中的说明。<br>
        2.打造出的装备无需鉴定。<br>
        3.打造方式先选择打造图纸，然后收集足够的材料即可完成打造。
    </div>

    <GamePopup v-model:show="showPaper" title="【选择图纸】" style="width: 60%;max-height: 30%;">
        <!-- 自定义内容 -->
        <div class="content" style="margin-top: 15px;">
            <div v-if="goods.length == 0">
                暂无图纸.
            </div>
            <div class="item" v-for="(item, index) in goods" :key="index">
                {{ index + 1 }}.<Abutton @click="ChoosePaper(item.goods)">{{ item.goods.goodsName }}</Abutton>({{
                    item.count }})
            </div>

        </div>
    </GamePopup>
</template>
<script setup lang="ts">

definePageMeta({
    layout: layout.default,
    middleware: 'page-loading'
})
const goods = ref<Array<any>>([]);
const useGoods = ref<any>({});
const verifyData = ref<any>({});
const needs = ref<Array<any>>([]);

onMounted(async () => {
    try {
        await BindData();
    }
    finally {
        PageLoading.Close();
    }
})
const BindData = async (): Promise<void> => {
    let result = await BusinessService.GetEquPaper();
    if (result.code == 0) {
        goods.value = result.data;
    }
    else {
        MessageExtend.ShowDialog("装备打造", result.msg);
    }
};

const showPaper = ref(false);

const ChoosePaper = async (data: any) => {
    useGoods.value = data;
    MessageExtend.LoadingToast("操作中...");
    let result = await BagService.CheckUserProp(0, 1, data.content);
    MessageExtend.LoadingClose();
    if (result.code == 0) {
        verifyData.value = result.data;
        needs.value = verifyData.value.needs;
        showPaper.value = false;
    }
    else {
        MessageExtend.ShowDialog("选择图纸", result.msg);
    }

}

const MakeEqu = async () => {
    MessageExtend.LoadingToast("打造中...");
    let npcId = LocalStorageHelper.GetOnNpc();
    let result = await BusinessService.HandleEquPaper(Number(npcId), useGoods.value.goodsId);
    MessageExtend.LoadingClose();
    if (result.code == 0) {
        return PageExtend.Redirect("/prop/equ?ue=" + result.data);
    }
    else {
        MessageExtend.ShowDialog("打造装备", result.msg);
    }
}

</script>