<?php
    if(isset($_POST["paramValues"])){
        $paramObj=json_decode($_POST["paramValues"]);
        $clsObj= new UpdateReportSubmissions();
        $clsObj->udpateReportSubmitionDetails($paramObj->reportSubmisionDetails);
    }
    class UpdateReportSubmissions{
        function __construct(){
            require_once dirName(__DIR__).'../../../../php/DB_Connect.php';
            $clsObj=new DB_conncect();
            $this->db=$clsObj->connectToDB();
            $this->db_Pdo=$clsObj->connectToDBVia_PDO();
        }
        function udpateReportSubmitionDetails($paramObj){
            $mainArray=array();
            $stmt=$this->getQueryStatement($paramObj);
            try{
                $this->db_Pdo->beginTransaction();
                    $executionStatus=$stmt->execute();
                $this->db_Pdo->commit();
                if(!$executionStatus){// failure
                    $mainArray["error"]=true;
                    $mainArray["message"]="Execution Failure";
                }else{
                    $noOfEffectedRows=$stmt->rowCount();
                    if(($noOfEffectedRows/2)==0){
                        $mainArray["error"]=true;
                        $mainArray["message"]="Details already Exist!";
                    }else{
                        $mainArray["error"]=false;
                        $mainArray["message"]="Succesfully ".($noOfEffectedRows/2)." Records Updated!";
                    }
                }
            }
            catch(PDOException $e){
                $mainArray["error"]=true;
                $mainArray["message"]="Internal Error 500!";
            }
            echo json_encode($mainArray);
        }
        function getQueryStatement($paramObj){
            $lastLoopIndex=count($paramObj)-1;
            $query="INSERT INTO `tesf`(`UNIQUE_ID`,`PO_NUMBER`,`REPORT_SUBMITED_TO_CUSTOMER_ON`,`REPORT_REMARK`) VALUES";
            forEach($paramObj as $loopIndex=>$currentLoopObj){
                $uniquId=$currentLoopObj->EmpId."_".$currentLoopObj->SiteId."_".$currentLoopObj->SiteVisitDate;
                if($loopIndex!=$lastLoopIndex){
                    $query.="('".$uniquId."','".$currentLoopObj->PONo."','".$currentLoopObj->reportDate."','".$currentLoopObj->{'Report Remarks'}."'),";
                }else{// last index
                     $query.="('".$uniquId."','".$currentLoopObj->PONo."','".$currentLoopObj->reportDate."','".$currentLoopObj->{'Report Remarks'}."') ";
                }
            }
            $query.=" ON DUPLICATE KEY UPDATE  `PO_NUMBER`=VALUES(`PO_NUMBER`),`REPORT_SUBMITED_TO_CUSTOMER_ON`=VALUES(`REPORT_SUBMITED_TO_CUSTOMER_ON`), `REPORT_REMARK`=VALUES(`REPORT_REMARK`)";                        
            $stmt=$this->db_Pdo->prepare($query);
            return $stmt;
        }
        
        // function getQueryStatement($paramObj){
        //     $lastLoopIndex=count($paramObj)-1;
        //     $query="INSERT INTO `site_allocations`(`SITE_ID`,`SITE_ASSIGNED_TO_EMPID`,`SITE_ASSIGNED_DATE`,`PO_NUMBER`,`REPORT_SUBMITED_TO_CUSTOMER_ON`,`REPORT_REMARK`) VALUES";
        //     forEach($paramObj as $loopIndex=>$currentLoopObj){
        //         if($loopIndex!=$lastLoopIndex){
        //             $query.="('".$currentLoopObj->SiteId."','".$currentLoopObj->EmpId."','".$currentLoopObj->SiteVisitDate."','".$currentLoopObj->PONo."','".$currentLoopObj->reportDate."','".$currentLoopObj->{'Report Remarks'}."'),";
        //         }else{// last index
        //             $query.="('".$currentLoopObj->SiteId."','".$currentLoopObj->EmpId."','".$currentLoopObj->SiteVisitDate."','".$currentLoopObj->PONo."','".$currentLoopObj->reportDate."','".$currentLoopObj->{'Report Remarks'}."')";
        //         }
        //     }
        //     $query.=" ON DUPLICATE KEY UPDATE  `PO_NUMBER`=VALUES(`PO_NUMBER`),`REPORT_SUBMITED_TO_CUSTOMER_ON`=VALUES(`REPORT_SUBMITED_TO_CUSTOMER_ON`), `REPORT_REMARK`=VALUES(`REPORT_REMARK`)";                        
        //     $stmt=$this->db_Pdo->prepare($query);
        //     return $stmt;
        // }
    }
?>