var express = require('express');
var router  = express.Router();

/* GET home page */
router.get('/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  db.getItemList((items)=>{
    res.render('index', { title: 'Dein.Equipment', items: items })
  })
});

router.get('/article/show/:identifier', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')

  const identifier = req.params.identifier

  db.getItem(identifier, (item)=>{
    db.getNotAvailableList(item.id, (notAvailableList)=>{
      item.unavailableDates = notAvailableList
      res.render('detail', { title: item.title, item: item })
    })  
  })
});

/* GET imprint */
router.get('/imprint/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  res.render('imprint', { title: 'Dein.Equipment Impressum' })
});

/* GET privacy */
router.get('/privacy/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  res.render('privacy', { title: 'Dein.Equipment Datenschutz' })
});

/* GET terms and conditions */
router.get('/terms/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  res.render('terms', { title: 'Dein.Equipment AGB' })
});

router.post('/request/', function(req, res, next) {
  var app = req.app
  var mailer = app.get('MAILER')
  
  const paramStart = req.body.start
  const paramEnd = req.body.end
  const paramName = req.body.name
  const paramContact = req.body.contact
  
  mailer.mail({
    name: paramName,
    contact: paramContact,
    start: paramStart,
    end: paramEnd
  })
  
  res.render('request', { title: 'Dein.Equipment: Anfrage gesendet' })
})

module.exports = router;
