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/promotion/
// ==================================================
router.get('/', adminonly, function(req, res, next) {
    let query = "SELECT * FROM promotion";
    db.all(query, (err, result) => {
        renderAllRecords(res, err, result, "promotion/allrecords");
    });
});

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

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

// ==================================================
// Route to obtain user input and save in database.
// ==================================================
router.post('/', adminonly, function(req, res, next) {
    let insertquery = "INSERT INTO promotion(promotitle, promoimage, description, startdate, enddate, discountrate) VALUES(?, ?, ?, ?, ?, ?)";
    db.run(insertquery, [req.body.promotitle, req.body.promoimage, req.body.description, req.body.startdate, req.body.enddate, req.body.discountrate], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/promotion');
        }
    });
});

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

// ==================================================
// Route to save edited data in database.
// ==================================================
router.post('/save', adminonly, function(req, res, next) {
    let updatequery = "UPDATE promotion SET promotitle = ?, promoimage = ?, description = ?, startdate = ?, enddate = ?, discountrate = ? WHERE promotion_id = ?";
    db.run(updatequery, [req.body.promotitle, req.body.promoimage, req.body.description, req.body.startdate, req.body.enddate, req.body.discountrate, req.body.promotion_id], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/promotion');
        }
    });
});

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

module.exports = router;