<template>
    <view class="container">
        <!-- 底部按钮 -->
        <view class="footer">
            <!-- 查询区域 -->
            <view class="query-section">
                <u-radio-group v-model="formData.searchType">
                    <u-radio label="查订单" name="1" labelSize="16px" iconSize="16px" />
                    <u-radio label="查物流" custom-style="margin-left:20px" name="2" labelSize="16px" iconSize="16px" />
                </u-radio-group>

                <view class="search-box">
                    <u-input
                        custom-style="font-size:32rpx"
                        placeholder-style="fong-size:32rpx"
                        v-model="formData.search"
                        :placeholder="formData.searchType == '1' ? '扫描/输入订单号' : '扫描/输入物流单号'"
                        border="surround"
                        clearable
                    >
                    </u-input>
                    <u-button color="#c8c8c8" text="查询" @click="handleSubmit" />
                </view>
            </view>

            <u-divider></u-divider>

            <view style="display: flex">
                <u-button size="large" type="success" text="扫码" @click="handleScan" />
            </view>
        </view>
    </view>
</template>

<script setup>
import { ref, onUnmounted } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";

const formData = ref({
    searchType: "2",
    search: "",
});

// 处理查询
const handleSubmit = async () => {
    if (!formData.value.search) {
        uni.$u.toast("请扫描/输入物流单号");
        return;
    }
    
    try {
        const res = await uni.$u.http.get("/app/workOrder/scanWorkOrder", {
            params: {
                waybillCode: formData.value.searchType === '2' ? formData.value.search : undefined,
                orderId: formData.value.searchType === '1' ? formData.value.search : undefined,
                type: 2
            }
        });
        
        if (res.code === 200) {
            const reqWaybillCode = formData.value.searchType === '2' ? formData.value.search : '';
            const reqOrderId = formData.value.searchType === '1' ? formData.value.search : '';
            
            if (res.data && res.data.id) {
                // 存在历史工单，跳转到历史工单页面
                uni.navigateTo({
                    url: `/pages/index/work-order/history?workOrderId=${res.data.id}&type=2&waybillCode=${res.data.waybillCode}&orderId=${res.data.orderId}`,
                });
            } else {
                // 不存在历史工单，跳转到创建工单页面，携带回填信息
                const orderId = res.data?.orderId || reqOrderId;
                const waybillCode = res.data?.waybillCode || reqWaybillCode;
                const expressType = res.data?.expressType || '';
                uni.navigateTo({
                    url: `/pages/order/recycle/created?waybillCode=${waybillCode}&orderId=${orderId}&expressType=${expressType}&readonly=1`,
                });
            }
        } else {
            uni.$u.toast(res.msg || "查询失败");
        }
    } catch (error) {
        console.error(error);
        uni.$u.toast("查询失败");
    }
};

// 处理扫码
const handleScan = () => {
    uni.scanCode({
        success: (res) => {
            formData.value.search = res.result;
            handleSubmit();
        },
    });
};

// 全局监听事件
// #ifdef APP-PLUS
const { unregister } = uni.$u.useEventListener((e) => {
    formData.value.search = e.barcode;
    handleSubmit();
});
// #endif

onUnmounted(() => {
    // #ifdef APP-PLUS
    unregister();
    // #endif
});

</script>

<style lang="scss" scoped>
@import "../components/common.scss";
</style>
