<template>
    <view class="verification-container">
        <view class="input-container">
            <input class="verification-input" type="text" v-model="verificationCode" placeholder="请输入核销码" />
        </view>

        <button class="verify-btn" @click="verifyOrder">立即核销</button>

        <view class="scan-container" @click="handleScan">
            <view class="scan-icon">
                <image class="barcode-icon"
                    src="https://static.hshuishang.com/gp_cancelAfterVerification.png"
                    mode="aspectFit"></image>
            </view>
            <text class="scan-text">扫码核销</text>
        </view>

        <view>
            <u-toast ref="uToast"></u-toast>
        </view>
    </view>
</template>

<script>
import { request, picUrl, NavgateTo } from '../../../utils';
import { apiArr } from '@/api/groupPurchase.js'

export default {
    data() {
        return {
            verificationCode: '',
        };
    },
    methods: {
        async verifyOrder() {
            if (this.verificationCode.trim()) {
                const params = {
                    verification_code: this.verificationCode,
                }
                try {
                    const res = await request(apiArr.groupBuyWriteOff, 'POST', params, { showErrorToast: false })
                    console.log("🚀 ~ verifyOrder ~ res:", res)
                    this.$refs.uToast.show({
                        title: '核销成功',
                        type: 'success',
                        message: "订单核销成功",
                        duration: 2000
                    })
                } catch (error) {
                    this.$refs.uToast.show({
                        title: '核销失败',
                        type: 'error',
                        message: "订单核销失败",
                        duration: 2000
                    })
                }
            } else {
                this.$u.toast('请输入核销码');
            }
        },
        // 新增扫码核销处理方法
        handleScan() {
            uni.scanCode({
                success: (res) => {
                    this.$refs.uToast.show({
                        title: '扫码成功',
                        type: 'success',
                        // icon: false,
                        message: "扫码成功",
                        duration: 2000
                    })
                    console.log('扫码成功，识别的码：', res.result);
                    this.verificationCode = res.result;
                },
                fail: (err) => {
                    this.$refs.uToast.show({
                        title: '扫码失败',
                        type: 'error',
                        message: "扫码失败 请重试",
                        duration: 2000
                    })
                    console.error('扫码失败:', err);
                }
            });
        }
    }
};
</script>

<style>
@import url('./index.css');
</style>
