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

router.post("/updateDefect", (req, res) => {
  // const id = req.body.id;
  // const defectStatus = req.body.defectStatus;

  const newData = req.body.newData;
  //this user is who solved defect
  const solvedBy = req.body.solvedBy;
  const solvedDate = req.body.solvedDate;

  newData &&
    newData.forEach((element) => {
      if (!element.defectStatus || !solvedBy || !solvedDate || !element.id) {
        console.log("something missing");
      } else {
        db.getConnection((err, connection) => {
          if (err) {
            console.log("database not connected error: >>>", err);
            throw err;
          }
          console.log("get Connection");
          let uQuer = `update finehhdi_eDefect.defect set 
            defectStatus='${element.defectStatus}',
            solvedDate = '${solvedDate}',
            solvedBy="${solvedBy}"
            where id=${element.id}`;

          connection.query(uQuer, (err, data) => {
            connection.release();
            if (err) {
              if (err.code === "ER_DUP_ENTRY") {
                res.status(409).send();
              } else if (err.code === "ENOTFOUND") {
                res.status(404).send();
              } else {
                res.status(401).send();
              }
            } else {
              res.status(200).send();
            }
          });
          console.log("release Connection");
        });
      }
    });
});

// router.post("/updateDefect", (req, res) => {
//   const id = req.body.id;
//   const defectStatus = req.body.defectStatus;
//   //this user is who solved defect
//   const solvedBy = req.body.username
//   const solvedDate = moment().format("YYYY-MM-DD");
//   console.log(solvedDate);

//   db.getConnection((err, connection) => {
//     if (err) {
//       console.log("database not connected error: >>>", err);
//       throw err;
//     }
//     console.log("get Connection");
//       let uQuer = `update finehhdi_eDefect.defect set
//         defectStatus='${defectStatus}',
//         solvedDate = '${solvedDate}',
//         solvedBy="${solvedBy}"
//         where id=${id}`;

//       connection.query(uQuer, (err, data) => {
//         connection.release()
//         if (err) {
//           console.log("error ==>", 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 {
//             console.log(err);
//             res.status(401).json({
//               message: "creation failed",
//               results: "Failed",
//             });
//           }
//         } else {
//           res.status(200).json({
//             message: "Created Successfully",
//             results: "Success",
//           });
//         }

//       });
//     console.log("release Connection");
//   });
// });

module.exports = router;
