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

router.post('/createOD',(req,res)=>{
    console.log("Body =====================>", req.body);
    const subAssembly = req.body.subAssemblyName;
    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 || "";

    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 = `INSERT into finehhdi_CIL.SetOperationDescription 
        (subAssembly,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) 
        values("${subAssembly}","${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}")`
        console.log("query from setOperationDescription ===================>",query)
        connection.query(query,(err,data)=>{
            connection.release();
            if (err) {
                console.log("error ==>", err);
                if (err.code === "ER_DUP_ENTRY") {
                    res.status(409).json({
                        message: "Duplicate Entry",
                        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: "Operation description creation failed",
                        results: "Failed",
                    });
                }
            }
            else {
                res.status(200).json({
                    message: "Operation description created Successfully",
                    results: "Success",
                });
            }
        })
    })

})


module.exports = router;