const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const employeeRequestService = require('./employeeRequest.service');

router.post('/create', authorize(), create);
router.post('/create-workflow', authorize(), createWorkflow);
router.get('/', authorize(), getAll);
router.get('/show', show);
router.get('/filter', authorize(), filter);
router.get('/filter-approved', authorize(), filterApproved);
router.get("/details/:id", getOne);
router.get("/filter-by-id", authorize(), filterByID);
router.get('/option', authorize(), option);
router.get('/count', authorize(), count);
router.put('/:id', authorize(), update);
router.put('/enabled/:id', authorize(), enabled);
router.delete('/:id', authorize(), _delete);

module.exports = router;

function create(req, res, next) {
    employeeRequestService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

function getAll(req, res, next) {
    employeeRequestService.getAll(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getOne(req, res, next) {
    employeeRequestService
    .getOne(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}

function show(req, res, next) {
    employeeRequestService.show()
        .then(result => res.json(result))
        .catch(next);
}

function filter(req, res, next) {
    employeeRequestService.filter(req.query)
        .then(result => res.json(result))
        .catch(next);
}
function filterApproved(req, res, next) {
    employeeRequestService.filterApproved(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function filterByID(req, res, next) {
    employeeRequestService
    .filterByID(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

function option(req, res, next) {
    employeeRequestService.option()
        .then(result => res.json(result))
        .catch(next);
}

function count(req, res, next) {
    employeeRequestService.count()
        .then(result => res.json(result))
        .catch(next);
}

function update(req, res, next) {
    employeeRequestService.update(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

function createWorkflow(req, res, next) {
    employeeRequestService.createWorkflow(req.body)
        .then(result => res.json(result))
        .catch(next);
}

function enabled(req, res, next) {
    employeeRequestService.enabled(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

function _delete(req, res, next) {
    employeeRequestService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}
