import BaseController from "../../common/base/base_controller";
const excel = require('node-xlsx');
const fs = require('fs');
const facade = require('../facade/index');
const config = require("../../../config/config.js");

class PriceInfoSummaryController extends BaseController {
    async main(req, res) {
        const renderData = {
            title: '材料信息价总表',
            userAccount: req.session.managerData.username,
            userID: req.session.managerData.userID,
            LicenseKey: config.getLicenseKey(process.env.NODE_ENV),
        };
        res.render("maintain/price_info_summary/html/main.html", renderData);
    }

    // 获取所有数据
    async getData(req, res) {
        try {
            const data = await facade.getData();
            res.json({ error: 0, message: 'getData success', data });
        } catch (err) {
            console.log(err);
        }
    }

    // 获取分页数据
    async getPagingData(req, res) {
        try {
            const { page, pageSize, searchStr } = JSON.parse(req.body.data);
            const data = await facade.getPagingData(page, pageSize, searchStr);
            res.json({ error: 0, message: 'getData success', data });
        } catch (err) {
            console.log(err);
        }
    }

    // 编辑总表
    async editSummaryData(req, res) {
        try {
            const { postData } = JSON.parse(req.body.data);
            await facade.editSummaryData(postData);
            res.json({ error: 0, message: 'editPrice success' });
        } catch (err) {
            console.log(err);
            res.json({ error: 1, message: err.toString() });
        }
    }

    // 保存至总表
    async saveInSummary(req, res) {
        try {
            const { documents } = JSON.parse(req.body.data);
            await facade.saveInSummary(documents);
            res.json({ error: 0, message: 'saveInSummary success' });
        } catch (err) {
            console.log(err);
            res.json({ error: 1, message: err.toString() });
        }
    }

    async aiMatch(req, res) {
        try {
            const { listA, listB } = JSON.parse(req.body.data);
            const data = await facade.aiMatch(listA, listB);
            res.json({ error: 0, data, message: 'aiMatch success' });
        } catch (err) {
            console.log(err);
            res.json({ error: 1, message: err.toString() });
        }
    }

    async exportSummaryData(request, response) {
        try {
            const excelData = await facade.exportExcelData();
            const buffer = excel.build([{ name: "材料信息价总表", data: excelData }]);
            const filePath = './public/exportPriceSummary.xlsx';
            fs.writeFileSync(filePath, buffer, 'binary');
            const stats = fs.statSync(filePath);
            // 下载相关header
            response.set({
                'Content-Type': 'application/octet-stream',
                'Content-Disposition': 'attachment; filename=infoPriceSummary.xlsx',
                'Content-Length': stats.size
            });
            fs.createReadStream(filePath).pipe(response);
            fs.unlink(filePath);
        } catch (error) {
            response.end(error);
        }
    }

}

module.exports = {
    priceInfoSummaryController: new PriceInfoSummaryController()
};