<template>
    <view class="container">
        <view class="book-list">
            <book-item v-for="(book, index) in bookList" :key="index" :book="book" @remove="removeBook" />
        </view>

        <!-- 底部信息和按钮 -->
        <view class="footer fixed-bottom flex-d" style="padding-top:10rpx">
            <text class="pad-10" v-if="bookList.length > 0">共 <text style="color: #ff0000;">{{ totalBooks }}</text>
                本，预估回收价 <text style="color: #ff0000;">¥{{ totalPrice }}</text></text>
            <view class="flex-a mt-12">
                <u-button size="large" type="primary" text="提交" @click="handleSubmit" />
                <u-button size="large" type="warning" text="扫码" @click="handleScan" />
            </view>
        </view>
    </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import BookItem from '../components/BookItemOrder.vue';

const bookList = ref([]);

const totalBooks = computed(() => bookList.value.length);

const totalPrice = computed(() => bookList.value.reduce((sum, book) => sum + parseFloat(book.auditPrice), 0).toFixed(2));

const removeBook = ({ book, index }) => {
    bookList.value.splice(index, 1);
};

const handleSubmit = () => {
    if (bookList.value.length === 0) {
        uni.$u.toast('请先添加书籍');
        return;
    }
    uni.$u.http.post('/app/offlineorder/submitOfflineOrder', bookList.value).then(res => {
        if (res.code == 200) {
            uni.$u.toast('提交成功');
            bookList.value.length = 0
        }
    });
};

const handleScan = () => {
    uni.scanCode({
        success: (res) => {
            handleBarcode(res.result);
            console.log('扫码结果:', res.result);
            // 添加新书逻辑
        },
        fail: (res) => {
            console.log('扫码失败:', res);
            uni.$u.toast('扫码失败');
        }
    });
};
//处理扫码数据
const handleBarcode = (barcode) => {
    console.log('扫码结果:', barcode);
    // 添加新书逻辑,根据barcode查询书籍信息
    uni.$u.http.get(`/app/offlineorder/getBookInfo/${barcode}`).then(res => {
        if (res.code == 200) {
            res.data.quantity = 1
            let item = bookList.value.find(item => item.isbn === res.data.isbn)
            if (item) {
                item.quantity += 1
                item.auditPrice = (item.expectPrice * item.quantity).toFixed(2)
                return;
            } else {
                res.data.auditPrice = (res.data.expectPrice * res.data.quantity).toFixed(2)
                bookList.value.push(res.data);
            }
        }
    })
}

onLoad(() => {
    // #ifdef APP-PLUS
    uni.$u.useGlobalEvent((e) => {
        handleBarcode(e.barcode)
    })
    // #endif
})
</script>

<style scoped>
.container {
    display: flex;
    flex-direction: column;
    padding: 12px;
    box-sizing: border-box;
    height: 100%;

    .book-list {
        height: calc(100% - 96px);
        overflow: auto;
    }
}

.footer {
    background-color: #fff;
    border-top: 1px solid #eee;
    font-size: 32rpx;
    color: #999999;
}
</style>