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

router.get("/selectOD", (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_CIL.SetOperationDescription ORDER BY id DESC`;
    console.log(query);
    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        console.log(err);
        console.log("error");
      } else {
        res.status(200).send({
          message: "Data Selected Successfully",
          data: data,
        });
      }
    });
})
});


router.post("/selectODForSubAssembly", (req, res) => {
  const subAssembly = req.body.subAssemblyName;
  
  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.SetOperationDescription where subAssembly="${subAssembly}"`;
    console.log(query);
    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        console.log(err);
        console.log("error");
      } else {
        res.status(200).send({
          message: "Data Selected for sub Assembly Successfully",
          data: data,
        });
      }
    });
})
});

// router.post("/selectArea", (req, res) => {
//     const areaName = req.body.areaName;
//     let query = `SELECT * FROM finehhdi_CIL.category WHERE areaName = '${areaName}'`;
  
//     console.log(query);
  
//     db.query(query, (err, data) => {
//       if (err) {
//         res.status(500).send({
//           message: "Error while Successfully",
//         });
//       } else {
//         res.status(200).send({
//           message: "Select area from area Successfully",
//           data: data,
//         });
//       }
//     });
//   });
  
//   router.post("/selectcategoryForFeedback", (req, res) => {
//     const categoryName = req.body.categoryName;
//     let query = `SELECT * FROM finehhdi_CIL.category WHERE categoryName = '${categoryName}'`;
  
//     console.log(query);
  
//     db.query(query, (err, data) => {
//       if (err) {
//         res.status(500).send({
//           message: "Error while Successfully",
//         });
//       } else {
//         res.status(200).send({
//           message: "Select categoryName Successfully",
//           data: data,
//         });
//       }
//     });
//   });
  
//   router.post("/selectAreaForObs", (req, res) => {
//     const areaName = req.body.areaName;
//     const submittedMonth = req.body.submittedMonth;
//     let query = `SELECT * FROM finehhdi_CIL.setBOS WHERE areaName = '${areaName}' AND month = '${submittedMonth}'`;
  
//     console.log(query);
  
//     db.query(query, (err, data) => {
//       if (err) {
//         res.status(500).send({
//           message: "Error while Successfully",
//         });
//       } else {
//         res.status(200).send({
//           message: "Select areaName from setBOS Successfully",
//           data: data,
//         });
//       }
//     });
//   });

module.exports = router;
