const express = require('express');
const router = express.Router();
const authorize = require('middleware/authorize-sapps');
const callplanService = require('./callPlan.service');

// routes
router.post('/create', authorize(), create);
router.post('/replace', authorize(), replace);
router.get('/', authorize(), getAll);
router.get('/filter', authorize(), getFilter);
router.get('/territory/:territory', authorize(), getDocumentTerritory);
router.get('/territory-employee', authorize(), getDocumentTerritoryEmployee);
router.get('/document/:document_no', authorize(), getByDocumentNo);
router.get('/:id', authorize(), getOne);
router.get('/name/:id', authorize(), getName);
router.get('/option/list', authorize(), getOption);
router.get('/count', authorize(), getCount);
router.put('/:id', authorize(), update);
router.put('/mass-update/:id', authorize(), massUpdate);
router.delete('/:id', authorize(), _delete);

module.exports = router;

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

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

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

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

function getDocumentTerritory(req, res, next) {
    callplanService.getDocumentTerritory(req.params.territory)
        .then(result => res.json(result))
        .catch(next);
}

function getDocumentTerritoryEmployee(req, res, next) {
    callplanService.getDocumentTerritoryEmployee(req.query)
        .then(result => res.json(result))
        .catch(next);
}
function getByDocumentNo(req, res, next) {
    callplanService.getByDocumentNo(req.params.document_no)
        .then(result => res.json(result))
        .catch(next);
}

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

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

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

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

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

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

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