<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 = () => {
    if (!formData.value.search) {
        uni.$u.toast("请扫描/输入物流单号");
        return;
    }
    uni.$u.http.post("/app/orderinfo/confirmOrder", formData.value).then((res) => {
        if (res.code == 200) {
            let text = formData.value.search + "收货成功";
            uni.$u.ttsModule.speak(text);
        } else {
            let text = formData.value.search + "订单不存在";
            uni.$u.ttsModule.speak(text);
        }
    });
};

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

onLoad(() => {
    // #ifdef APP-PLUS
    uni.$u.useGlobalEvent((e) => {
        formData.value.search = e.barcode;
        handleSubmit();
    });
    // #endif
});
onShow(() => {
    uni.$u.updateActivePageOnShow();
});
onUnmounted(() => {
    uni.$u.cleanupOnPageUnload();
});
</script>

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