var express = require("express");
const router = express.Router();
const db = require("../../utils/database");
const dbFuntion = require("../../utils/dbfunction");
const { response } = require("express");
const moment = require("moment");

router.get("/selectCIL", (req, res) => {
  var status = req.query.status;
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    var query = `select * from finehhdi_CIL.SetCIL order by id desc`;
  
  
    connection.query(query, (err, result) => {
      connection.release();
      if (err) {
        console.log(err);
        res.status(401).json({
          message: "Some error in select CIL selection",
          result: "Failed",
          data: [],
        });
      } else {
        res.status(201).json({
          message: "select data Successfully",
          results: "Success",
          data: result,
        });
      }
    });
  })
});

router.post("/selectCILData", (req, res) => {
  const machineName = req.body.machineName;
  const shift = req.body.shift;
  const day = req.body.day;
  const dateAndTime = moment(new Date()).format("DD-MM-yyyy");
  const beforeSchedule = req.body.beforeSchedule;
  const afterSchedule = req.body.afterSchedule;
  console.log("date====>", moment(new Date()).format("DD-MM-yyyy"));
 
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query = `select * from finehhdi_CIL.SetCIL where machineName = "${machineName}" AND shift="${shift}"`;
    console.log("quey from centralLine data according to machine===>", query);
    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        console.log("errrrrorrrrr", err);
  
        res.status(401).json({
          message: "Some error in select CL selection",
          results: "Failed",
          data: [],
        });
      } else {
        res.status(201).json({
          message: "select data Successfully",
          results: "Success",
          data: data,
        });
        console.log("data", data);
      }
    });
  })
 
});

router.post("/CheckCILData", (req, res) => {
  console.log("checkCIlData ====>", req.body);
  const area = req.body.area;
  const machine = req.body.machine;
  const subassembly = req.body.subassembly;
  console.log("area ===>", area);
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query;
    req.body.subassembly == "all"
      ? (query = `select * from finehhdi_CIL.SetCIL where areaName="${area}" and machineName="${machine}"`)
      : (query = `select * from finehhdi_CIL.SetCIL where areaName="${area}" and machineName="${machine}" and subAssemblyName="${subassembly}"`);
    console.log("Query for getting data on SetCIL", query);
  
    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        res.status(500).send("error while getting data");
      } else {
        res.status(200).send({
          message: "selected data successfully",
          data: data,
        });
      }
    });
  })
  
});
router.post("/CheckCILDatanew", (req, res) => {
  console.log("checkCIlData ====>", req.body);
  const area = req.body.area;
  const machine = req.body.machine;
  // const subassembly = req.body.subassembly;
  console.log("area ===>", area);
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query;
    // req.body.subassembly == "all"
    //   ? (query = `select * from finehhdi_CIL.SetCIL where areaName="${area}" and machineName="${machine}"`)
    //   : (query = `select * from finehhdi_CIL.SetCIL where areaName="${area}" and machineName="${machine}" and subAssemblyName="${subassembly}"`);
    // console.log("Query for getting data on SetCIL", query);
    query = `select * from finehhdi_CIL.SETCIL1 where areaName="${area}" and machineName="${machine}"`;
    console.log("Query for getting data on SetCIL", query);
  
    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        res.status(500).send("error while getting data");
      } else {
        res.status(200).send({
          message: "selected data successfully",
          data: data,
        });
        console.log("response", data)
      }
    });
  })
});

// Select getDepartment
router.get("/getDepartment", (req, res) => {
  
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query = `SELECT * FROM finehhdi_eDefect.Department`;
    console.log(query);
  
    connection.query(query, (err, data) => {
      connection.release();
      console.log("error from Depart name", err);
      // console.log("data from Depart name", data);
      if (err) {
        res
          .status(500)
          .send({ msg: "Error While Selection for Department name" });
      } else {
        res.status(200).send({
          msg: "Seleted for Depart name Successfully",
          data: data,
        });
      }
    });
  })
});



// Select getMachinesType
router.post("/getMachinesType", (req, res) => {
  const department = req.body.department;
  console.log("machine name from frontend", department);
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query = `SELECT * FROM finehhdi_eDefect.machineTypes where department= "${department}"`;
    console.log(query);
    connection.query(query, (err, data) => {
      connection.release();
      console.log("error from Machine name", err);
      console.log("data from Machine name", data);
      if (err) {
        res.status(500).send({ msg: "Error While Selection for Machine name" });
      } else {
        res.status(200).send({
          msg: "Seleted for Machine name Successfully",
          data: data,
        });
      }

    });
  })
});

// Select selectEquipmentOwnerName
router.post("/selectEquipmentOwnerName", (req, res) => {
  const department = req.body.department;
  console.log("department name from frontend", department);
  const machineName = req.body.machineName;
  console.log("machine name from frontend", machineName);
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query = `SELECT * FROM finehhdi_eDefect.machineTypes where machineTypes= "${machineName}" AND department= "${department}"`;
    console.log(query);
    connection.query(query, (err, data) => {
      connection.release();
      console.log("error from Equipment Owner name", err);
      console.log("data from Equipment Owner name", data);
      if (err) {
        res
          .status(500)
          .send({ msg: "Error While Selection for Equipment Owner name" });
      } else {
        res.status(200).send({
          msg: "Seleted for Equipment Owner name Successfully",
          data: data,
        });
      }
    });
  })
});

router.get("/selectMachineName", (req, res) => {
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let query = `select * from finehhdi_eDefect.machineTypes`;
    console.log("quey from show area data", query);
    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        console.log("errrrrorrrrr", err);
  
        res.status(401).json({
          message: "Some error in select CL selection",
          results: "Failed",
          data: [],
        });
      } else {
        res.status(201).json({
          message: "select data Successfully",
          results: "Success",
          data: data,
        });
        console.log("dataaaaaa", data);
      }
    });
  })
});


// Select getSetCIL1
router.get("/getSetCIL1", (req, res) => {
  console.log("getSetCIL1 request ==>");
  // let query=`SELECT * FROM finehhdi_CIL.SetCIL Department`
//   let query = `select * from finehhdi_CIL.testSetCIL `;
db.getConnection((err, connection) => {
  if (err) {
    console.log("database not connected error: >>>", err);
    res.send(500).status({
      msg: "Error from database",
      err: err,
    });
  }
  var query = `SELECT * FROM finehhdi_CIL.SETCIL1 order by id desc`;

  console.log(query);
  connection.query(query, (err, data) => {
    connection.release();
    if (err) {
      res.status(500).send({ message: "Error While Selection for Department name" });
    } else {
      res.status(200).send({
        message: "Seleted for Depart name Successfully",
        data: data,
      });
    }
  });
})
});

module.exports = router;
