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

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

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

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

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

// ==================================================
// Route to save edited data in database.
// ==================================================
router.post('/save', adminonly, function(req, res, next) {
    let updatequery = "UPDATE orderdetail SET orderdetail_id = ?, product_id = ?, saleprice = ?, qty = ? WHERE orderdetail_id = ?";
    db.run(updatequery, [req.body.orderdetail_id, req.body.product_id, req.body.saleprice, req.body.qty, req.body.orderdetail_id], (err) => {
        if (err) {
            renderError(res, err, 400);
        } else {
            res.redirect('/orderdetail');
        }
    });
});

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

module.exports = router;