<?php
session_start();
if(isset($_POST["paramValues"])){
  $paramObj=json_decode($_POST['paramValues']);
 
 //echo json_encode($empInfoArray);
 $obj=new EmployeeInfo();
 $todayDate=date("Y-m-d");
 $userNameID= $_SESSION['userName'].'('.$_SESSION["userId"].')';
 
     if($paramObj->queryType=="insert"){
        $obj->insertOperations($paramObj,$userNameID,$todayDate);
     }elseif($paramObj->queryType=="update"){
        $obj->updateOperations($paramObj,$userNameID,$todayDate);
     }
 
}

class EmployeeInfo{
    
    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 insertOperations($paramObj,$userNameID,$todayDate){
        $mainArray=array();
        
          //insert in empcircleTable
          $empCirclequery=$this->db->prepare("INSERT INTO `empCircle`(`EMP_ID`, `EMP_NAME`, `TL_NAME`, `TL_EMP_ID`, `CIRCLE`, `LEFT_FROM_ORGANIZATION`, `CREATED_DATE`, `CREATED_BY`) VALUES(?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE `TL_NAME`=VALUES(`TL_NAME`),`TL_EMP_ID`=VALUES(`TL_EMP_ID`), `CIRCLE`=VALUES(`CIRCLE`)");
          $empCirclequery->bind_param('ssssssss',$paramObj->empId,$paramObj->empName,$paramObj->TLName ,$paramObj->TLId,$paramObj->empCircle, $paramObj->leftOrg, $todayDate, $userNameID);
          
           //update in fortnightClaims Table
          $fortNightQuery=$this->db->prepare("UPDATE `fort_night_claims` SET `TL_NAME`=?, `TL_ID`=?,`EMP_CIRCLE`=?  WHERE EMP_ID=? ");
          $fortNightQuery->bind_param('ssss',$paramObj->TLName,$paramObj->TLId,$paramObj->empCircle, $paramObj->empId);
          
          //update in tesf table
          $tesfQuery=$this->db->prepare("UPDATE `tesf` SET `EMP_CIRCLE`=? WHERE `EMP_ID`=?");
          $tesfQuery->bind_param('ss',$paramObj->empCircle,$paramObj->empId);
        try{
            $this->db_Pdo->beginTransaction();
            $empQueryExecutionResult=$empCirclequery->execute();
            $FNQueryExecutionResult=$fortNightQuery->execute();
            $tesdQueryExecutionResult=$tesfQuery->execute();
            $this->db_Pdo->commit();
             if($empQueryExecutionResult && $FNQueryExecutionResult && $tesdQueryExecutionResult){
                $mainArray["message"]="INSERTED";
            }else{
                 $mainArray["message"]="FAILURE";
            }
        }
        catch(PDOException $e){
            $this->db_Pdo->rollback();
             $mainArray["message"]="FAILURE";
        }
        
        echo json_encode($mainArray);
        
    }
    
    function updateOperations($paramObj,$userNameID,$todayDate){
        $mainArray=array();

        $query=$this->db->prepare("UPDATE `empCircle` SET `TL_NAME`=?, `TL_EMP_ID`=?, `CIRCLE`=?, `LEFT_FROM_ORGANIZATION`=?, `UPDATED_DATE`=?, `UPATED_BY`=? WHERE `EMP_ID`=?");
        $query->bind_param('sssssss',$paramObj->TLName,$paramObj->TLId,$paramObj->empCircle,$paramObj->leftOrg, $todayDate, $userNameID, $paramObj->empId);
        try{
            $this->db_Pdo->beginTransaction();
            $queryExecutionResult=$query->execute();
            $this->db_Pdo->commit();
            if($queryExecutionResult){
             $mainArray["message"]="UPDATED";
            }
        }
        catch(PDOException $e){
            $this->db_Pdo->rollback();
             $mainArray["message"]="FAILURE";
        }
        
        echo json_encode($mainArray);
        
    }
    
    
}

?>