<template>
    <view class="container">
        <!-- <view class="container">
            <button @click="initBluetooth" :disabled="isInitializing">初始化蓝牙</button>
            <button @click="startDiscovery" :disabled="isDiscovering || !isBluetoothOpened">开始搜索设备</button>
            <button @click="connectDevice" :disabled="!selectedDevice || isConnecting">连接设备</button>
            <button @click="startDataMonitor" :disabled="!isConnected">开始监听数据</button>
            <view v-if="scanData">扫描结果: {{ scanData }}</view>
        </view> -->

        <!-- <u-button type="primary" @click="goScan" text="扫码" /> -->

        <view class="main-content">
            <view class="input-group">
                <u-input placeholder-style="font-size:32rpx" v-model="unpackCode" placeholder="扫描/输入物流单号"
                    border="surround" />
                <u-button type="primary" @click="confirmTrackingNumber(unpackCode)" text="确定"
                    v-permission="'app:express:quickUnpack:confirm'" />
            </view>
        </view>

        <view class="fixed-bottom">
            <u-button size="large" type="success" @click="scanCode" text="扫码" class="scan-button" />
        </view>

    </view>
</template>

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

//跳转至扫码页面
function goScan() {
    uni.navigateTo({
        url: '/pages/index/express/scan'
    })
}

const unpackCode = ref('');

function confirmTrackingNumber(code) {
    uni.$u.http.post('/app/ordersign/unpackingSign', {
        packageCode: code
    }).then(res => {
        if (res.code == 200) {
            let text = code + '拆包成功'
            uni.$u.ttsModule.speak(text)
        } else {
            let text = code + res.msg
            uni.$u.ttsModule.speak(text)
        }
    })
}

function scanCode() {
    uni.scanCode({
        success: (res) => {
            unpackCode.value = res.result;
            confirmTrackingNumber(res.result)
            // 处理扫码结果
            console.log('扫码结果:', res.result);
        },
        fail: (err) => {
            console.error('扫码失败:', err);
        }
    });
}

import keymap from './components/keymap.js'
const inputCache = ref('');
const keypress = (e) => {
    if (e.keyCode === 66 || e.key =='Enter') {
        unpackCode.value = inputCache.value;
        confirmTrackingNumber(unpackCode.value)
        inputCache.value = '';
    } else {
        inputCache.value += keymap[e.keyCode] || ''
    }
}

onLoad(() => {
    // #ifdef APP-PLUS
    uni.$u.useGlobalEvent((e) => {
        unpackCode.value = e.barcode;
        confirmTrackingNumber(e.barcode);
    });
    // #endif

    // Initialize Bluetooth
    // #ifdef APP-PLUS
    plus.key.addEventListener("keyup", keypress);
    // #endif 
    // #ifdef H5
    document.addEventListener("keyup", keypress);
    // #endif
});
onUnmounted(()=>{
    // #ifdef APP-PLUS
    plus.key.removeEventListener("keyup", keypress);
    // #endif
    // #ifdef H5
    document.removeEventListener("keyup", keypress);
    // #endif
})
onShow(() => {
    uni.$u.updateActivePageOnShow()
})
onUnmounted(() => {
    uni.$u.cleanupOnPageUnload();
});
</script>

<style lang="scss" scoped>
.main-content {
    padding: 20px;
    gap: 20px;
}

.input-group {
    display: flex;
    gap: 10px;
    margin-bottom: 30rpx;

    :deep(.u-button) {
        width: 160rpx
    }

    .u-input {
        background-color: #fff;
    }
}
</style>