<?php
session_start();
if(isset($_POST["paramValues"])){
    $paramValues=json_decode($_POST["paramValues"]);
    $month=$paramValues->month;
    $year=$paramValues->year;
    $loggedInUserID = $_SESSION["userId"];
    $loggedInUserDepartment = $_SESSION['teamLeads'];
    
    $obj=new fetchFortNightEmpInfo($loggedInUserID, $loggedInUserDepartment, $month, $year);
    
}else{
     header('location: ../login.php');
}
class fetchFortNightEmpInfo{
    
    function __construct($loggedInUserID, $loggedInUserDepartment, $month, $year){
        require_once dirname(__DIR__) . '../../php/DB_Connect.php';
        $this->DB_con = new DB_conncect(); //encapsulation
        $this->db = $this->DB_con->connectToDB();
        $this->fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $month, $year);
    }
    function fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $month, $year){
        // $queryStart="SELECT  `EMP_ID`, `NAME`, `FORTNIGHT` ,`SUBMITION_STATUS`, `FROM_DATE`, `TO_DATE`, `TL_NAME`, `TL_ID`,`NO_OF_SITE_VISITS`, `EMP_FN_FINAL_AMOUNT`, `TL_FN_FINAL_AMOUNT`, `FINANCE_FN_FINAL_AMOUNT` FROM  `fort_night_claims`";
        // $queryEnd;
        // $conditionStatement;
        $departMent;
        if($loggedInUserDepartment==TL_DEPARTMENT){
            $departMent="TL";
        }else{
            $departMent="Finance";
        }
        $conditionStatement = $this->db->prepare("CALL `get_fort_night`(?,?,?,?)");
        $conditionStatement->bind_param("ssss",$loggedInUserID,$departMent,$month,$year);
        $conditionStatement->execute();
        $conditionStatement->bind_result($pendingClaims);
         $conditionStatement->store_result();
        //  $conditionStatement->store_result();
         $mainArray = array();
         $subArray=array();
              while ($row=$conditionStatement->fetch()) {
                  $mainArray["claimsCount"]=$pendingClaims;
              }
        //  $conditionStatement->next_result(); 
    if($conditionStatement->next_result()) {
        $conditionStatement->bind_result($empId, $empName, $fortnight, $submission_status, $fromDate, $toDate,$noOfSiteVisits,$empFnFinalAmount,$tlFnFinalAmount,$financeFnFinalAmount,$paidAmount,$empCircle,$perminantRejectCount);
         $conditionStatement->store_result();
        while ($row=$conditionStatement->fetch()) {
                  $return_arr["empId"]=$empId;
                  $return_arr["empName"]=$empName;
                  $return_arr["fortnight"]=$fortnight;
                  $return_arr["submission_status"]=$submission_status;
                  $return_arr["fromDate"]=$fromDate;
                  $return_arr["toDate"]=$toDate;
                  $return_arr["no_of_site_visits"]=$noOfSiteVisits;
                  $return_arr["eng_totClaimedAmt"]=number_format($empFnFinalAmount,2,'.','');
                  $return_arr["TL_totApprovedAmt"]=number_format($tlFnFinalAmount,2,'.','');
                  $return_arr["finance_totApprovedAmt"]=number_format($financeFnFinalAmount,2,'.','');
                  $return_arr["paid_amount"]=number_format($paidAmount,2,'.','');
                  $return_arr["empCircle"]=$empCircle;
                  $return_arr["perminent_reject_count"]=$perminantRejectCount;
                  
                  array_push($subArray, $return_arr);
              }
    }
    $mainArray['Fn_Claims']=$subArray;
    echo json_encode($mainArray);
    
    
    }
    
}

?>