<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 { 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'

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 authCode = ref('')

function printReceipt() {
    let data = {
        printerIp: '192.168.1.53',
        orderData: {
            orderNo: '20260327008',
            shopName: '川味小馆',
            createTime: new Date().toLocaleString(),
            products: [
                { name: '回锅肉', price: '32.00', num: 1 },
                { name: '米饭', price: '2.00', num: 2 },
                { name: '紫菜蛋花汤', price: '8.00', num: 1 }
            ],
            total: '42.00',
            payType: '支付宝',
            remark: '微辣'
        }
    }
    ipcRenderer.send('portPrint', JSON.stringify(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>