<?php

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_config.php';


if (isset($_POST['progress'])) {
    
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

    $task_id= $_POST['task_id'];
    $employee_id= $_POST['employee_id'];
    $progress_date= $_POST['progress_date'];
    $progress= $_POST['progress'];
    $completed_activities= $_POST['completed_activities'];
    $completed_datetime= $_POST['completed_datetime'];
    $latitude=$_POST['latitude'];
    $longitude=$_POST['longitude'];
    $iamge=$_POST['camera'];
    $checkinimagepath=$task_id."_".$completed_datetime.".jpg";
    $checkinimagepath=str_replace("-","",$checkinimagepath);
    $checkinimagepath=str_replace(":","",$checkinimagepath);
    $checkinimagepath=str_replace(" ","",$checkinimagepath);
    
if($iamge!="null" && !empty($iamge)){
        
    $imageName = "../../../App/Progressimages/".$checkinimagepath;
    $imageData = base64_decode($iamge);
    $source = imagecreatefromstring($imageData);
    $rotate = imagerotate($source, $angle, 0); // if want to rotate the image
    $imageSave = imagejpeg($rotate,$imageName,100);
    imagedestroy($source);
      
    $insert_query= "INSERT INTO task_progress (task_id,employee_id,progress_date,progress,completed_activities,completed_datetime,p_latitude,p_longitude,progressImage) VALUES('$task_id','$employee_id','$progress_date','$progress','$completed_activities','$completed_datetime','$latitude','$longitude','$checkinimagepath')";
   
}else{
    $insert_query= "INSERT INTO task_progress (task_id,employee_id,progress_date,progress,completed_activities,completed_datetime,p_latitude,p_longitude) VALUES('$task_id','$employee_id','$progress_date','$progress','$completed_activities','$completed_datetime','$latitude','$longitude')";
}

   $result = mysqli_query($con,$insert_query);

    if ($result) {
        $response["s"] = 1;
    } else {
        $response["s"] = 0;
    }
    
    mysqli_close($con);
 
}
     // echoing JSON response
    echo json_encode($response);

?>