<?php
// array for JSON response
$response = array();
$response["list"] = array();

require_once __DIR__ . '/db_config.php';

if (isset($_POST['JSON'])) {
    $JSON_Received = $_POST['JSON'];
    $obj = json_decode($JSON_Received, true);                      
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
    
    foreach($obj as $key => $value )  {  

        $companyname = $value['companyname'];
        $emailid= $value['emailid'];
        $date= $value['date'];
        $checkintime= $value['checkintime'];
        $checkouttime= $value['checkouttime'];
        $checkinlocation= $value['checkinlocation'];
        $checkoutlocation= $value['checkoutlocation'];
        $status= $value['status'];
        $totaltime= $value['totaltime'];
        $checkinimage= $value['checkinimage'];
        $checkinimagepath= $value['checkinimagepath'];
        $checkinsignature= $value['checkinsignature'];
        $checkinsignaturepath= $value['checkinsignaturepath'];
        $id=$value['_id'];
        $facility_id = $value['facility_id'];
        if( strcmp($value['totaltime'],"") == 0 ){
            $totaltime=  "00:00:00" ;
        }

        $imageName = "../../../App/Attendanceimages/".$checkinimagepath;
        $imageData = base64_decode($checkinimage);
        $source = imagecreatefromstring($imageData);
        $rotate = imagerotate($source, $angle, 0); // if want to rotate the image
        $imageSave = imagejpeg($rotate,$imageName,100);
        imagedestroy($source);

        $imageName2 = "../../../App/Attendanceimages/".$checkinsignaturepath;
        $imageData2 = base64_decode($checkinsignature);
        $source2 = imagecreatefromstring($imageData2);
        $rotate2 = imagerotate($source2, $angle, 0); 
        $imageSave2 = imagejpeg($rotate2,$imageName2,100);
        imagedestroy($source2);
        
        $facility_id_out='NA';
        
        $query="INSERT INTO attendance(companyname, emailid, date, checkintime, checkouttime, checkinlocation, checkoutlocation, status, totaltime,checkinimage,checkinimagepath,checkinsignature,checkinsignaturepath,facility_id,facility_id_out) VALUES ('$companyname', '$emailid', '$date', '$checkintime', '$checkouttime', '$checkinlocation', '$checkoutlocation', '$status', '$totaltime','" . mysql_escape_string(file_get_contents($imageName)) . "','$checkinimagepath','" . mysql_escape_string(file_get_contents($imageName2)) . "','$checkinsignaturepath','$facility_id','$facility_id_out')";
        
       /* echo "INSERT INTO attendance(companyname, emailid, date, checkintime, checkouttime, checkinlocation, checkoutlocation, status, totaltime,checkinimage,checkinimagepath,checkinsignature,checkinsignaturepath,facility_id,facility_id_out) VALUES ('$companyname', '$emailid', '$date', '$checkintime', '$checkouttime', '$checkinlocation', '$checkoutlocation', '$status', '$totaltime',null,'$checkinimagepath',null,'$checkinsignaturepath','$facility_id','$facility_id_out')";
       */
       
        $result = mysqli_query($con,$query);
        if($result){
            array_push($response["list"],$id);
        }
    }
    mysqli_close($con);
}
echo json_encode($response);
?>