const Component = require(`vn-print/core/component`);
const Report = require(`vn-print/core/report`);
const reportBody = new Component('report-body');
const reportHeader = new Component('report-header');
const reportFooter = new Component('report-footer');
const invoiceIncoterms = new Report('invoice-incoterms');

module.exports = {
    name: 'invoice',
    async serverPrefetch() {
        this.invoice = await this.fetchInvoice(this.reference);
        this.client = await this.fetchClient(this.reference);
        this.taxes = await this.fetchTaxes(this.reference);
        this.intrastat = await this.fetchIntrastat(this.reference);
        this.rectified = await this.fetchRectified(this.reference);
        this.hasIncoterms = await this.fetchHasIncoterms(this.reference);

        const tickets = await this.fetchTickets(this.reference);
        const sales = await this.fetchSales(this.reference);

        const map = new Map();

        for (let ticket of tickets) {
            ticket.sales = [];

            map.set(ticket.id, ticket);
        }

        for (let sale of sales) {
            const ticket = map.get(sale.ticketFk);

            if (ticket) ticket.sales.push(sale);
        }

        this.tickets = tickets;

        if (!this.invoice)
            throw new Error('Something went wrong');
    },
    data() {
        return {totalBalance: 0.00};
    },
    computed: {
        ticketsId() {
            const tickets = this.tickets.map(ticket => ticket.id);

            return tickets.join(', ');
        },
        botanical() {
            let phytosanitary = [];
            for (let ticket of this.tickets) {
                for (let sale of ticket.sales) {
                    if (sale.botanical)
                        phytosanitary.push(sale.botanical);
                }
            }

            return phytosanitary.filter((item, index) =>
                phytosanitary.indexOf(item) == index
            ).join(', ');
        },
        taxTotal() {
            const base = this.sumTotal(this.taxes, 'base');
            const vat = this.sumTotal(this.taxes, 'vat');
            return base + vat;
        }
    },
    methods: {
        fetchInvoice(reference) {
            return this.findOneFromDef('invoice', [reference]);
        },
        fetchClient(reference) {
            return this.findOneFromDef('client', [reference]);
        },
        fetchTickets(reference) {
            return this.rawSqlFromDef('tickets', [reference]);
        },
        fetchSales(reference) {
            return this.rawSqlFromDef('sales', [reference, reference]);
        },
        fetchTaxes(reference) {
            return this.rawSqlFromDef(`taxes`, [reference]);
        },
        fetchIntrastat(reference) {
            return this.rawSqlFromDef(`intrastat`, [reference, reference, reference]);
        },
        fetchRectified(reference) {
            return this.rawSqlFromDef(`rectified`, [reference]);
        },
        fetchHasIncoterms(reference) {
            return this.findValueFromDef(`hasIncoterms`, [reference]);
        },
        saleImport(sale) {
            const price = sale.quantity * sale.price;

            return price * (1 - sale.discount / 100);
        },
        ticketSubtotal(ticket) {
            let subTotal = 0.00;
            for (let sale of ticket.sales)
                subTotal += this.saleImport(sale);

            return subTotal;
        },
        sumTotal(rows, prop) {
            let total = 0.00;
            for (let row of rows)
                total += parseFloat(row[prop]);

            return total;
        }
    },
    components: {
        'report-body': reportBody.build(),
        'report-header': reportHeader.build(),
        'report-footer': reportFooter.build(),
        'invoice-incoterms': invoiceIncoterms.build()
    },
    props: {
        reference: {
            type: String,
            description: 'The invoice ref'
        }
    }
};