<template>
    <JyPopup ref="popup" showSave @click="savePost" title="填写快递单号">
        <view class="jy-delivery">
            <uni-forms :modelValue="formData">
                <uni-forms-item label="快递公司" name="company">
                    <uni-easyinput :inputBorder="false" type="text" v-model="formData.company" placeholder="输入快递公司" />
                </uni-forms-item>
                <uni-forms-item label="快递单号" name="numbers">
                    <uni-easyinput :inputBorder="false" type="text" suffixIcon="scan" v-model="formData.numbers"
                        placeholder="输入快递单号" @suffix="scan" />
                </uni-forms-item>
            </uni-forms>
        </view>
    </JyPopup>
</template>

<script setup>
import { ref, reactive, defineEmits } from 'vue'
import JyPopup from '@/components/public/jy-popup'
const popup = ref(null)
const emit = defineEmits(['savePost'])
const formData = reactive({
    company: '',
    numbers: ''
})

const scan = async () => {
    try {
        const result = await uni.scanCode();
        if (result && result.result) {
            // 提取快递单号
            const extractedOrderNumber = extractOrderNumber(result.result);
            formData.numbers = extractedOrderNumber;
        } else {
            uni.showToast({
                title: '扫码结果为空',
                icon: 'none'
            });
        }
    } catch (error) {
        uni.showToast({
            title: '扫码失败',
            icon: 'none'
        });
    }
};

// 提取快递单号的函数
const extractOrderNumber = (scanResult) => {
    // 假设扫码结果中的快递单号在字符串的开头
    return scanResult.split('\n')[0];
};
const open = () => {
    popup.value.open()
}
const savePost = () => {
    emit('savePost')
}
defineExpose({
    open
})
</script>

<style scoped lang="scss">
.jy-delivery {
    padding: 20px;
}
</style>