var express = require("express");
const router = express.Router();
const db = require("../../utils/database");

router.post("/insertMachineType", (req, res) => {
  //var code=req.body.code
  var type = req.body.type;
  var department = req.body.department;
  var equipmentName = req.body.equipmentName;

  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    var query = `insert into finehhdi_eDefect.machineTypes (machineTypes,department,equipmentName) values ("${type}","${department}","${equipmentName}");`;
    connection.query(query, (err, result) => {
      connection.release();
      if (err) {
        if (err.code === "ER_DUP_ENTRY") {
          res.status(409).json({
            message: "Duplicate User",
            results: "Failed",
          });
        } else if (err.code === "ENOTFOUND") {
          res.status(404).json({
            message: "Internet Connection Failed",
            results: "failed",
          });
        } else {
          res.status(401).json({
            message: "dep creation failed",
            results: "Failed",
          });
        }
      } else {
        res.status(200).json({
          message: "Created Successfully",
          results: "Success",
        });
      }
    });
    console.log("release Connection");
  });
});

///////
router.get("/getTypes", (req, res) => {
  var status = req.query.status;
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    var query = `select * from finehhdi_eDefect.machineTypes order by id desc`;

    connection.query(query, (err, result) => {
      connection.release();
      if (err) {
        res.status(401).json({
          message: "Some error in type selection",
          results: "Failed",
          mtype: [],
        });
      } else {
        res.status(201).json({
          message: "type Successfully",
          results: "Success",
          mtype: result,
        });
      }
    });
    console.log("release Connection");
  });
});

//for getOwnerName api
router.post("/getOwnerName", (req, res) => {
  var machineType = req.body.machineType;
  var department = req.body.department;

  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    var query = `select * from finehhdi_eDefect.machineTypes where department="${department}" AND machineTypes="${machineType}"`;
    connection.query(query, (err, result) => {
      connection.release();
      if (err) {
        res.status(401).json({
          message: "Some error in type selection",
          results: "Failed",
          data: [],
        });
      } else {
        res.status(200).json({
          message: "get Owner Name Successfully",
          results: "Success",
          data: result,
        });
      }
    });
    console.log("release Connection");
  });
});

//for getMachines api
router.post("/getMachinesType", (req, res) => {
  var department = req.body.department;
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    var query = `select * from finehhdi_eDefect.machineTypes where department="${department}"`;
    connection.query(query, (err, result) => {
      connection.release();
      if (err) {
        res.status(401).json({
          message: "Some error in type selection",
          results: "Failed",
          data: [],
        });
      } else {
        res.status(200).json({
          message: "getMachinesType Name Successfully",
          results: "Success",
          data: result,
        });
      }
    });
    console.log("release Connection");
  });
});

// get machine types by department and username
router.post("/getMachinesTypeFilter", (req, res) => {
  var department = req.body.department;
  var machine = req.body.machine;

  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    var query = `select * from finehhdi_eDefect.machineTypes where department="${department}"`;

    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        res.status(401).json({
          message: "Some error in type selection",
          results: "Failed",
          data: [],
        });
      } else {
        res.status(200).json({
          message: "getMachinesType Name Successfully",
          results: "Success",
          data: data,
        });
      }
    });
    console.log("release Connection");
  });
});

module.exports = router;
