const { splitSite } = require("../middlewares");
const db = require("../models");
const EventRequest = db.event_request;

// Create a new Event Request Item
exports.createEventRequest = (req, res) => {
  // Validate request
  if (!req.body.customer_id) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
	// Create an eventRequest Item
  const eventRequest = new EventRequest({
    customer_id: req.body.customer_id,
		customer_display: req.body.customer_display,
    upload: req.body.upload,
    resource: req.body.resource,
    resource_display: req.body.resource_display,
    source: req.body.source,
    type: req.body.type,
    status: req.body.status || 'active',
    symptom: req.body.symptom,
    transportation: req.body.transportation,
    np: req.body.np,
    notes: req.body.notes || [],
		create_by: req.body.create_by,
		create_date: req.body.create_date,
    edit_history: req.body.edit_history,
    site
  });
  // Save eventRequest Item in the database
  eventRequest
    .save(eventRequest)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the event request Record."
      });
    });
};

// Retrive all eventRequest Records from database.
exports.getAllEventRequests = (req, res) => {
	var params = req.query;
	var condition = {};
  
	condition = splitSite.splitSiteGet(req, condition);
	EventRequest.find(condition)
		.then(data => {
			res.send(data);
		})
		.catch(err => {
			res.status(500).send({
				message:
					err.message || "Some error occurred while retrieving Event Requests."
			});
		});
};


// Update an Event Request Item by the id in the request
exports.updateRequestItem = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  EventRequest.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Event Request with id=${id}. Maybe Event Request was not found!`
        });
      } else res.send({ success: true, message: "Event Request was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Event Request with id=" + id
      });
    });
};

// Delete an Event Request by id
exports.deleteEventRequest = (req, res) => {
  const id = req.params.id;
  EventRequest.findByIdAndRemove(id)
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot delete Event Request with id=${id}. Maybe Event Request was not found!`
        });
      } else {
        res.send({
          message: "Event Request was deleted successfully!"
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Could not delete Event Request with id=" + id
      });
    });
};