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

router.post("/updateCIL", (req, res) => {
  const id = req.body.id;
  const areaName = req.body.areaName;
  const machineName = req.body.machineName;
  const equipmentOwnerName = req.body.equipmentOwnerName;
  const subAssemblyName = req.body.subAssemblyName;
  //For HP values
  const HP1 = req.body.HP1 || 0;
  const HP2 = req.body.HP2 || 0;
  const HP3 = req.body.HP3 || 0;
  const HP4 = req.body.HP4 || 0;
  const HP5 = req.body.HP5 || 0;
  const HP6 = req.body.HP6 || 0;
  const HP7 = req.body.HP7 || 0;
  const HP8 = req.body.HP8 || 0;
  const HP9 = req.body.HP9 || 0;
  const HP10 = req.body.HP10 || 0;
  const HP11 = req.body.HP11 || 0;
  const HP12 = req.body.HP12 || 0;
  const HP13 = req.body.HP13 || 0;
  const HP14 = req.body.HP14 || 0;
  const HP15 = req.body.HP15 || 0;
  const HP16 = req.body.HP16 || 0;
  const HP17 = req.body.HP17 || 0;
  const HP18 = req.body.HP18 || 0;
  const HP19 = req.body.HP19 || 0;
  const HP20 = req.body.HP20 || 0;
  const HP21 = req.body.HP21 || 0;
  const HP22 = req.body.HP22 || 0;
  const HP23 = req.body.HP23 || 0;
  const HP24 = req.body.HP24 || 0;
  const HP25 = req.body.HP25 || 0;
  //For HP Variant Value
  const HPVariant1 = req.body.HPVariant1 || "";
  const HPVariant2 = req.body.HPVariant2 || "";
  const HPVariant3 = req.body.HPVariant3 || "";
  const HPVariant4 = req.body.HPVariant4 || "";
  const HPVariant5 = req.body.HPVariant5 || "";
  const HPVariant6 = req.body.HPVariant6 || "";
  const HPVariant7 = req.body.HPVariant7 || "";
  const HPVariant8 = req.body.HPVariant8 || "";
  const HPVariant9 = req.body.HPVariant9 || "";
  const HPVariant10 = req.body.HPVariant10 || "";
  const HPVariant11 = req.body.HPVariant11 || "";
  const HPVariant12 = req.body.HPVariant12 || "";
  const HPVariant13 = req.body.HPVariant13 || "";
  const HPVariant14 = req.body.HPVariant14 || "";
  const HPVariant15 = req.body.HPVariant15 || "";
  const HPVariant16 = req.body.HPVariant16 || "";
  const HPVariant17 = req.body.HPVariant17 || "";
  const HPVariant18 = req.body.HPVariant18 || "";
  const HPVariant19 = req.body.HPVariant19 || "";
  const HPVariant20 = req.body.HPVariant20 || "";
  const HPVariant21 = req.body.HPVariant21 || "";
  const HPVariant22 = req.body.HPVariant22 || "";
  const HPVariant23 = req.body.HPVariant23 || "";
  const HPVariant24 = req.body.HPVariant24 || "";
  const HPVariant25 = req.body.HPVariant25 || "";

  //For Frequency Value
  const frequency1 = req.body.frequency1 || "";
  const frequency2 = req.body.frequency2 || "";
  const frequency3 = req.body.frequency3 || "";
  const frequency4 = req.body.frequency4 || "";
  const frequency5 = req.body.frequency5 || "";
  const frequency6 = req.body.frequency6 || "";
  const frequency7 = req.body.frequency7 || "";
  const frequency8 = req.body.frequency8 || "";
  const frequency9 = req.body.frequency9 || "";
  const frequency10 = req.body.frequency10 || "";
  const frequency11 = req.body.frequency11 || "";
  const frequency12 = req.body.frequency12 || "";
  const frequency13 = req.body.frequency13 || "";
  const frequency14 = req.body.frequency14 || "";
  const frequency15 = req.body.frequency15 || "";
  const frequency16 = req.body.frequency16 || "";
  const frequency17 = req.body.frequency17 || "";
  const frequency18 = req.body.frequency18 || "";
  const frequency19 = req.body.frequency19 || "";
  const frequency20 = req.body.frequency20 || "";
  const frequency21 = req.body.frequency21 || "";
  const frequency22 = req.body.frequency22 || "";
  const frequency23 = req.body.frequency23 || "";
  const frequency24 = req.body.frequency24 || "";
  const frequency25 = req.body.frequency25 || "";

  //For on_off Value
  const on_off1 = req.body.on_off1 || 0;
  const on_off2 = req.body.on_off2 || 0;
  const on_off3 = req.body.on_off3 || 0;
  const on_off4 = req.body.on_off4 || 0;
  const on_off5 = req.body.on_off5 || 0;
  const on_off6 = req.body.on_off6 || 0;
  const on_off7 = req.body.on_off7 || 0;
  const on_off8 = req.body.on_off8 || 0;
  const on_off9 = req.body.on_off9 || 0;
  const on_off10 = req.body.on_off10 || 0;
  const on_off11 = req.body.on_off11 || 0;
  const on_off12 = req.body.on_off12 || 0;
  const on_off13 = req.body.on_off13 || 0;
  const on_off14 = req.body.on_off14 || 0;
  const on_off15 = req.body.on_off15 || 0;
  const on_off16 = req.body.on_off16 || 0;
  const on_off17 = req.body.on_off17 || 0;
  const on_off18 = req.body.on_off18 || 0;
  const on_off19 = req.body.on_off19 || 0;
  const on_off20 = req.body.on_off20 || 0;
  const on_off21 = req.body.on_off21 || 0;
  const on_off22 = req.body.on_off22 || 0;
  const on_off23 = req.body.on_off23 || 0;
  const on_off24 = req.body.on_off24 || 0;
  const on_off25 = req.body.on_off25 || 0;

  //For operation description Value
  const operDes1 = req.body.operDes1 || "";
  const operDes2 = req.body.operDes2 || "";
  const operDes3 = req.body.operDes3 || "";
  const operDes4 = req.body.operDes4 || "";
  const operDes5 = req.body.operDes5 || "";
  const operDes6 = req.body.operDes6 || "";
  const operDes7 = req.body.operDes7 || "";
  const operDes8 = req.body.operDes8 || "";
  const operDes9 = req.body.operDes9 || "";
  const operDes10 = req.body.operDes10 || "";
  const operDes11 = req.body.operDes11 || "";
  const operDes12 = req.body.operDes12 || "";
  const operDes13 = req.body.operDes13 || "";
  const operDes14 = req.body.operDes14 || "";
  const operDes15 = req.body.operDes15 || "";
  const operDes16 = req.body.operDes16 || "";
  const operDes17 = req.body.operDes17 || "";
  const operDes18 = req.body.operDes18 || "";
  const operDes19 = req.body.operDes19 || "";
  const operDes20 = req.body.operDes20 || "";
  const operDes21 = req.body.operDes21 || "";
  const operDes22 = req.body.operDes22 || "";
  const operDes23 = req.body.operDes23 || "";
  const operDes24 = req.body.operDes24 || "";
  const operDes25 = req.body.operDes25 || "";

  //For OPL Value
  const OPL1 = req.body.OPL1 || "";
  const OPL2 = req.body.OPL2 || "";
  const OPL3 = req.body.OPL3 || "";
  const OPL4 = req.body.OPL4 || "";
  const OPL5 = req.body.OPL5 || "";
  const OPL6 = req.body.OPL6 || "";
  const OPL7 = req.body.OPL7 || "";
  const OPL8 = req.body.OPL8 || "";
  const OPL9 = req.body.OPL9 || "";
  const OPL10 = req.body.OPL10 || "";
  const OPL11 = req.body.OPL11 || "";
  const OPL12 = req.body.OPL12 || "";
  const OPL13 = req.body.OPL13 || "";
  const OPL14 = req.body.OPL14 || "";
  const OPL15 = req.body.OPL15 || "";
  const OPL16 = req.body.OPL16 || "";
  const OPL17 = req.body.OPL17 || "";
  const OPL18 = req.body.OPL18 || "";
  const OPL19 = req.body.OPL19 || "";
  const OPL20 = req.body.OPL20 || "";
  const OPL21 = req.body.OPL21 || "";
  const OPL22 = req.body.OPL22 || "";
  const OPL23 = req.body.OPL23 || "";
  const OPL24 = req.body.OPL24 || "";
  const OPL25 = req.body.OPL25 || "";

  const dateAndTime = req.body.dateAndTime;

  let uQuery = `update finehhdi_CIL.SetCIL set 
    areaName='${areaName}',
    machineName='${machineName}',
    equipmentOwnerName = '${equipmentOwnerName}',
    subAssemblyName = '${subAssemblyName}',
    HP1 = ${HP1},
    HP2 = ${HP2},
    HP3 = ${HP3},
    HP4 = ${HP4},
    HP5 = ${HP5},
    HP6 = ${HP6},
    HP7 = ${HP7},
    HP8 = ${HP8},
    HP9 = ${HP9},
    HP10 = ${HP10},
    HP11 = ${HP11},
    HP12 = ${HP12},
    HP13 = ${HP13},
    HP14 = ${HP14},
    HP15 = ${HP15},
    HP16 = ${HP16},
    HP17 = ${HP17},
    HP18 = ${HP18},
    HP19 = ${HP19},
    HP20 = ${HP20},
    HP21 = ${HP21},
    HP22 = ${HP22},
    HP23 = ${HP23},
    HP24 = ${HP24},
    HP25 = ${HP25},
    HPVariant1 = '${HPVariant1}',
    HPVariant2 = '${HPVariant2}',
    HPVariant3 = '${HPVariant3}',
    HPVariant4 = '${HPVariant4}',
    HPVariant5 = '${HPVariant5}',
    HPVariant6 = '${HPVariant6}',
    HPVariant7 = '${HPVariant7}',
    HPVariant8 = '${HPVariant8}',
    HPVariant9 = '${HPVariant9}',
    HPVariant10 = '${HPVariant10}',
    HPVariant11 = '${HPVariant11}',
    HPVariant12 = '${HPVariant12}',
    HPVariant13 = '${HPVariant13}',
    HPVariant14 = '${HPVariant14}',
    HPVariant15 = '${HPVariant15}',
    HPVariant16 = '${HPVariant16}',
    HPVariant17 = '${HPVariant17}',
    HPVariant18 = '${HPVariant18}',
    HPVariant19 = '${HPVariant19}',
    HPVariant20 = '${HPVariant20}',
    HPVariant21 = '${HPVariant21}',
    HPVariant22 = '${HPVariant22}',
    HPVariant23 = '${HPVariant23}',
    HPVariant24 = '${HPVariant24}',
    HPVariant25 = '${HPVariant25}',
    frequency1 = '${frequency1}',
    frequency2 = '${frequency2}',
    frequency3 = '${frequency3}',
    frequency4 = '${frequency4}',
    frequency5 = '${frequency5}',
    frequency6 = '${frequency6}',
    frequency7 = '${frequency7}',
    frequency8 = '${frequency8}',
    frequency9 = '${frequency9}',
    frequency10 = '${frequency10}',
    frequency11 = '${frequency11}',
    frequency12 = '${frequency12}',
    frequency13 = '${frequency13}',
    frequency14 = '${frequency14}',
    frequency15 = '${frequency15}',
    frequency16 = '${frequency16}',
    frequency17 = '${frequency17}',
    frequency18 = '${frequency18}',
    frequency19 = '${frequency19}',
    frequency20 = '${frequency20}',
    frequency21 = '${frequency21}',
    frequency22 = '${frequency22}',
    frequency23 = '${frequency23}',
    frequency24 = '${frequency24}',
    frequency25 = '${frequency25}',
    on_off1 = ${on_off1},
    on_off2 = ${on_off2},
    on_off3 = ${on_off3},
    on_off4 = ${on_off4},
    on_off5 = ${on_off5},
    on_off6 = ${on_off6},
    on_off7 = ${on_off7},
    on_off8 = ${on_off8},
    on_off9 = ${on_off9},
    on_off10 = ${on_off10},
    on_off11 = ${on_off11},
    on_off12 = ${on_off12},
    on_off13 = ${on_off13},
    on_off14 = ${on_off14},
    on_off15 = ${on_off15},
    on_off16 = ${on_off16},
    on_off17 = ${on_off17},
    on_off18 = ${on_off18},
    on_off19 = ${on_off19},
    on_off20 = ${on_off20},
    on_off21 = ${on_off21},
    on_off22 = ${on_off22},
    on_off23 = ${on_off23},
    on_off24 = ${on_off24},
    on_off25 = ${on_off25},
    operDes1 = '${operDes1}',
    operDes2 = '${operDes2}',
    operDes3 = '${operDes3}',
    operDes4 = '${operDes4}',
    operDes5 = '${operDes5}',
    operDes6 = '${operDes6}',
    operDes7 = '${operDes7}',
    operDes8 = '${operDes8}',
    operDes9 = '${operDes9}',
    operDes10 = '${operDes10}',
    operDes11 = '${operDes11}',
    operDes12 = '${operDes12}',
    operDes13 = '${operDes13}',
    operDes14 = '${operDes14}',
    operDes15 = '${operDes15}',
    operDes16 = '${operDes16}',
    operDes17 = '${operDes17}',
    operDes18 = '${operDes18}',
    operDes19 = '${operDes19}',
    operDes20 = '${operDes20}',
    operDes21 = '${operDes21}',
    operDes22 = '${operDes22}',
    operDes23 = '${operDes23}',
    operDes24 = '${operDes24}',
    operDes25 = '${operDes25}',
    OPL1 = '${OPL1}',
    OPL2 = '${OPL2}',
    OPL3 = '${OPL3}',
    OPL4 = '${OPL4}',
    OPL5 = '${OPL5}',
    OPL6 = '${OPL6}',
    OPL7 = '${OPL7}',
    OPL8 = '${OPL8}',
    OPL9 = '${OPL9}',
    OPL10 = '${OPL10}',
    OPL11 = '${OPL11}',
    OPL12 = '${OPL12}',
    OPL13 = '${OPL13}',
    OPL14 = '${OPL14}',
    OPL15 = '${OPL15}',
    OPL16 = '${OPL16}',
    OPL17 = '${OPL17}',
    OPL18 = '${OPL18}',
    OPL19 = '${OPL19}',
    OPL20 = '${OPL20}',
    OPL21 = '${OPL21}',
    OPL22 = '${OPL22}',
    OPL23 = '${OPL23}',
    OPL24 = '${OPL24}',
    OPL25 = '${OPL25}',
    dateAndTime = '${dateAndTime}'
    where id=${id}`;

    console.log("update query =============>", uQuery);
    db.getConnection((err, connection) => {
      if (err) {
        console.log("database not connected error: >>>", err);
        res.send(500).status({
          msg: "Error from database",
          err: err,
        });
      }
      
        connection.query(uQuery, (err, data) => {
          connection.release();
          if (err) {
            console.log(err);
            res.status(401).json({
              message: "error! CIL cant be Updated for somereason",
              results: "Failed",
            });
          } else {
            res.status(200).json({
              message: "CIL Updated Successfully",
              results: "Success",
            });
          }
        });
    })
});


