<?php
session_start();
if(isset($_POST['paramValues'])){

$paramObj=json_decode($_POST['paramValues']);
$loggedInUserID = $_SESSION['userId'];
// $empId=$paramObj->empId;
// $fortNight=$paramObj->fortNight;
// $TLName=$paramObj->TLName;
// $TLId=$paramObj->TLId;

$obj=new updateTLID();
$obj->updateTLName_Id($paramObj, $loggedInUserID);

}else {
  header('location: ../../login.php');
}
class updateTLID
{
     function __construct()
     {

          require_once dirname(__DIR__) . '../../php/DB_Connect.php';
          $DBclassObject = new DB_conncect(); //encapsulation
          $this->db = $DBclassObject->connectToDB();
          $this->db_Pdo = $DBclassObject->connectToDBVia_PDO();
     }

     function updateTLName_Id($paramObj, $loggedInUserID)
     {
          $result;
          $fortNightQuery = $this->db->prepare("UPDATE `fort_night_claims` SET `TL_NAME`=? , `TL_ID`=?  WHERE `EMP_ID`=? AND `FORTNIGHT`=?");
          $fortNightQuery->bind_param('ssss',$paramObj->TLName, $paramObj->TLId, $paramObj->empId, $paramObj->fortNight);
          
          $tesf2Query =$this->db->prepare("UPDATE `tesf-2` SET  `TL_NAME`=?, `TL_EMP_ID`=? WHERE `ENGINEER_EMP_ID`=? AND `SITE_VISIT_DATE` BETWEEN ? AND ? AND `TL_EMP_ID`=?");
          $tesf2Query->bind_param('ssssss',$paramObj->TLName, $paramObj->TLId, $paramObj->empId, $paramObj->fromDate, $paramObj->toDate, $loggedInUserID);
          
            //$arrayResponse=array();
        try{
            $this->db_Pdo->beginTransaction();
                $fortNightQueryExecutionStatus=$fortNightQuery->execute();
                $tesf2QueryExecutionStatus=$tesf2Query->execute();
                if($fortNightQueryExecutionStatus && $tesf2QueryExecutionStatus){
                    $result="UPDATED";
                } else {
                  $result="FAILURE";
                }
            $this->db_Pdo->commit();
        } catch (PDOException $e) {
                    // roll back the transaction if something failed
                    $this->db_Pdo->rollback();
                    $result="FAILURE";
        }
         
           echo  json_encode($result);
     }
}

