const express = require('express');
var invTempRouter = express.Router();
const models = require('../models/index.js');
const chkLogin = require('../middleware/auth.js');
const aclCheck = require('../middleware/aclCheck.js');
const requireUserGroup = require('../middleware/requireUserGroup.js');
const validateListQuery = require('../middleware/validateListQuery.js');
var resp = require('../response.js');

const validateEntityScopedListQuery = validateListQuery({ requireEntId: true });

invTempRouter.use(chkLogin, requireUserGroup('User'));

invTempRouter.post('/createinvtemp', chkLogin, aclCheck("create", "Invoice Template"), async (req, res) => {
    new models.InvoiceTemplate().createInvTemp(req.body, req.session.userId).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error adding Invoice Template data",
                    result.errors,
                    req,
                    false
                )
            )
        }   
        return res.json(
            resp(
                "Invoice Template created",
                "Invoice Template added sucessfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error adding Invoice Template data",
                err,
                req
            )
        )
    })
});

invTempRouter.post('/gettaxnames', chkLogin, aclCheck("read", "Tax"), async (req, res) => {
    new models.Tax().getTax(req.body.ent_id).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error returning Tax Names",
                    result.errors,
                    req,
                    false
                )
            )
        }
        return res.json(
            resp(
                "Tax Names",
                "List of Tax Id and Names",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error returning Tax Names",
                err,
                req
            )
        )
    })
});

invTempRouter.post('/getinvtempnames', chkLogin, aclCheck("read", "Invoice Template"), async (req, res) => {
    new models.InvoiceTemplate().getTempName(req.body.ent_id).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error returning Invoice Template Names",
                    result.errors,
                    req,
                    false
                )
            )
        }
        return res.json(
            resp(
                "Invoice Template Names",
                "List of Invoice Template Id and Names",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error returning Invoice Template Names",
                err,
                req
            )
        )
    })
});

invTempRouter.post('/readinvtemps', chkLogin, aclCheck("read", "Invoice Template"), validateEntityScopedListQuery, async (req, res) => {
    new models.InvoiceTemplate().readInvTemp(req.body.col, req.body.dir, req.body.offset, req.body.limit, req.body.ent_id, req.body.search).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error listing Templates",
                    result.errors,
                    req,
                    false
                )
            )
        }
        new models.InvoiceTemplate().countInvTemp(req.body.ent_id).then((count) => {
            return res.json(
                resp(
                    "Template Data",
                    "List of Templates",
                    {rows: result, rowCount: count},
                    req
                )
            )
        })
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Tax",
                err,
                req
            )
        )
    })
});

invTempRouter.post('/readinvtemprow', chkLogin, aclCheck("read", "Tax"), async (req, res) => {
    new models.InvoiceTemplate().readInvTempRow(req.body).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    `Error finding Invoice Temaplate with ID ${req.body.id}`,
                    err,
                    req,
                    false
                )
            )   
        }
        return res.json(
            resp(
                "Invoice Template Row Data",
                `Invoice Template with ID ${req.body.id}`,
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                `Error finding Invoice Template with ID ${req.body.id}`,
                err,
                req
            )
        )
    })
});

invTempRouter.put('/updateinvtemp', chkLogin, aclCheck("update", "Invoice Template"), async (req, res) => {
    new models.InvoiceTemplate().updateInvTemp(req.body).then((result) => {
        return res.json(
            resp(
                "Invoice Template updated",
                "Invoice Template updated successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error updating Invoice Template data",
                err,
                req
            )
        )
    })
});

invTempRouter.delete('/deleteinvtemp', chkLogin, aclCheck("delete", "Tax"), async (req, res) => {
    new models.Tax().deleteTax(req.body).then((result) => {
        return res.json(
            resp(
                "Tax Deleted",
                "Tax deleted successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error deleting Tax data",
                err,
                req
            )
        )
    })
});

module.exports = invTempRouter;