// const { id } = require('date-fns/locale');
var express=require('express')
const router = express.Router();
const db = require("../../utils/database");
const moment = require('moment')

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

    const username   = req.body.username;
    const userEmail   = req.body.userEmail;
    const C_msg      = req.body.C_msg;
    const C_time     = moment().format('DD/MM/YY');   
    const isResolved = req.body.isResolved;
    // const C_Number   = req.body.C_Number;
    const forOfs = req.body.forOfs
    const forBos = req.body.forBos
    const forDH = req.body.forDH

    let query = `INSERT INTO finehhdi_fineAD.complain (username,userEmail,C_msg,C_time,isResolved,forOfs,forBOS,forDH)
    VALUES ("${username}","${userEmail}","${C_msg}","${C_time}",${isResolved},${forOfs},${forBos},${forDH})`;

    console.log("query froom insert complain",query);

    db.query(query,(err,data)=>{
        if(err){
            console.log("eroor==>",err)
            res.status(500).send({msg:"Error while Inserting"})
        }
        else{
            res.status(200).send({msg:"Inserted Successfully"})
            let query = `select * from finehhdi_fineAD.complain`
            console.log("query from else",query);
            db.query(query,(err,data)=>{
                if(err){
                    console.log("error from else block",err);
                }
                else{

                    let query =   ` update finehhdi_fineAD.complain set C_Number = id where userEmail="${userEmail}" `
                    console.log(query);
                    db.query(query,(err,data)=>{
                        if(err){
                            console.log("error while updating c Number",err);

                        
                        }
                        else{
                            // res.status(500).send({msg:"successfully"})
                            console.log("sucess");
                        }
                    })
                }
            })
        }
    })

})


module.exports = router;