var express = require('express');
var router = express.Router();
var { adminonly, renderAllRecords, renderOneRecord } = require('../util');

// ==================================================
// Route to list all records. Display view to list all records
// URL: http://localhost:3039/saleorder/
// ==================================================
router.get('/', adminonly, function(req, res, next) {
    let query = "SELECT * FROM saleorder";
    db.all(query, (err, result) => {
        renderAllRecords(res, err, result, "saleorder/allrecords");
    });
});

// ==================================================
// Route to view one specific record. Notice the view is one record
// URL: http://localhost:3039/saleorder/1/show
// ==================================================
router.get('/:recordid/show', adminonly, function(req, res, next) {
    let query = "SELECT * FROM saleorder WHERE order_id = ?";
    db.all(query, [req.params.recordid], (err, result) => {
        renderOneRecord(res, err, result, "saleorder/onerec");
    });
});

// ==================================================
// Route to show empty form to obtain input form end-user.
// URLL http://localhost:3039/saleorder/addrecord
// ==================================================
router.get('/addrecord', adminonly, function(req, res, next) {
    res.render('saleorder/addrec');
});

// ==================================================
// Route to obtain user input and save in database.
// ==================================================
router.post('/', adminonly, function(req, res, next) {
    let insertquery = "INSERT INTO saleorder(customer_id, saledate, customernotes, paymentstatus) VALUES(?, ?, ?, ?)";
    db.run(insertquery, [req.body.customer_id, req.body.saledate, req.body.customernotes, req.body.paymentstatus], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/saleorder');
        }
    });
});

// ==================================================
// Route to edit one specific record.
// URL: http://localhost:3039/saleorder/1/edit
// ==================================================
router.get('/:recordid/edit', adminonly, function(req, res, next) {
    let query = "SELECT * FROM saleorder WHERE order_id = ?";
    db.all(query, [req.params.recordid], (err, result) => {
        renderOneRecord(res, err, result, "saleorder/editrec");
    });
})

// ==================================================
// Route to save edited data in database.
// ==================================================
router.post('/save', adminonly, function(req, res, next) {
    let updatequery = "UPDATE saleorder SET customer_id = ?, saledate = ?, customernotes = ?, paymentstatus = ? WHERE order_id = ?";
    db.run(updatequery, [req.body.customer_id, req.body.saledate, req.body.customernotes, req.body.paymentstatus, req.body.order_id], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/saleorder');
        }
    });
});

// ==================================================
// Route to delete one specific record.
// URL: http://localhost:3039/saleorder/1/delete
// ==================================================
router.get('/:recordid/delete', adminonly, function(req, res, next) {
    let query = "DELETE FROM saleorder WHERE order_id = ?";
    db.run(query, [req.params.recordid], (err) => {
        if (err) {
            renderError(res, err);
        } else {
            res.redirect('/saleorder');
        }
    });
});

module.exports = router;