const vnReport = require('../../../core/mixins/vn-report.js');
const {DOMImplementation, XMLSerializer} = require('xmldom');
const {models} = require('vn-loopback/server/server');
const jsBarcode = require('jsbarcode');
const moment = require('moment');

module.exports = {
    name: 'buy-label-supplier',
    mixins: [vnReport],
    async serverPrefetch() {
        const buy = await models.Buy.findById(this.id, null);
        this.buys = await this.rawSqlFromDef('buy', [buy.entryFk, buy.entryFk, buy.entryFk, this.id]);
        const date = new Date();
        this.weekNum = moment(date).isoWeek();
        this.dayNum = moment(date).day();
    },
    methods: {
        getBarcode(id) {
            const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
            const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

            jsBarcode(svgNode, id, {
                xmlDocument: document,
                format: 'code128',
                displayValue: false,
                width: 3.8,
                height: 115,
            });
            return new XMLSerializer().serializeToString(svgNode);
        }
    },
    props: {
        id: {
            type: Number,
            required: true,
            description: 'The entry id'
        }
    }
};