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

router.get("/selectConfig", (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.CILConfig order by id DESC`;
    console.log("Select Query ", query);

    connection.query(query, (err, data) => {
      connection.release();
      if (err) {
        console.log("error found", err);
      } else {
        res.status(200).json({
          message: "succesfully fetched",
          results: "success",
          data: data,
        });
      }
    });
  });
});

router.post("/getSubAssembly", (req, res) => {
  const area = req.body.area;
  const machine = req.body.machine;
  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.CILConfig where area = "${area}" AND machineName="${machine}"`;
    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);
      }
    });
  });
});
module.exports = router;
