<?php
// session_start();
// if(isset($_SESSION['userId'])){
    if(isset($_POST['paramValues'])){
        $paramObj=json_decode($_POST['paramValues']);
        $month=$paramObj->month;
        $year=$paramObj->year;
        $tlId=$paramObj->tlId;
        $classObj=new DashBoard();
        $classObj->getDashBoard($month, $year, $tlId);
        
    }else{
        echo "no result found";
    }
// }else{
//     header('location: ../../login.php');
// }

class DashBoard{
    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 getDashBoard($month,$year,$tlId){
        $returnResponseArray=array();
        $queryDataArray=array();
        $mainArray=array();
        $resultRelatedTitle=array('fortNightClaimsStatusCount','dayClaimsCount',
                                  'siteVisitsCount','fortNightClaimsAmount',
                                  'circleWiseSiteVisitCount','circleWiseProjectsCount',
                                  'allfortNightClaims', 'allEmployeesInfo',
                                  'newEmployeesInfo','TLInfo');
        $query=$this->db->prepare("CALL `dash_board_TL`(?,?,?)");
        $query->bind_param("sss",$month,$year,$tlId);
        try{
            $this->db_Pdo->beginTransaction();
                $queryExecutionResult=$query->execute();
            $this->db_Pdo->commit();
            if(!$queryExecutionResult){
                $returnResponseArray['message']="FAILURE";
            }else{
                 $returnResponseArray['message']="success";
            }   
            $loopCount=0;
            do{
                $result=$query->get_result();
                if($result){
                    $tempArray=array();//every loop new array;
                    switch($loopCount){
                        case query_fortNightClaimsStatusCount: //0 box-dashBord  
                                while($data=$result->fetch_assoc()){
                                    $tempArray[$data["EmpCircle"]][$data["SubmitionStatus"]]=$data["StatusCount"];
                                    $tempArray[$data["EmpCircle"]]["tl_perminent_reject"]+=intval($data["tl_perminent_reject"]);
                                    $tempArray[$data["EmpCircle"]]["finance_perminent_reject"]+=intval($data["finance_perminent_reject"]);
                                }
                                $queryDataArray=$tempArray;
                                break;
                        case query_dayClaimsCount: //1 box-dashBord 
                                while($data=$result->fetch_assoc()){
                                    $tempArray[$data["EmpCircle"]]["pending_at_EMP"]=intval($data["pending_at_EMP"]);
                                    $tempArray[$data["EmpCircle"]]["pending_at_TL"]=intval($data["pending_at_TL"]);
                                    $tempArray[$data["EmpCircle"]]["pending_at_finance"]=intval($data["pending_at_finance"]);
                                    $tempArray[$data["EmpCircle"]]["TL_reject"]=intval($data["TL_reject"]);
                                    $tempArray[$data["EmpCircle"]]["finance_approved"]=intval($data["finance_approved"]);
                                    $tempArray[$data["EmpCircle"]]["finance_reject"]=intval($data["finance_reject"]);
                                    $tempArray[$data["EmpCircle"]]["paid"]=intval($data["paid"]);
                                    $tempArray[$data["EmpCircle"]]["tl_perminent_reject"]=intval($data["tl_perminent_reject"]);
                                    $tempArray[$data["EmpCircle"]]["finance_perminent_reject"]=intval($data["finance_perminent_reject"]);
                                }
                                $queryDataArray=$tempArray;
                                break;
                        case query_siteVisitsCount:// 2 line-chart
                                while($data=$result->fetch_assoc()){
                                     $tempArray[$data["EmpCircle"]][$data["MonthValue"]]=intval($data["siteVisitCount"]);
                                }
                                $queryDataArray=$tempArray;
                                break;
                        case query_fortNightClaimsAmount: //3 pie-chart
                                while($data=$result->fetch_assoc()){
                                    $tempArray[$data["EmpCircle"]]["EmpClaimedAmount"]=intval($data["EmpClaimedAmount"]);
                                    $tempArray[$data["EmpCircle"]]["TotalTLApprovedAmount"]=intval($data["TotalTLApprovedAmount"]);
                                    $tempArray[$data["EmpCircle"]]["TotalFinanceApprovedAmount"]=intval($data["TotalFinanceApprovedAmount"]);
                                    $tempArray[$data["EmpCircle"]]["PaidAmount"]=intval($data["PaidAmount"]);
                                    
                                }
                                $queryDataArray=$tempArray;
                                break;
                        case query_circleWiseSiteVisitCount:// 4 colum-chart
                                while($data=$result->fetch_assoc()){
                                    $tempArray[$data["SiteCircle"]]["Circle"]=$data["SiteCircle"];
                                    $tempArray[$data["SiteCircle"]]["NoOfSiteVisits"]=intval($data["NoOfSiteVisits"]);
                                    $tempArray[$data["SiteCircle"]]["RegularVisits"]=intval($data["RegularVisits"]);
                                    $tempArray[$data["SiteCircle"]]["Revisits"]=intval($data["Revisits"]);
                                }
                                $queryDataArray=$tempArray;
                                break;
                        case query_circleWiseProjectsCount:// 5 project table
                                while($data=$result->fetch_assoc()){
                                    $tempArray[$data["SiteCircle"]][$data["Project"]]=$data["ProjectCount"];
                                }
                                $queryDataArray=$tempArray;
                                break;
                        case query_allfortNightClaims:// 6 table
                                while($data=$result->fetch_assoc()){
                                        if(isset($tempArray[$data["EMP_CIRCLE"]])){
                                            array_push($tempArray[$data["EMP_CIRCLE"]],$data);
                                        }else{
                                            $tempArray[$data["EMP_CIRCLE"]]=[];
                                            array_push($tempArray[$data["EMP_CIRCLE"]],$data);
                                        }
                                    }
                                $queryDataArray=$tempArray;
                                break;
                        case query_allEmployeesInfo:
                            while($data=$result->fetch_assoc()){
                                 array_push($queryDataArray,$data);
                            }
                            break;
                        case query_newEmployeesInfo:
                             while($data=$result->fetch_assoc()){
                                    $tempArray[$data["EMP_ID"]]=$data;
                                }
                                 $queryDataArray=$tempArray;
                            break;
                        case query_tlInfo:
                             while($data=$result->fetch_assoc()){
                                 
                                 //isset($tempArray[$data["TL_NAME"]."_".$data["TL_ID"]]) ? "":isset($tempArray[$data["TL_NAME"]."_".$data["TL_ID"]])=[];
                                 
                                    if(isset($tempArray[$data["TL_NAME"]."_".$data["TL_ID"]])){
                                        array_push($tempArray[$data["TL_NAME"]."_".$data["TL_ID"]],$data["CIRCLE"]);
                                    }else{
                                        $tempArray[$data["TL_NAME"]."_".$data["TL_ID"]]=[];
                                         array_push($tempArray[$data["TL_NAME"]."_".$data["TL_ID"]],$data["CIRCLE"]);
                                    }
                                }
                                 $queryDataArray=$tempArray;
                            break;
                        default:
                                while($data=$result->fetch_assoc()){
                                     array_push($queryDataArray,$data);
                                }
                                break;
                    }
                    if($resultRelatedTitle[$loopCount]){
                        $mainArray[$resultRelatedTitle[$loopCount]]=$queryDataArray;
                    }else{
                        $mainArray["default"]=$queryDataArray;
                    }
                        $queryDataArray=array();// clear temp array
                }
                $loopCount++; 
            }while($query->next_result());// check for next query result ;
            $returnResponseArray['responseData']=$mainArray;
        }
        catch (PDOException $e) {
            $this->db_Pdo->rollback();
            $returnResponseArray['message']="FAILURE";
        }
        echo json_encode($returnResponseArray);
        
    }
    
}


?>