var express = require("express");
const router = express.Router();
const db = require("../../utils/database");
const dbFuntion = require("../../utils/dbfunctions");
const bcrypt = require("bcrypt");
const { response } = require("express");
var jwt = require("jsonwebtoken");
const { json } = require("body-parser");
const moment = require('moment')

router.get("/getUsers", (req, res) => {
  var getUsersQuer = `Select * From finehhdi_fineAD.users Order By id DESC`;
  let objData = [];
  db.query(getUsersQuer, (err, data) => {
    if (err) {
      console.log(err);
      res.json({ err, error: "Users Not Found" });
    } else {
    //   data.map((item) => {
    //     const { password, ...other } = item;
    //     objData.push(other);
    //   });
    //   res.status(201).json(objData);
    res.status(201).json(data)
    }
  });
});

router.get("/selectUser", (req, res) => {
  // var username = req.body.username;
  //   console.log(email, password);

  db.query(
    // `select isAdmin from users where username="${username}"`,
    `select * from finehhdi_fineAD.users ORDER BY id desc`,
    (err1, data) => {
      if (err1) {
        console.log(err1);
        res.json({
          error: err1,
        });
      } else {
        console.log("get request success");
        res.status(200).json({
          data: data,
        });
      }
    }
  );
});

//individual data
router.post("/userIndividualData", (req, res) => {
  var id = req.body.id;
  var date = moment(new Date).format('DD-MM-YYYY')
  console.log("date from individual request ==>", date)

  // var query = `SELECT * FROM users WHERE id = ${id}`;
  var query = `SELECT * FROM finehhdi_fineAD.users WHERE id = ${id} AND DATE_FORMAT(STR_TO_DATE(ofsDate,'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE("${date}",'%d-%m-%Y'),'%m-%Y')`;
  console.log(query)
  db.query(query, (err, data) => {
    if (err) {
      console.log("error from individual data request");
      res.status(500).send({
        message: "req failed!",
      });
    } else {
      res.status(200).send({
        message: "get successfully",
        data: data,
      });
    }
  });
});
//for indiviual
router.post("/userIndividualDatabyID", (req, res) => {
  var id = req.body.id;
  if(!id || id ===undefined){
    return
  }
  //creating array to push value inside
  let objData = [];
 
  var query = `SELECT * FROM finehhdi_fineAD.users WHERE id = ${id}`;
  console.log(query)
  db.query(query, (err, data) => {
    if (err) {
      console.log("error from individual data request");
      console.log(err);
      res.status(500).send({
        message: "req failed!",
        
      });
    } else {
      data.map((item) => {
        const { password, ...other } = item;
        objData.push(other);
      });
      res.status(200).send({
          message: "get successfully",
        data: objData,
      });
    }
  });
});


module.exports = router;