router.post("/updateSetCIL", (req, res)=>{
  console.log("req.body from updateSetCIL ====>", req.body);
  const id  = req.body.id
  const areaName  = req.body.areaName
  const machineName  = req.body.machineName
  const equipmentOwnerName  = req.body.equipmentOwnerName
  const subAssemblyName  = req.body.subAssemblyName
  const HP  = req.body.HP
  const HPVariant  = req.body.HPVariants
  const frequency  = req.body.frequency
  const startDay  = req.body.startDay
  const OPL  = req.body.OPL
  const on_off  = req.body.onOff
  const operDesc  = req.body.operDesc
  const submittedDate  = req.body.submittedDate;
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      res.send(500).status({
        msg: "Error from database",
        err: err,
      });
    }
    let updateQuery = `UPDATE finehhdi_CIL.SETCIL1 SET  
    areaName='${areaName}',
    machineName='${machineName}',
    equipmentOwnerName = '${equipmentOwnerName}',
    subAssemblyName = '${subAssemblyName}',
    HP = '${HP}',
    HPVariants = '${HPVariant}',
    frequency = '${frequency}',
    startDay = '${startDay}',
    OPL = '${OPL}',
    onOff = '${on_off}',
    operDesc = '${operDesc}',
    submittedDate = '${submittedDate}'
    WHERE id = ${id}
    `
  
    connection.query(updateQuery, (err, data)=>{
      connection.release();
      if (err) {
        console.log(err);
        res.status(401).json({
          message: "error! CIL cant be Updated for somereason",
          results: "Failed",
        });
      } else {
        res.status(200).json({
          message: "CIL Updated Successfully",
          results: "Success",
          data: data
        });
      }
    })
  })

})


module.exports = router;
