<?php

require_once __DIR__ . '/db_config.php';

$r = array();

if (isset($_POST["id"]) ) {

$report_id=$_POST["id"];
$type=$_POST["type"];
$cname=$_POST["cname"];
$data=$_POST["data"];
$name=$_POST["name"];
$number=$_POST["number"];

date_default_timezone_set("Asia/Calcutta");   //India time (GMT+5:30)
$datetime=date('Y-m-d H:i:s');

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

date_default_timezone_set('Asia/Calcutta');
$imageFolder="defectreport_images";
    
    $year=date('Y');
    $month=date('m');
    
    //dynamic folders creation starts
   
    $folder = "../../../$imageFolder";
    if(!file_exists($folder)){
        mkdir($folder);
    }
                
    $folder = "../../../$imageFolder/$year/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
                
    $folder = "../../../$imageFolder/$year/$month/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $folder = "../../../$imageFolder/$year/$month/";
    if(!file_exists($folder)){
        mkdir($folder);
    }
    
    $datetime=date('YmdHis');

    //Dynamic folder creation ends
    $imageName = $folder.$travelId.$datetime.".jpg";
    
    if(file_exists($imageName)){
        $imageName = $folder.$travelId.$datetime."1.jpg";
    }
    
    $data = addslashes($data);
    $imageData = base64_decode($data);
    $source = imagecreatefromstring($imageData);
    $rotate = imagerotate($source, $angle, 0); // if want to rotate the image
    $imageSave = imagejpeg($rotate,$imageName,100);
    imagedestroy($source);
    
    $fileLocation = str_replace("../","",$imageName);

$query = "INSERT INTO defect_report_image(report_id,image_path,file_type,datetime,type,incharge_name,incharge_number,companyname) VALUES('$report_id','$fileLocation','jpg','$datetime','$type','$name','$number','$cname')";
$result=mysqli_query($con,$query);

if($result){ $r["s"]=1; }
else{ $r["s"]=2; }

mysqli_close($con);

}

echo json_encode($r,JSON_UNESCAPED_SLASHES);


?>