var express = require("express");
const router = express.Router();
const db = require("../../utils/database");
const dbFuntion = require("../../utils/dbfunction");
const { response, json } = require("express");
const moment = require("moment");
const path = require("path");
const multer = require("multer");
const fs = require("fs");
const { log } = require("console");
const { CONNREFUSED } = require("dns");

const storage = multer.diskStorage({
  // https://www.npmjs.com/package/multer#diskstorage
  destination: "public/uploads",
  filename: function (req, file, cb) {
    cb(null, `file-${new Date().getTime()}.${file.mimetype.split("/")[1]}`);
  },
});
var upload = multer({ storage: storage });

// router.post("/createCIL", (req, res) => {
router.post("/createCIL", upload.any(), (req, res) => {
  const url = req.protocol + "://" + req.get("host");
  console.log("url check ============>", url);
  console.log("req body ============>", req.body);
  console.log("req files ============>", req.files);

  const areaName = req.body.areaName;
  const machineName = req.body.machineName;
  const equipmentOwnerName = req.body.equipmentOwnerName;
  const subAssemblyName = req.body.subAssemblyName;
  // const Range1 = req.body.Range1;

  //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 || "";

  //StartDay
  const startDay1 = req.body.startDay1 || "";
  const startDay2 = req.body.startDay2 || "";
  const startDay3 = req.body.startDay3 || "";
  const startDay4 = req.body.startDay4 || "";
  const startDay5 = req.body.startDay5 || "";
  const startDay6 = req.body.startDay6 || "";
  const startDay7 = req.body.startDay7 || "";
  const startDay8 = req.body.startDay8 || "";
  const startDay9 = req.body.startDay9 || "";
  const startDay10 = req.body.startDay10 || "";
  const startDay11 = req.body.startDay11 || "";
  const startDay12 = req.body.startDay12 || "";
  const startDay13 = req.body.startDay13 || "";
  const startDay14 = req.body.startDay14 || "";
  const startDay15 = req.body.startDay15 || "";
  const startDay16 = req.body.startDay16 || "";
  const startDay17 = req.body.startDay17 || "";
  const startDay18 = req.body.startDay18 || "";
  const startDay19 = req.body.startDay19 || "";
  const startDay20 = req.body.startDay20 || "";
  const startDay21 = req.body.startDay21 || "";
  const startDay22 = req.body.startDay22 || "";
  const startDay23 = req.body.startDay23 || "";
  const startDay24 = req.body.startDay24 || "";
  const startDay25 = req.body.startDay25 || "";

  //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 || "";
  if (
    req.files[0] === undefined ||
    req.files[0] === null ||
    req.files[0] === " "
  ) {
    var OPL1 = "No OPL1";
  } else {
    var OPL1 = url + "/uploads/" + req.files[0].filename;
  }
  //OPL2
  if (
    req.files[1] === undefined ||
    req.files[1] === null ||
    req.files[1] === " "
  ) {
    var OPL2 = "No OPL2";
  } else {
    var OPL2 = url + "/uploads/" + req.files[1].filename;
  }
  //OPL3
  if (
    req.files[2] === undefined ||
    req.files[2] === null ||
    req.files[2] === " "
  ) {
    var OPL3 = "No OPL3";
  } else {
    var OPL3 = url + "/uploads/" + req.files[2].filename;
  }
  //OPL4
  if (
    req.files[3] === undefined ||
    req.files[3] === null ||
    req.files[3] === " "
  ) {
    var OPL4 = "No OPL4";
  } else {
    var OPL4 = url + "/uploads/" + req.files[3].filename;
  }
  //OPL5
  if (
    req.files[4] === undefined ||
    req.files[4] === null ||
    req.files[4] === " "
  ) {
    var OPL5 = "No OPL5";
  } else {
    var OPL5 = url + "/uploads/" + req.files[4].filename;
  }
  //OPL6
  if (
    req.files[5] === undefined ||
    req.files[5] === null ||
    req.files[5] === " "
  ) {
    var OPL6 = "No OPL6";
  } else {
    var OPL6 = url + "/uploads/" + req.files[5].filename;
  }
  //OPL7
  if (
    req.files[6] === undefined ||
    req.files[6] === null ||
    req.files[6] === " "
  ) {
    var OPL7 = "No OPL7";
  } else {
    var OPL7 = url + "/uploads/" + req.files[6].filename;
  }
  //OPL8
  if (
    req.files[7] === undefined ||
    req.files[7] === null ||
    req.files[7] === " "
  ) {
    var OPL8 = "No OPL8";
  } else {
    var OPL8 = url + "/uploads/" + req.files[7].filename;
  }
  //OPL9
  if (
    req.files[8] === undefined ||
    req.files[8] === null ||
    req.files[8] === " "
  ) {
    var OPL9 = "No OPL9";
  } else {
    var OPL9 = url + "/uploads/" + req.files[8].filename;
  }
  //OPL10
  if (
    req.files[9] === undefined ||
    req.files[9] === null ||
    req.files[9] === " "
  ) {
    var OPL10 = "No OPL10";
  } else {
    var OPL10 = url + "/uploads/" + req.files[9].filename;
  }
  //OPL11
  if (
    req.files[10] === undefined ||
    req.files[10] === null ||
    req.files[10] === " "
  ) {
    var OPL11 = "No OPL11";
  } else {
    var OPL11 = url + "/uploads/" + req.files[10].filename;
  }
  //OPL12
  if (
    req.files[11] === undefined ||
    req.files[11] === null ||
    req.files[11] === " "
  ) {
    var OPL12 = "No OPL12";
  } else {
    var OPL12 = url + "/uploads/" + req.files[11].filename;
  }
  //OPL13
  if (
    req.files[12] === undefined ||
    req.files[12] === null ||
    req.files[12] === " "
  ) {
    var OPL13 = "No OPL13";
  } else {
    var OPL13 = url + "/uploads/" + req.files[12].filename;
  }
  //OPL14
  if (
    req.files[13] === undefined ||
    req.files[13] === null ||
    req.files[13] === " "
  ) {
    var OPL14 = "No OPL14";
  } else {
    var OPL14 = url + "/uploads/" + req.files[13].filename;
  }
  //OPL15
  if (
    req.files[14] === undefined ||
    req.files[14] === null ||
    req.files[14] === " "
  ) {
    var OPL15 = "No OPL15";
  } else {
    var OPL15 = url + "/uploads/" + req.files[14].filename;
  }
  //OPL16
  if (
    req.files[15] === undefined ||
    req.files[15] === null ||
    req.files[15] === " "
  ) {
    var OPL16 = "No OPL16";
  } else {
    var OPL16 = url + "/uploads/" + req.files[15].filename;
  }
  //OPL17
  if (
    req.files[16] === undefined ||
    req.files[16] === null ||
    req.files[16] === " "
  ) {
    var OPL17 = "No OPL17";
  } else {
    var OPL17 = url + "/uploads/" + req.files[16].filename;
  }
  //OPL18
  if (
    req.files[17] === undefined ||
    req.files[17] === null ||
    req.files[17] === " "
  ) {
    var OPL18 = "No OPL18";
  } else {
    var OPL18 = url + "/uploads/" + req.files[17].filename;
  }
  //OPL19
  if (
    req.files[18] === undefined ||
    req.files[18] === null ||
    req.files[18] === " "
  ) {
    var OPL19 = "No OPL19";
  } else {
    var OPL19 = url + "/uploads/" + req.files[18].filename;
  }
  //OPL20
  if (
    req.files[19] === undefined ||
    req.files[19] === null ||
    req.files[19] === " "
  ) {
    var OPL20 = "No OPL20";
  } else {
    var OPL20 = url + "/uploads/" + req.files[19].filename;
  }
  //OPL21
  if (
    req.files[20] === undefined ||
    req.files[20] === null ||
    req.files[20] === " "
  ) {
    var OPL21 = "No OPL21";
  } else {
    var OPL21 = url + "/uploads/" + req.files[20].filename;
  }
  //OPL22
  if (
    req.files[21] === undefined ||
    req.files[21] === null ||
    req.files[21] === " "
  ) {
    var OPL22 = "No OPL22";
  } else {
    var OPL22 = url + "/uploads/" + req.files[21].filename;
  }
  //OPL23
  if (
    req.files[22] === undefined ||
    req.files[22] === null ||
    req.files[22] === " "
  ) {
    var OPL23 = "No OPL23";
  } else {
    var OPL23 = url + "/uploads/" + req.files[22].filename;
  }
  //OPL24
  if (
    req.files[23] === undefined ||
    req.files[23] === null ||
    req.files[23] === " "
  ) {
    var OPL24 = "No OPL24";
  } else {
    var OPL24 = url + "/uploads/" + req.files[23].filename;
  }
  //OPL25
  if (
    req.files[24] === undefined ||
    req.files[24] === null ||
    req.files[24] === " "
  ) {
    var OPL25 = "No OPL25";
  } else {
    var OPL25 = url + "/uploads/" + req.files[24].filename;
  }

  const month = req.body.month;
  const dateAndTime = req.body.dateAndTime;

  var query = `insert into finehhdi_CIL.SetCIL 
        (areaName,machineName,equipmentOwnerName,subAssemblyName,
        HP1,HP2,HP3,HP4,HP5,HP6,HP7,HP8,HP9,HP10,HP11,HP12,HP13,HP14,HP15,HP16,HP17,HP18,HP19,HP20,HP21,HP22,HP23,HP24,HP25,
        HPVariant1,HPVariant2,HPVariant3,HPVariant4,HPVariant5,HPVariant6,HPVariant7,HPVariant8,HPVariant9,HPVariant10,
        HPVariant11,HPVariant12,HPVariant13,HPVariant14,HPVariant15,HPVariant16,HPVariant17,HPVariant18,HPVariant19,HPVariant20,
        HPVariant21,HPVariant22,HPVariant23,HPVariant24,HPVariant25,
        frequency1,frequency2,frequency3,frequency4,frequency5,frequency6,frequency7,frequency8,frequency9,frequency10,
        frequency11,frequency12,frequency13,frequency14,frequency15,frequency16,frequency17,frequency18,frequency19,frequency20,
        frequency21,frequency22,frequency23,frequency24,frequency25,
        startDay1,startDay2,startDay3,startDay4,startDay5,startDay6,startDay7,startDay8,startDay9,startDay10,
        startDay11,startDay12,startDay13,startDay14,startDay15,startDay16,startDay17,startDay18,startDay19,startDay20,
        startDay21,startDay22,startDay23,startDay24,startDay25,
        on_off1,on_off2,on_off3,on_off4,on_off5,on_off6,on_off7,on_off8,on_off9,on_off10,
        on_off11,on_off12,on_off13,on_off14,on_off15,on_off16,on_off17,on_off18,on_off19,on_off20,
        on_off21,on_off22,on_off23,on_off24,on_off25,
        operDes1,operDes2,operDes3,operDes4,operDes5,operDes6,operDes7,operDes8,operDes9,operDes10,
        operDes11,operDes12,operDes13,operDes14,operDes15,operDes16,operDes17,operDes18,operDes19,operDes20,
        operDes21,operDes22,operDes23,operDes24,operDes25,
        OPL1,OPL2,OPL3,OPL4,OPL5,OPL6,OPL7,OPL8,OPL9,OPL10,OPL11,OPL12,OPL13,OPL14,OPL15,OPL16,OPL17,OPL18,OPL19,OPL20,
        OPL21,OPL22,OPL23,OPL24,OPL25,
        dateAndTime        )
        values ("${areaName}","${machineName}","${equipmentOwnerName}","${subAssemblyName}",
        ${HP1},${HP2},${HP3},${HP4},${HP5},${HP6},${HP7},${HP8},${HP9},${HP10},${HP11},${HP12},${HP13},${HP14},${HP15},${HP16},${HP17},${HP18},${HP19},${HP20},
        ${HP21},${HP22},${HP23},${HP24},${HP25},
        "${HPVariant1}","${HPVariant2}","${HPVariant3}","${HPVariant4}","${HPVariant5}","${HPVariant6}","${HPVariant7}","${HPVariant8}","${HPVariant9}","${HPVariant10}",
        "${HPVariant11}","${HPVariant12}","${HPVariant13}","${HPVariant14}","${HPVariant15}","${HPVariant16}",
        "${HPVariant17}","${HPVariant18}","${HPVariant19}","${HPVariant20}","${HPVariant21}","${HPVariant22}","${HPVariant23}","${HPVariant24}","${HPVariant25}",
        "${frequency1}","${frequency2}","${frequency3}","${frequency4}","${frequency5}","${frequency6}","${frequency7}","${frequency8}","${frequency9}","${frequency10}",
        "${frequency11}","${frequency12}","${frequency13}","${frequency14}","${frequency15}","${frequency16}","${frequency17}","${frequency18}","${frequency19}","${frequency20}",
        "${frequency21}","${frequency22}","${frequency23}","${frequency24}","${frequency25}",
        "${startDay1}","${startDay2}","${startDay3}","${startDay4}","${startDay5}","${startDay6}","${startDay7}","${startDay8}","${startDay9}","${startDay10}",
        "${startDay11}","${startDay12}","${startDay13}","${startDay14}","${startDay15}","${startDay16}","${startDay17}","${startDay18}","${startDay19}","${startDay20}",
        "${startDay21}","${startDay22}","${startDay23}","${startDay24}","${startDay25}",
        ${on_off1},${on_off2},${on_off3},${on_off4},${on_off5},${on_off6},${on_off7},${on_off8},${on_off9},${on_off10},
        ${on_off11},${on_off12},${on_off13},${on_off14},${on_off15},${on_off16},${on_off17},${on_off18},${on_off19},${on_off20},
        ${on_off21},${on_off22},${on_off23},${on_off24},${on_off25},
        "${operDes1}","${operDes2}","${operDes3}","${operDes4}","${operDes5}","${operDes6}","${operDes7}","${operDes8}","${operDes9}","${operDes10}",
        "${operDes11}","${operDes12}","${operDes13}","${operDes14}","${operDes15}","${operDes16}","${operDes17}","${operDes18}","${operDes19}","${operDes20}",
        "${operDes21}","${operDes22}","${operDes23}","${operDes24}","${operDes25}",
        "${OPL1}","${OPL2}","${OPL3}","${OPL4}","${OPL5}","${OPL6}","${OPL7}","${OPL8}","${OPL9}","${OPL10}",
        "${OPL11}","${OPL12}","${OPL13}","${OPL14}","${OPL15}","${OPL16}","${OPL17}","${OPL18}","${OPL19}","${OPL20}","${OPL21}","${OPL22}","${OPL23}","${OPL24}","${OPL25}",
        "${dateAndTime}")  `;
  // "${shift}","${dateAndTime}", "${day}")`;
  console.log("createQuery ================>", query);

  db.query(query, (err, result) => {
    if (err) {
      console.log("error form query ==>", err);
      res.status(501).json({
        message: "CIL creation failed",
        results: "Failed",
      });
    } else {
      // sendEmail(EmailData)
      // console.log("result from query ==>",result)
      res.status(200).json({
        message: "CIL Created Successfully",
        results: "Success",
      });
    }
  });
});

