"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrintController = void 0;
const common_1 = require("@nestjs/common");
const zlib = __importStar(require("zlib"));
let PrintController = class PrintController {
    async printReceipt(body) {
        const now = new Date().toLocaleString("sv-SE").replace("T", " ");
        const formatRupiah = (number) => 'Rp' + Number(number).toLocaleString('id-ID');
        const page1 = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>

@page{
margin:0;
}

body{
margin:0;
font-family: monospace;
}

/* 
Calculate row height
line-height ≈ 1.2 . font-size
16px * 1.2 = 19.2px
Add some padding for better readability
2px top + 2px bottom = 4px
Total row height = 19.2px + 4px = 23.2px
Add or Subtract total row height to the padding
 */

.receipt{
width:480px; /* 60mm @203dpi */
padding:12px 12px 87px 50px;
position:relative;
box-sizing:border-box;
}

h2{
font-size:20px;
margin:0 0 8px 0;
text-align:center;
}

table{
border-collapse:collapse;
font-size:18px;
}

td{
padding:2px 0;
white-space:nowrap;
}

.label{
padding-right:6px;
}

.separator{
padding-right:6px;
}

.value{
text-align:right;
padding-left:10px;
}

.total-row td{
font-weight:bold;
padding-top:6px;
}

.vertical-date{
position:absolute;
left:4px;
top:45px;
writing-mode: vertical-rl;
font-size:18px;
border-right:2px dotted #000;
padding-right:4px;
}

</style>
</head>
<body>

<div class="receipt">

<div class="vertical-date">
${now}
</div>

<h2>${body.storeName}</h2>

<table>

<tr>
<td class="label">Kode Item</td>
<td class="separator">:</td>
<td class="value">${body.kodeBarang}</td>
</tr>

<tr>
<td class="label">Tipe</td>
<td class="separator">:</td>
<td class="value">${body.type}</td>
</tr>

<tr>
<td class="label">Berat Kotor</td>
<td class="separator">:</td>
<td class="value">${body.beratKotor}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Berat Bersih</td>
<td class="separator">:</td>
<td class="value">${body.beratBersih}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Karat</td>
<td class="separator">:</td>
<td class="value">${body.kadarKarat}</td>
</tr>

<tr>
<td class="label">Kadar (%)</td>
<td class="separator">:</td>
<td class="value">${body.kadarPersen}&nbsp;%</td>
</tr>

<tr>
<td class="label">Harga/gr</td>
<td class="separator">:</td>
<td class="value">${formatRupiah(body.hargaPerGram)}</td>
</tr>

<tr class="total-row">
<td class="label">TOTAL</td>
<td class="separator">:</td>
<td class="value">${formatRupiah(body.total)}</td>
</tr>
</table>
</div>
</body>
</html>
`;
        const page2 = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>

@page{
margin:0;
}

body{
margin:0;
font-family: monospace;
}

/* 
Calculate row height
line-height ≈ 1.2 . font-size
16px * 1.2 = 19.2px
Add some padding for better readability
2px top + 2px bottom = 4px
Total row height = 19.2px + 4px = 23.2px
Add or Subtract total row height to the padding
 */

.receipt{
width:480px; /* 60mm @203dpi */
padding:12px 12px 87px 50px;
position:relative;
box-sizing:border-box;
}

h2{
font-size:20px;
margin:0 0 8px 0;
text-align:center;
}

table{
border-collapse:collapse;
font-size:18px;
}

td{
padding:2px 0;
white-space:nowrap;
}

.label{
padding-right:6px;
}

.separator{
padding-right:6px;
}

.value{
text-align:right;
padding-left:10px;
}

.total-row td{
font-weight:bold;
padding-top:6px;
}

.vertical-date{
position:absolute;
left:4px;
top:45px;
writing-mode: vertical-rl;
font-size:18px;
border-right:2px dotted #000;
padding-right:4px;
}

</style>
</head>
<body>

<div class="receipt">

<div class="vertical-date">
${now}
</div>

<h2>${body.storeName}</h2>

<table>

<tr>
<td class="label">Kode Item</td>
<td class="separator">:</td>
<td class="value">${body.kodeBarang}</td>
</tr>

<tr>
<td class="label">Tipe</td>
<td class="separator">:</td>
<td class="value">${body.type}</td>
</tr>

<tr>
<td class="label">Berat Kotor</td>
<td class="separator">:</td>
<td class="value">${body.beratKotor}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Berat Bersih</td>
<td class="separator">:</td>
<td class="value">${body.beratBersih}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Karat</td>
<td class="separator">:</td>
<td class="value">${body.kadarKarat}</td>
</tr>

<tr>
<td class="label">Kadar (%)</td>
<td class="separator">:</td>
<td class="value">${body.kadarPersen}&nbsp;%</td>
</tr>

<tr>
<td class="label">&nbsp;/gr</td>
<td class="separator">&nbsp;</td>
<td class="value">&nbsp;&nbsp;</td>
</tr>

<tr class="total-row">
<td class="label">&nbsp;</td>
<td class="separator">&nbsp;</td>
<td class="value">&nbsp;&nbsp;</td>
</tr>
</table>
</div>
</body>
</html>
`;
        const pages = [page1, page2];
        const json = JSON.stringify(pages);
        const gzipped = zlib.gzipSync(json);
        const base64 = gzipped.toString('base64');
        const intentUrl = 'intent://#Intent;' +
            'scheme=print-intent;' +
            'S.content=' + encodeURIComponent(base64) + ';' +
            'end;';
        return { intentUrl };
    }
};
exports.PrintController = PrintController;
__decorate([
    (0, common_1.Post)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], PrintController.prototype, "printReceipt", null);
exports.PrintController = PrintController = __decorate([
    (0, common_1.Controller)('print')
], PrintController);
//# sourceMappingURL=print.controller.js.map