import request from '../libs/request';
import lodop from '../libs/lodop';
import dateFormat from 'date-fns/format';
import saveAs from 'file-saver';

const res = 'DataView';

export default {
    getByCode(code) {
        return request({
            url: `api/${res}/getByCode`,
            method: 'get',
            params: {
                code
            }
        });
    },
    save(code, viewColumns) {
        return request({
            url: `api/${res}/saveDataView`,
            method: 'post',
            data: {
                code,
                viewColumns
            }
        });
    },
    saveSetting(data) {
        return request({
            url: `api/${res}/saveSetting`,
            method: 'post',
            data
        });
    },
    saveUserView(code, viewColumns) {
        return request({
            url: `api/${res}/saveUserView`,
            method: 'post',
            data: {
                code,
                viewColumns
            }
        });
    },
    saveFilterColumn(code, filterColumns) {
        return request({
            url: `api/${res}/saveFilterColumn`,
            method: 'post',
            data: {
                code,
                filterColumns
            }
        });
    },
    reset(code) {
        return request({
            url: `api/${res}/removeUserView`,
            method: 'post',
            data: {
                code
            }
        });
    },
    queryColumnByModel(model) {
        return request({
            url: `api/${res}/queryColumnByModel`,
            method: 'get',
            params: {
                model
            }
        });
    },
    queryColumnByDataSource(dataSource) {
        return request({
            url: `api/${res}/queryColumnByDataSource`,
            method: 'get',
            params: {
                dataSource
            }
        });
    },
    getSchemaByModel(model) {
        return request({
            url: `api/${res}/getSchemaByModel`,
            method: 'get',
            params: {
                model
            }
        });
    },
    async exportExcel(tableView, data) {
        const result = await request({
            url: `api/DataView/export`,
            method: 'post',
            responseType: 'blob',
            data: {
                data,
                returnType: 1,
                viewCode: tableView.code
            }
        });

        const blob = new Blob([result], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        });
        saveAs(blob, tableView.name + '-' + dateFormat(new Date(), 'yyyyMMddHHmmss') + '.xlsx');
    },
    async exportPdf(tableView, data) {
        const result = await request({
            url: `api/DataView/export`,
            method: 'post',
            responseType: 'blob',
            data: {
                data,
                returnType: 3,
                viewCode: tableView.code
            }
        });

        const blob = new Blob([result], { type: 'application/pdf' });
        saveAs(blob, tableView.name + '-' + dateFormat(new Date(), 'yyyyMMddHHmmss') + '.pdf');
    },
    async printList(tableView, data, landscape = true) {
        let LODOP;
        try {
            LODOP = lodop.getLodop();
        } catch (e) {
            alert('请检查打印驱动！' + e.message);
            return;
        }

        const result = await request({
            url: `api/DataView/export`,
            method: 'post',
            data: {
                data,
                returnType: 2,
                viewCode: tableView.code
            }
        });

        LODOP.PRINT_INIT('Print');

        if (landscape) {
            LODOP.ADD_PRINT_TABLE('4%', '3%', '94%', '90%', result);
            LODOP.SET_PRINT_PAGESIZE(2, 0, 0, 'A4');
        } else {
            LODOP.ADD_PRINT_TABLE('4%', '3%', '94%', '90%', result);
            LODOP.SET_PRINT_PAGESIZE(1, 0, 0, 'A4');
        }
        LODOP.PRINTA();
    }
};