//api for tset set CIL

router.post("/createTestCIL", (req, res) => {

  console.log("req.body ==>", req.body);
  const areaName = req.body.areaName;
  const machineName = req.body.machineName;
  const subAssemblyName = req.body.subAssemblyName;
  const equipmentOwnerName = req.body.equipmentOwnerName;

  const subAssemblyData = req.body.subAssemblyData;
  // const subAssemblyData = JSON.parse(req.body.subAssemblyData);
  if (subAssemblyData.length <= 0) {
    return res.status(403).send({
      message: "Please add at least one CIL"
    })
  }

  console.log("subAsssmeblyData ====>", subAssemblyData);
  var isDone = 0;
  var arrayExist = [];

  // Object.entries(subAssemblyData).forEach(item => {
    db.getConnection((err, connection) => {
      if (err) {
        console.log("database not connected error: >>>", err);
        res.send(500).status({
          msg: "Error from database",
          err: err,
        });
      }
      Object.values(subAssemblyData).forEach((item) => {
        // console.log(item);
        // console.log("item.OPL",item.OPL);
        if (item.OPL === undefined || item.OPL === null || item.OPL === "") {
          var OPL = "No OPL";
        } else {
          var OPL = item.OPL;
        }
        const query = `Insert into finehhdi_CIL.SETCIL1 (areaName , machineName , subAssemblyName,equipmentOwnerName, HP, HPVariants, frequency, startDay, OPL, onOff, operDesc , submittedDate ) 
        values("${areaName}","${machineName}", "${subAssemblyName}", "${equipmentOwnerName}","${item.HP}", "${item.HPVariant}", "${item.Frequency}","${item.StartDay}","${OPL}","${item.on_off}","${item.OperDes}", "${item.submittedDate}")`;
        // values("${areaName}","${machineName}", "${subAssemblyName}", "${equipmentOwnerName}","${item.HP}", "${item.HPVariant}", "${item.Frequency}","${item.StartDay}","${item.OPL}","${item.on_off}","${item.OperDes}", "${item.submittedDate}")`;
        // console.log("query from insert ====>", query);
        connection.query(query, (err, result) => {
          connection.release();
          if (err) {
            console.log(err);
            isDone = 0;
            arrayExist.push(isDone);
            res.status(501).send()
            // res.send()
            // res.status(401).json({
            //   message: "Some error in select CIL selection",
            //   result: "Failed",
            //   data: [],
            // });
          } else {
            res.status(200).send()
            console.log("select data successfully");
            isDone = 1;
            arrayExist.push(isDone);
            // res.status(201).json({
            //   message: "select data Successfully",
            //   results: "Success",
            //   data: result,
            // });
          }
        });
      });
    })

  console.log("isDone ==>", isDone);
  console.log("arrayExist ==>", arrayExist);
  if (arrayExist.includes(1)) {
    console.log("data inserted Successfully");
    res.status(200).json({
      message: "select data Successfully",
      results: "Success",
    });
  }
});

module.exports = router;
