<?php 
    ini_set('display_errors', 0);
    error_reporting(E_ALL);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    require("../connect.php");
    include './funcConvertToSecond.php';
    date_default_timezone_set('Asia/Jakarta');

    $inspectionDate = strtotime($_POST['inspectiondate']); // Inspection date
	$inspectionTime = timeToSeconds($_POST['inspectiontime']); // Inspection time
	$pointMapInspection = "[map]".$_POST['latitude'].",".$_POST['longitude']."(".$_POST['nodoc'].")[/map]";
	$filename_db = time()."_"."INSPECTION".$_POST['addedby'].$_POST['blockid'].
	    $_POST['locationcategory'].$_POST['problemcategory'].date("YmdHis",time()).".png";

	$queryUploadInspection = "INSERT INTO app_entity_51 
	(parent_item_id, date_added, created_by, field_1710, 
	field_1927, field_1707, field_1706, field_1703, 
	field_1704, field_1705, field_1701, field_1702, 
	field_1708, field_1716, field_1713, field_1719, 
	field_1720, field_1718, field_1721, field_1722, 
	field_1723, field_1711, field_1712) VALUES (
	?, UNIX_TIMESTAMP(), ?, ?, ?, ?, ?, ?, ?, ?, 
	?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    
    $stmtUploadInspection = mysqli_prepare($conn, $queryUploadInspection);
    mysqli_stmt_bind_param($stmtUploadInspection, 'iiissssssiisssisisssss', 
    $_POST['divisionid'], $_POST['uploadby'], $_POST['addedby'], 
    $_POST['nodoc'], $_POST['holding'], $_POST['region'], $_POST['company'], 
    $_POST['site'], $_POST['estate'], $_POST['divisionid'], $_POST['blockid'], 
    $_POST['tphcode'], $inspectionDate, $inspectionTime, $_POST['locationcategory'], 
    $_POST['locationdetail'], $_POST['problemcategory'], $_POST['problemsubject'], $_POST['problemdetail'], 
    $_POST['followup'], $pointMapInspection, $filename_db);
    
    if (mysqli_stmt_execute($stmtUploadInspection)) {
        echo json_encode(array("UPLOADINSPECTION"=>"SUCCESS"));
        
        $stmtImgInspection = mysqli_query($conn, "SELECT field_1712 FROM app_entity_51 WHERE id = ".mysqli_insert_id($conn).";");
        $rowImgInspection = mysqli_fetch_assoc($stmtImgInspection);
        
        if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m"))) {
            mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m"));
            if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) {
                mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"));
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".time()."_".$rowImgInspection['field_1712'], "w+");
                fwrite($fp, base64_decode($_POST['imginspection']));
            } else if (is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) { 
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".time()."_".$rowImgInspection['field_1712'], "w+");
                fwrite($fp, base64_decode($_POST['imginspection']));
            }
        } else if (is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m"))) {
            if (!is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) {
                mkdir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"));
                // Upload image
               $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".time()."_".$rowImgInspection['field_1712'], "w+");
                fwrite($fp, base64_decode($_POST['imginspection']));
            } else if (is_dir("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d"))) { 
                // Upload image
                $fp = fopen("../../../sls/uploads/attachments/".date("Y")."/".date("m")."/".date("d")."/".time()."_".$rowImgInspection['field_1712'], "w+");
                fwrite($fp, base64_decode($_POST['imginspection']));
            }
        }
        
    }
  
?>