<template>
    <view class="container">
        <view class="header">
            <view class="flex-d">
                <text>仓库: {{ detail.godownName }}</text>
                <text class="mt-16">库位编号: {{ detail.positionCode }}</text>
                <text class="mt-16">订单数量: {{ originList.length }}</text>
            </view>
        </view>

        <view class="product-details">
            <LocationOrderItem v-for="(item, index) in orderList" :key="index" :item="item" />
        </view>

        <!-- 底部扫码输入框 -->
        <view class="fixed-bottom pad-20" style="background: #ffffff;">
            <u-search placeholder="请输入快递单号/订单编号" :searchIconSize="24" bgColor="#f6f7f6"
                @search="onSearch" v-model="orderCode" :clearabled="true" :focus="false"
                :showAction="false" :height="42"></u-search>

            <u-icon name="scan" size="36" color="#19be6b" @click="openScan"></u-icon>
        </view>
    </view>
</template>

<script setup>
import {
    reactive,
    ref,
    onUnmounted
} from 'vue'
import {
    onLoad,
    onShow
} from '@dcloudio/uni-app'
import LocationOrderItem from './components/LocationOrderItem.vue'

const orderCode = ref('')
const originList = ref([])
const orderList = ref([])

//根据库位编号查询订单
const detail = ref({})
const getOrderListByPositionCode = (positionCode) => {
    uni.$u.http.get('/app/stock/getOrderByPositionCode?positionCode=' + positionCode).then(res => {
        if (res.code == 200) {
            detail.value = res.data
            orderList.value = res.data.godownStockLogResults
            originList.value = res.data.godownStockLogResults
            if (originList.value.length > 0) {
                uni.$u.ttsModule.speak('查询到' + originList.value.length + '条订单')
            } else {
                uni.$u.toast('暂无订单')
                uni.$u.ttsModule.speak('暂无订单')
            }
        } else {
            uni.$u.toast(res.msg)
            uni.$u.ttsModule.speak(res.msg)
        }
    })
}

const onSearch = () => {
    if (!orderCode.value) {
        orderList.value = originList.value
        uni.$u.toast('请输入订单号或物流单号')
        return
    }

    // 判断是否为纯数字字符串（订单号）
    const searchValue = String(orderCode.value).trim()
    const isNumeric = /^[0-9]+$/.test(searchValue)

    // 在orderList中搜索
    const result = originList.value.filter(item => {
        if (isNumeric) {
            // 如果是纯数字，按orderId搜索
            return item.orderId === orderCode.value
        } else {
            // 非纯数字，按物流单号搜索
            return item.waybillCode === orderCode.value
        }
    })

    orderList.value = result
}

function openScan() {
    uni.scanCode({
        success: (res) => {
            orderCode.value = res.result
            onSearch()
        }
    })
}

// #ifdef APP-PLUS
const { unregister } = uni.$u.useEventListener((e) => {
    if (e.barcode) {
        orderCode.value = e.barcode
        onSearch()
    }
});
// #endif

onLoad((options) => {
    if (options.positionCode) {
        getOrderListByPositionCode(options.positionCode)
    }
});
onUnmounted(() => {
    // #ifdef APP-PLUS
    unregister();
    // #endif
});
</script>

<style lang="scss" scoped>
.header {
    display: flex;
    justify-content: space-between;
    padding: 20rpx 30rpx;
    background-color: #ffffff;
}

.status {
    color: green;
}


.log-item {
    display: flex;
    justify-content: space-between;
    padding: 20rpx 30rpx;
    border-bottom: 1px solid #e0e0e0;
}
</style>