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

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

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

// ==================================================
// Route to obtain user input and save in database.
// ==================================================
router.post('/', adminonly, function(req, res, next) {
    var homepage_value = 0;
    if (req.body.homepage) {
        homepage_value = 1;
    }

    let insertquery = "INSERT INTO product(productname, prodimage, description, saleprice, stock, homepage) VALUES(?, ?, ?, ?, ?, ?)";
    db.run(insertquery, [req.body.productname, req.body.prodimage, req.body.description, req.body.saleprice, req.body.stock, homepage_value], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/product');
        }
    });
});

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

// ==================================================
// Route to save edited data in database.
// ==================================================
router.post('/save', adminonly, function(req, res, next) {
    var homepage_value = 0;
    if (req.body.homepage) {
        homepage_value = 1;
    }

    let updatequery = "UPDATE product SET productname = ?, prodimage = ?, description = ?, saleprice = ?, stock = ?, homepage = ? WHERE product_id = ?";
    db.run(updatequery, [req.body.productname, req.body.prodimage, req.body.description, req.body.saleprice, req.body.stock, homepage_value, req.body.product_id], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/product');
        }
    });
});

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

module.exports = router;