const db = require("../models");
const Staff = db.staff;

var bcrypt = require("bcryptjs");
const { splitSite } = require("../middlewares");
// Create and Save a new Staff (driver, distributor, admin)
exports.createStaff = (req, res) => {
  // Validate request
  if (!req.body.username) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  // Create a Staff
  const staff = new Staff({
    username: req.body.username || req.body.email || '',
    name: req.body.name || '',
    name_cn: req.body.name_cn || '',
    email: req.body.email || '',
    password: req.body.password ? bcrypt.hashSync(req.body.password, 8) : '',
    roles: req.body.roles || [],
    mobile_phone: req.body.mobile_phone || '',
    phone: req.body.phone || '',
    home_phone: req.body.home_phone || '',
    language: req.body.language || '',
    employment_status: req.body.employment_status || '',
    status: req.body.status || 'active',
    title: req.body.title || '',
    title_cn: req.body.title_cn || '',
    firstname: req.body.firstname || '',
    lastname: req.body.lastname || '',
    department: req.body.department || '',
    birth_date: req.body.birth_date || null,
    driver_capacity: req.body.driver_capacity || null,
    date_hired: req.body.date_hired || null,
    create_by: req.body.create_by || '',
    create_date: new Date(),
    edit_by: req.body.edit_by || '',
    edit_date: new Date(),
    salt: req.body.salt || '264897',
    city: req.body.city || '',
    state: req.body.state || '',
    zipcode: req.body.zipcode || '',
    group: req.body.group || [],
    tags: req.body.tags || [],
    parent_id: req.body.parent_id || '5eee3552b02fac3d4acfd5ea',
    site
  });
  // Save Staff in the database
  staff
    .save(staff)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Staff."
      });
    });
};

// Update a Staff by the id in the request
exports.updateStaff = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  if (req.body.password) {
    req.body.password = bcrypt.hashSync(req.body.password, 8);
  }
  Staff.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update staff with id=${id}. Maybe staff was not found!`
        });
      } else res.send({ success: true, message: "Staff was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Staff with id=" + id
      });
    });
};
// Get Staff with username or email 
exports.getStaffsWithNameOrEmail = (req, res) => {
  var params = req.query;
  var condition = {};
  const nameOrEmail = params?.nameOrEmail;
  if (nameOrEmail) {
    condition = { $or: [ 
      { email: nameOrEmail },
      { name: nameOrEmail }
    ]};
  }
  condition = splitSite.splitSiteGet(req, condition);
  Staff.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving staffs."
      });
    });
};



