import ngModule from '../module';
import Section from 'salix/components/section';

export default class Controller extends Section {
    get checked() {
        const rows = this.$.model.data || [];
        const checkedRows = [];
        for (let row of rows) {
            if (row.checked)
                checkedRows.push(row.id);
        }

        return checkedRows;
    }

    get totalChecked() {
        return this.checked.length;
    }

    preview(invoiceOut) {
        this.selectedInvoiceOut = invoiceOut;
        this.$.summary.show();
    }

    openPdf() {
        if (this.checked.length <= 1) {
            const [invoiceOutId] = this.checked;
            const url = `api/InvoiceOuts/${invoiceOutId}/download?access_token=${this.vnToken.token}`;
            window.open(url, '_blank');
        } else {
            const invoiceOutIds = this.checked;
            const invoicesIds = invoiceOutIds.join(',');
            const serializedParams = this.$httpParamSerializer({
                access_token: this.vnToken.token,
                ids: invoicesIds
            });
            const url = `api/InvoiceOuts/downloadZip?${serializedParams}`;
            window.open(url, '_blank');
        }
    }
}

ngModule.vnComponent('vnInvoiceOutIndex', {
    template: require('./index.html'),
    controller: Controller
});