'use strict';
var bodyParser = require('body-parser')

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

const pool = require("../psql/db_setup.js");
const q = require("../psql/queries"); //import queries

var visit = {};

function _getCurrentDate(){
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1; //January is 0!
  var yyyy = today.getFullYear();

  if(dd<10) {
      dd = '0'+dd
  } 

  if(mm<10) {
      mm = '0'+mm
  } 

  today = yyyy + '-' + mm + '-' + dd;

  return today;
}

visit.getForm = function(req, res){
  let place = "";
  
  if(req.query.place != null){
    console.log(req.query.place);
    place = req.query.place;
  }
  if(req.query.date != null){
    console.log(req.query.date);
    let date = req.query.date;
  }  
  
  function _render(err, result){
    if(err) {
      console.error(err);
      res.send("ERROR" + err);
    } else {
      let currentDate = _getCurrentDate();
      let placeList = result.rows;
      let isVis = "not-visible";
      let errMsg = "";

      console.log(currentDate);

      res.render("add-visit", {
       placeList: placeList,
       place:place,
       currentDate:currentDate,
       isVis:isVis,
       errMsg:errMsg
     });
    }
  }
  console.log(q.getNames);
  pool.query(q.getNames, _render);
}

// This function works but is not robust.
// Need to figure out how to handle username
visit.post = function(req, res) {

  console.log("body", req.body);

  let name = req.body.name;
  let place = req.body.place;
  let date = req.body.date;
  const query = {
    text: q.insertVisit, //VALUES example: (travis, Nay Aug Park, 2011-2-3 )
    values: [name, place, date]
  }
  pool.query(query)
    .then(req => {
      let successMsg = name + ", thanks for adding your visit to " + place +
      " on " + date + "!";
      console.log(successMsg);
      res.status(200).send({msg: successMsg});
    })
    .catch(error =>  {

      let isVis = "visible";
      console.error(error.code)
      let errMsg = "Something went wrong, sorry.";
      if(error.code === "23505") {

        errMsg = "Sorry, " + name + ". But you've already visited "
        + place + " on " + date + ".";

        console.log("Duplicate Error");
        res.status(400).send({msg: errMsg});
      }
    })
}

module.exports = visit;


@@ -2170,16 +2170,40 @@
 orry.%22;%0A
+      //Duplicate Visit%0A
       if
@@ -2431,16 +2431,653 @@
       %7D%0A
+      //Name does not exist%0A      else if (error.code === %2223502%22) %7B%0A        errMsg = %22Sorry, but, %22 + name + %22 is not a current user%22 +%0A        %22 please check that your name is entered correctly.%22;%0A%0A        console.log(%22Name Error%22);%0A        res.status(400).send(%7Bmsg: errMsg%7D);%0A      %7D %0A      //Place does not exist%0A      else if (error.code === %2223505%22 ) %7B%0A        errMsg = %22Sorry, %22 + name + %22 but %22 + place +%0A        %22 does not exist. Please make sure that place is entered correctly.%22;%0A         res.status(400).send(%7Bmsg: errMsg%7D);%0A      %7D%0A      //All other errors%0A      else %7B%0A        res.status(400).send(%7Bmsg: errMsg%7D);%0A      %7D%0A
     %7D)%0A%7D

Add other errors