var express = require('express');
var router = express.Router();
var Page = require('../lib/page.controller');

/* GET home page. */
router.get('/', function(req, res, next) {

  Page.getPageByUrl('/', function(result){
    if (result == 0){
      res.render('error',{
        message: 'Page not found',
        error: {} 
      });
    }else{
      res.render('contentPage', {Page: result});
    }
  });
});


/* GET dynamic page. */
router.get('/:page', function(req, res, next) {
  var page = req.params.page;
  
  if(page == 'manager'){
    next();
  }else{
    
    Page.getPageByUrl('/'+page, function(result){
      if (result == 0){
        res.render('error',{
          message: '404 Page not found',
          error: {} 
        });
      }else{
        res.render(result.plantilla, {Page: result});
      }
    });
  }
});



module.exports = router;
