import * as Esc from "@/utils/EscPostUtils.js";

// const testObj = {
//     mer_name: "莲花农贸市场", //商户名称
//     service_phone: "0830-2669767", //客服电话
//     number: "PF171568087790938356", //单号
//     create_time: "2024-5-15 18:00:32", //下单时间
//     total_price: "29.96", //总价
//     pay_price: "29.96", //实付
//     pay_type: "微信支付", //支付方式
//     pay_number: "4200002159202405159003084211", //支付单号
//     goods: [
//         {
//             name: "白菜 大白菜", //商品名称
//             sell: "236.60", //单价
//             nums: "13", //数量
//             unit_name: "kg", //单位
//             total: "2896.34" //小计
//         },
//         {
//             name: "小黄鸭", //商品名称
//             sell: "9.56", //单价
//             nums: "2", //数量
//             unit_name: "只", //单位
//             total: "19.38" //小计
//         },
//         {
//             name: "卷心菜", //商品名称
//             sell: "15.37", //单价
//             nums: "8", //数量
//             unit_name: "斤", //单位
//             total: "134.38" //小计
//         }
//     ]
// }

const testObj = {
    "id": 119,
    "store_id": 5,
    "staff_id": 0,
    "order_id": "PF1717833856******",
    "pid": null,
    "uid": 0,
    "real_name": "",
    "user_phone": "",
    "user_address": "",
    "cart_id": "79",
    "freight_price": "0.00",
    "total_num": 1,
    "total_price": "0.00",
    "total_postage": "0.00",
    "pay_price": "0.00",
    "cost": "0.00",
    "profit": "0.00",
    "earnings": "0.00",
    "vip_price": "0.00",
    "pay_postage": "0.00",
    "deduction_price": "0.00",
    "coupon_id": 0,
    "coupon_price": "0.00",
    "paid": 1,
    "pay_time": 1717833856,
    "pay_type": "7",
    "pay_type_name": "微信小程序",
    "status": 2,
    "refund_status": 0,
    "refund_type": 0,
    "refund_express": "",
    "refund_reason_wap_img": null,
    "refund_reason_wap_explain": "",
    "refund_reason_time": 0,
    "refund_cancle_time": null,
    "refund_reason_wap": "",
    "refund_reason": "",
    "refund_num": 0,
    "refund_price": "0.00",
    "delivery_name": "",
    "delivery_code": "",
    "delivery_type": "",
    "delivery_id": "",
    "fictitious_content": "",
    "delivery_uid": 0,
    "mark": "",
    "remark": "",
    "mer_id": 0,
    "verify_code": "6-******",
    "shipping_type": 2, // 1快递, 2自提, 3收银
    "reservation": 0,
    "reservation_time": null,
    "is_writeoff": 1,
    "is_remind": 0,
    "create_time": "2024-00-08 16:04:16",
    "update_time": "2024-00-08 16:56:23",
    "delete_time": null,
    "system_store_name": "店铺名称",
    "system_store_phone": "156****6666",
    "staff_name": "新店阿萨",
    "staff_phone": "156****6666",
    "nickname": "",
    "user_mobile": "",
    "info": [
        {
            "store_id": 5,
            "product_id": 30,
            "cart_num": 1,
            "price": null,
            "total_price": null,
            "unit_name": "只",
            "store_name": "商品名称"
        }
    ]
}

export const printTicket = (obj = {}, test = false) => {
    let str = "";
    if (!obj || !obj.order_id) obj = testObj;
    str += Esc.Size2(2) + Esc.Center() + Esc.boldFontOn() + obj.system_store_name + Esc.Size1() + "\n";
    str += Esc.fillLine(" ") + Esc.boldFontOff() + "\n";
    if (obj.shipping_type) {
        if(obj.is_writeoff == 1) str += Esc.Left() + "核销码: " + Esc.boldFontOn() + obj.verify_code + Esc.boldFontOff() + " (已核销)" + "\n";
        else if(obj.reservation == 1) str += Esc.Left() + "核销码: " + Esc.boldFontOn() + obj.verify_code + Esc.boldFontOff() + " (次日提)" + "\n";
        else str += Esc.Left() + "核销码: " + Esc.boldFontOn() + obj.verify_code + Esc.boldFontOff() + "\n";
    }
    str += Esc.Left() + "单号: " + obj.order_id + "\n";
    str += Esc.Left() + "下单时间: " + obj.create_time + "\n";

    // 商品信息
    str += Esc.fillLine("=") + "\n";

    str += Esc.inline3("单价", "数量", "小计", " ", 1) + "\n";
    obj.info.forEach(item => {
        str += Esc.Left() + item.store_name + "\n";
        let total = +item.price || 0;
        total *= +item.cart_num;
        total = total.toFixed(2);
        str += Esc.inline3(`${item.price || '0.00'}元`, `${item.cart_num}${item.unit_name || ''}`, `${item.cart_info?.pay_price || total}元`, " ", 1) + "\n";
    })

    str += Esc.fillLine("=") + "\n";

    if (obj.deduction_price) { // 有优惠
        str += Esc.Left() + "合计: " + obj.total_price + "元" + "\n";
        str += Esc.Left() + "优惠: " + obj.deduction_price + "元" + "\n";
    }
    else str += Esc.Left() + "合计: " + obj.pay_price + "元" + "\n";
    str += Esc.Left() + "实付款: " + obj.pay_price + "元" + "\n";
    str += Esc.Left() + "支付方式: " + obj.pay_type_name + "\n";
    // str += Esc.Left() + "支付单号: " + obj.order_id + "\n";
    str += Esc.Left() + "店铺电话: " + obj.system_store_phone + "\n";

    if (obj.shipping_type == 1) { // 快递
        str += Esc.fillLine("=") + "\n";
        str += Esc.Left() + "收货人: " + obj.real_name + "\n";
        str += Esc.Left() + "收货电话: " + obj.user_phone + "\n";
        str += Esc.Left() + "收货地址: " + obj.user_address + "\n";
    }

    str += Esc.fillLine("=") + "\n";

    // 票尾
    str += Esc.fillLine(" ") + "\n";
    str += Esc.Center() + "欢迎下次光临!" + "\n";
    str += Esc.feedLines(" ") + "\n";
    // 切纸
    str += Esc.cutPaper();
    return str;
} 