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

export default {
    add(res, data) {
        return request({
            url: `api/${res}/add`,
            method: 'post',
            data
        });
    },
    update(res, data) {
        return request({
            url: `api/${res}/update`,
            method: 'put',
            data
        });
    },
    remove(res, id) {
        return request({
            url: `api/${res}/remove`,
            method: 'delete',
            params: {
                id
            }
        });
    },
    get(res, id) {
        return request({
            url: `api/${res}/get`,
            method: 'get',
            params: {
                id
            }
        });
    },
    query(res, filter, path, method) {
        if (path == null || path == '') {
            path = 'query';
        }

        if (method == null || method == '') {
            method = 'post';
        }

        return request({
            url: `api/${res}/${path}`,
            method: method,
            data: filter
        });
    },
    async exportExcel(tableView, filter) {
        let path = 'query';
        let method = 'post';

        if (tableView.functionName != null && tableView.functionName != '') {
            path = tableView.functionName;
        }

        if (tableView.functionType != null && tableView.functionType != '') {
            method = tableView.functionType;
        }

        let data = {
            ...filter,
            per: 100000,
            returnType: 1,
            viewCode: tableView.code
        };

        const result = await request({
            url: `api/${tableView.model}/${path}`,
            method: method,
            responseType: 'blob',
            data
        });

        const blob = new Blob([result], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        });
        saveAs(blob, tableView.name + '-' + dateFormat(new Date(), 'yyyyMMddHHmmss') + '.xlsx');
    },
    async exportPdf(tableView, filter) {
        let path = 'query';
        let method = 'post';

        if (tableView.functionName != null && tableView.functionName != '') {
            path = tableView.functionName;
        }

        if (tableView.functionType != null && tableView.functionType != '') {
            method = tableView.functionType;
        }

        let data = {
            ...filter,
            per: 100000,
            returnType: 3,
            viewCode: tableView.code
        };

        const result = await request({
            url: `api/${tableView.model}/${path}`,
            method: method,
            responseType: 'blob',
            data
        });

        const blob = new Blob([result], { type: 'application/pdf' });
        saveAs(blob, tableView.name + '-' + dateFormat(new Date(), 'yyyyMMddHHmmss') + '.pdf');
    },
    async printList(tableView, filter, landscape = true) {
        let path = 'query';
        let method = 'post';

        if (tableView.functionName != null && tableView.functionName != '') {
            path = tableView.functionName;
        }

        if (tableView.functionType != null && tableView.functionType != '') {
            method = tableView.functionType;
        }

        let LODOP;
        try {
            LODOP = lodop.getLodop();
        } catch (e) {
            alert('请检查打印驱动！' + e.message);
            return;
        }

        let data = {
            ...filter,
            per: 1000,
            returnType: 2,
            viewCode: tableView.code
        };

        const result = await request({
            url: `api/${tableView.model}/${path}`,
            method: method,
            data
        });

        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();
    }
};
