<template>
    <!-- <el-dialog v-model="showDialog" title="发现新版本" width="500" :close-on-click-modal="false"
        :close-on-press-escape="false" :show-close="false">
        <div class="message">
            {{ updataInfo.message }}
        </div>
        <div class="progress_wrap" style="padding-top: 20px;">
            <el-progress :percentage="uploadPro" :stroke-width="15" striped :striped-flow="uploadPro < 100" />
        </div>
        <template #footer>
            <div class="footer" style="padding: 0 20px 20px;">
                <el-button v-if="!updataInfo.isUp">下次更新</el-button>
                <el-button type="primary" :loading="isUpload" @click="uplaodHandle">
                    <template v-if="!uploadSucess">
                        <template v-if="!isUpload">
                            立即更新
                        </template>
<template v-else>
                            下载中...
                        </template>
</template>
<template v-else>
                        立即安装
                    </template>
</el-button>
</div>
</template>
</el-dialog> -->
    <!-- <el-input v-model="authCode" placeholder="请扫描支付码"></el-input>
    <el-button type="primary" @click="microPayAjax">反扫支付</el-button> -->
    <el-button type="primary" @click="printReceipt">打印盘点单</el-button>
</template>

<script setup>
import dayjs from 'dayjs';
import { onMounted, ref } from 'vue'
import { findVersion } from '@/api/user.js'
import packageData from "../../../package.json";
import { ipcRenderer } from 'electron'
import { microPay } from '@/api/order.js'
import { usePrint } from '@/store/print';
import { printData } from '@/api/print.js'
import { useUser } from '@/store/user.js'
import { ALL_KITCHEN, ONLY_KITCHEN, REFUND_KITCHEN } from '../../../electron/printService'

const showDialog = ref(false)
const updataInfo = ref({})
const isUpload = ref(false)
const uploadPro = ref(0)
const uploadSucess = ref(false)
const uploadResponse = ref({})
const tempFilePath = ref('')
const printStore = usePrint()
const authCode = ref('')
const user = useUser()

// RECHARGE=22 STOCK=30 STOCK_CHECK=32 CALL=38
async function printReceipt() {
    const order = {
        shop_name: '张三的店',
        loginAccount: '',
        isBefore: true,
        carts: [
            {
                id: 1,
                name: "菜品名称1",
                skuName: "多放香菜",
                salePrice: "1.0",
                number: "10",
                totalAmount: "10",
                remark: '多放点盐',
                proGroupInfo: [
                    {
                        proName: '西红柿',
                        number: '2'
                    }
                ]
            },
            // {
            //     id: 2,
            //     name: "菜品名称2",
            //     skuName: "500ml",
            //     salePrice: "1.0",
            //     number: "10",
            //     totalAmount: "10",
            //     remark: ''
            // },
            // {
            //     id: 3,
            //     name: "菜品名称3",
            //     skuName: "",
            //     salePrice: "1.0",
            //     number: "10",
            //     totalAmount: "10",
            //     remark: ''
            // },
        ],
        amount: "10.00",
        originAmount: '10.00',
        discountAmount: "0.00",
        discountAllAmount: 0,
        orderAmount: 10,
        discount: 0,
        remark: "给我多放点辣椒，谢谢老板",
        orderInfo: {
            masterId: "",
            orderNo: "202404021023542223445",
            orderNum: '12',
            discountAllAmount: 0,
            orderAmount: 10,
            tableName: '室内-A1',
            dineMode: 'dine-in'
        },
        loginAccount: '李四',
        printerName: '堂食打印机',
        deviceName: "",
        createdAt: "2024-04-02 10:15",
        printTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
    }
    // ALL_KITCHEN('192.168.1.53', order)

    // ONLY_KITCHEN('192.168.1.53', order)

    REFUND_KITCHEN('192.168.1.53', order)

    // const res = await printData({
    //     shopId: user.shopInfo.id,
    //     dataId: 38,
    //     type: 'CALL'
    // })

    // console.log(JSON.parse(res.data));

    // printStore.CALL(JSON.parse(res.data))
}

// 检查版本更新
async function findVersionAjax() {
    try {
        const res = await findVersion()
        let reg = /\./g;
        if (res.version.replace(reg, '') > packageData.version.replace(reg, '') && res.url) {
            showDialog.value = true
            updataInfo.value = res
        }
    } catch (error) {
        console.log(error);
    }
}

// 下载新版本
async function uplaodHandle() {
    try {
        if (!uploadSucess.value) {
            isUpload.value = true
            ipcRenderer.send('downloadFile', JSON.stringify({ url: updataInfo.value.url }))
            // await downloadFile(updataInfo.value.url)
            // isUpload.value = false
            // uploadSucess.value = true
        } else {
            // 安装文件
        }
    } catch (error) {
        console.log(error);
    }
}


// 反扫支付
async function microPayAjax() {
    try {
        const res = await microPay({
            authCode: authCode.value,
            shopId: 1,
            orderId: 1,
            buyerRemark: ''
        })
        console.log('res===', res);
    } catch (error) {
        console.log(error);
    }
}

onMounted(() => {
    // findVersionAjax()
    ipcRenderer.on('updateProgress', (event, res) => {
        // console.log('updateProgress===', event, res);
        uploadPro.value = res
    })
})
</script>