const config = require(`vn-print/core/config`);
const vnReport = require('../../../core/mixins/vn-report.js');
const md5 = require('md5');
const fs = require('fs-extra');

module.exports = {
    name: 'delivery-note',
    mixins: [vnReport],
    async serverPrefetch() {
        this.ticket = await this.findOneFromDef('ticket', [this.id]);
        this.checkMainEntity(this.ticket);
        this.client = await this.findOneFromDef('client', [this.id]);
        this.sales = await this.rawSqlFromDef('sales', [this.id]);
        this.address = await this.findOneFromDef(`address`, [this.id]);
        this.services = await this.rawSqlFromDef('services', [this.id]);
        this.taxes = await this.findOneFromDef('taxes', [this.id]);
        this.packagings = await this.rawSqlFromDef('packagings', [this.id]);
        this.signature = await this.findOneFromDef('signature', [this.id]);
    },
    data() {
        return {totalBalance: 0.00};
    },
    computed: {
        dmsPath() {
            if (!this.signature) return;

            const hash = md5(this.signature.id.toString()).substring(0, 3);
            const file = `${config.storage.root}/${hash}/${this.signature.id}.png`;

            if (!fs.existsSync(file)) return null;

            const src = fs.readFileSync(file);
            const base64 = Buffer.from(src, 'utf8').toString('base64');

            return `data:image/png;base64, ${base64}`;
        },
        deliverNoteType() {
            return this.type ? this.type : 'deliveryNote';
        },
        serviceTotal() {
            let total = 0.00;
            this.services.forEach(service => {
                total += parseFloat(service.price) * service.quantity;
            });

            return total;
        },
        showPrices() {
            return this.deliverNoteType != 'withoutPrices';
        },
        footerType() {
            const translatedType = this.$t(this.deliverNoteType);
            return `${translatedType} ${this.id}`;
        },
        hasObservations() {
            return this.ticket.description !== null;
        }
    },
    methods: {
        getSubTotal() {
            let subTotal = 0.00;
            this.sales.forEach(sale => {
                subTotal += sale.quantity * sale.price * (1 - sale.discount / 100);
            });

            return subTotal;
        },
        getTotalBase() {
            let totalBase = 0.00;
            this.taxes.forEach(tax => {
                totalBase += parseFloat(tax.Base);
            });

            return totalBase;
        },
        getTotalTax() {
            let totalTax = 0.00;
            this.taxes.forEach(tax => {
                totalTax += parseFloat(tax.tax);
            });

            return totalTax;
        },
        getTotal() {
            return this.getTotalBase() + this.getTotalTax();
        },
        getBotanical() {
            let phytosanitary = [];
            this.sales.forEach(sale => {
                if (sale.botanical)
                    phytosanitary.push(sale.botanical);
            });

            return phytosanitary.filter((item, index) =>
                phytosanitary.indexOf(item) == index
            ).join(', ');
        }
    },
    props: {
        id: {
            type: Number,
            required: true,
            description: 'The ticket id'
        },
        type: {
            type: String,
            required: false
        }
    }
};