<template>
    <div class="content">
        <Abutton @click="Runing">航行</Abutton>.<Abutton @click="AutoRuning">{{ runState == 0 ? "自动航行" : "取消自动" }}
        </Abutton>.
        <Abutton @click="StopRun">取消</Abutton><br>
        航线：{{ data.onCity }} ⇒ {{ data.toCity }}<br>
        航距：{{ position }}/{{ distance - position }}/{{ distance }}海里<br>
        你可以:
        <span v-if="isShip == 1">
            <Abutton>钓鱼</Abutton>
        </span>
        <br>
        安全：{{ GameTool.GetSeaMapSafetyName(safety) }}<br>
    </div>
    <div class="content">
        您看到：<br>
        <div class="common">
            <span v-for="item in business">
                <Abar :href='"/map/business?id="+item.busId'>{{ item.busName }}</Abar>
            </span>
        </div>
    </div>
</template>
<script setup lang="ts">

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



const BindData = async (): Promise<void> => {
    let result = await MapService.GetUserRun();
    if (result.code == 0) {
        data.value = result.data;
        distance.value = result.data.distance;
        position.value = result.data.position;
        isShip.value = result.data.isShip;
        safety.value = result.data.safety;
        business.value = result.data.business;
        if (runState.value == 1) {
            setTimeout(async () => {
                await Runing();
            }, 1000);
        }

    }
    else if (result.code == 100) {
        PageExtend.RedirectTo("/map");
    }
    else {
        MessageExtend.ShowDialogEvent("提示", result.msg, () => {
            PageExtend.Redirect("/map");
        });
    }
};

let runLock = false;
const Runing = async () => {
    if (runLock) {
        return;
    }
    runLock = true;
    let result = await MapService.UserMapRuning();
    if (result.code == 0) {
        if (result.msg != '') {
            MessageExtend.ShowToast(result.msg);
        }
        await BindData();
    }
    else if (result.code == 100) {
        PageExtend.RedirectTo("/map");
    }
    else {
        MessageExtend.ShowDialog("提示", result.msg);
    }
    runLock = false;
}

const StopRun = async () => {
    MessageExtend.ShowConfirmDialogAsyc("航海操作", `您确定要取消航行吗?`, async () => {
        let result = await MapService.StopUserRun();
        if (result.code == 0) {
            PageExtend.RedirectTo("/map");
        }
        else {
            MessageExtend.Notify(result.msg, "danger");
        }
        return true;
    });
}
const AutoRuning = async () => {
    if (runState.value == 0) {
        runState.value = 1;
        await Runing()
    }
    else {
        runState.value = 0;
    }
}
</script>