const db = require("../models");
const Client = db.client;
const Op = db.Sequelize.Op;

// Create and Save a new Tutorial
exports.create = (req, res) => {
  console.log(req);
  const client = {
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    middle_name: req.body.middle_name,
    company_name: req.body.company_name,
    address: req.body.address,
    phone_number: req.body.phone_number,
    email_address: req.body.email_address,
    occupation: req.body.occupation,
    date_of_verification: req.body.date_of_verification,
    type_of_document: req.body.type_of_document,
    document_number: req.body.document_number,
    expire_date: req.body.expire_date,
    in_person: req.body.in_person,
    file_store_path: req.body.file_store_path,
    notes: req.body.notes 
  };
  console.log(client);

  // Save Tutorial in the database
  Client.create(client)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Tutorial."
      });
    });
};


exports.findAll = (req, res) => {
  const title = req.query.title;
  const page = req.query.page;
  const type = req.query.type;
  var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;
  var totalData = 0;
  var numberOfPages = 0;
  var numberOfRows = 10;

  Client.count({
    where: condition
  })
  .then(function(count) {
    if(type == 1)
    {
        numberOfRows = count;
		var offSetCount = ((page-1)*numberOfRows);
		  Client.findAll({ where: condition,limit:numberOfRows,offset:offSetCount,order: [
				['id', 'DESC'],
			]})
			.then(data => {
			  var jsondata = {'data':data,'totalpage':numberOfPages,'currentpage':page-1};
			  res.send(jsondata);
			})
			.catch(err => {
			  res.status(500).send({
				message:
				  err.message || "Some error occurred while retrieving tutorials."
			  });
			});

    }
    else 
    {
        totalData = count;
        numberOfPages = Math.ceil(totalData/numberOfRows);
		
		var offSetCount = ((page-1)*numberOfRows);
		  Client.findAll({ where: condition,limit:numberOfRows,offset:offSetCount,order: [
				['id', 'DESC'],
			]})
			.then(data => {
			  var jsondata = {'data':data,'totalpage':numberOfPages,'currentpage':page-1};
			  res.send(jsondata);
			})
			.catch(err => {
			  res.status(500).send({
				message:
				  err.message || "Some error occurred while retrieving tutorials."
			  });
			});
    }

  });
  
};

 
exports.findOne = (req, res) => {
  const id = req.params.id;

  Client.findByPk(id)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message: "Error retrieving Client with id=" + id
      });
    });
};

// Update a Tutorial by the id in the request
exports.update = (req, res) => {
  const id = req.params.id;

  Client.update(req.body, {
    where: { id: id }
  })
    .then(num => {
      if (num == 1) {
        res.send({
          message: "Client was updated successfully."
        });
      } else {
        res.send({
          message: `Cannot update Client with id=${id}. Maybe Client was not found or req.body is empty!`
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Error updating Client with id=" + id
      });
    });
};

// Delete a Tutorial with the specified id in the request
exports.delete = (req, res) => {
  const id = req.params.id;

  Client.destroy({
    where: { id: id }
  })
    .then(num => {
      if (num == 1) {
        res.send({
          success:true,
          message: "Client was deleted successfully!"
        });
      } else {
        res.send({
          success:false,
          message: `Cannot delete Client with id=${id}. Maybe Client was not found!`
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        success:false,
        message: "Could not delete Client with id=" + id
      });
    });
};
 
