<?php
require("../connect.php");
include './funcConvertToSecond.php';
date_default_timezone_set('Asia/Jakarta');

$sqlcheck  = " SELECT * FROM app_entity_62 WHERE ";
$sqlcheck .= " field_1209 = '".$_POST['nodoc'].";'";

$checkresult = $conn->query($sqlcheck);

if ($checkresult->num_rows == 0) {

    // Preparing datetime and img filename
    $verifdate = strtotime($_POST['inputdate']);
    $harvestdate = strtotime($_POST['harvestdate']);
    $timeharvest = timeToSeconds($_POST['inputtime']);
    $pointMapVMP = "[map]".$_POST['latvmp'].",".$_POST['longvmp']."(".$_POST['tphcode'].")[/map]";
    $filename_db = time()."_"."VMP".$_POST['addedby'].$_POST['harvester'].$_POST['tphid'].date("YmdHis",time()).".png";
    
    // Preparing insert query
    $queryInsertVMP = "INSERT INTO app_entity_62 (parent_item_id, date_added, 
    created_by, field_906, field_909, field_910, field_900, field_1287, 
    field_1127, field_1209, field_2014, field_907, field_911, field_912, 
    field_985, field_913,  field_973, field_974, field_975, field_914, 
    field_915, field_916, field_917) VALUES (?, UNIX_TIMESTAMP(), 
    ?, ? ,? ,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?)";
    
    $stmtInsertVMP = mysqli_prepare($conn, $queryInsertVMP);
    mysqli_stmt_bind_param($stmtInsertVMP, 'iiiisssssiississsssss', $_POST['tphid'], 
    $_POST['uploadby'], $_POST['addedby'], $_POST['tphid'], $_POST['tphqr'], 
    $harvestdate, $verifdate, $timeharvest, $_POST['nodoc'], 
    $_POST['worktype'], $_POST['harvester'], $_POST['buahtinggal'], $_POST['luaspanen'], 
    $_POST['pokoksample'], $_POST['busuktdkpotong'], $_POST['brodtinggal'], 
    $_POST['pelepahsusun'], $_POST['latvmp'], $_POST['longvmp'], $pointMapVMP, $filename_db);

    if (mysqli_stmt_execute($stmtInsertVMP)) { // If success, response back to android app
        echo json_encode(array("UPLOADVMP"=>"SUCCESS"));
        
        $lastInsertID = mysqli_insert_id($conn); // Getting last ID
        
        $stmtImgVMP = mysqli_query($conn, "SELECT field_917 FROM app_entity_62 WHERE id = ".$lastInsertID.";");
        $rowImgVMP = mysqli_fetch_assoc($stmtImgVMP);
        
        $stmtValuesVMP = mysqli_query($conn, "SELECT id, parent_item_id, field_418 
        FROM app_entity_34 WHERE id = (SELECT parent_item_id FROM app_entity_43 WHERE id = ".$_POST['tphid'].");");
        $rowValuesVMP = mysqli_fetch_assoc($stmtValuesVMP);
        
        // Inserting to values app_entity_62_values
        mysqli_query($conn, "INSERT INTO app_entity_62_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 906, ".$_POST['addedby'].");");
        mysqli_query($conn, "INSERT INTO app_entity_62_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 909, ".$_POST['tphid'].");");
        mysqli_query($conn, "INSERT INTO app_entity_62_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 2014, ".$_POST['worktype'].");");
        mysqli_query($conn, "INSERT INTO app_entity_62_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 907, ".$_POST['harvester'].");");
        mysqli_query($conn, "INSERT INTO app_entity_62_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 899, ".$rowValuesVMP['id'].");");
        mysqli_query($conn, "INSERT INTO app_entity_62_values (items_id, fields_id, value) VALUES (".$lastInsertID.", 898, ".$rowValuesVMP['parent_item_id'].");");
        
        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")."/".$rowImgVMP['field_917'], "w+");
                fwrite($fp, base64_decode($_POST['vmpimg']));
            } 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")."/".$rowImgVMP['field_917'], "w+");
                fwrite($fp, base64_decode($_POST['vmpimg']));
            }
        } 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")."/".$rowImgVMP['field_917'], "w+");
                fwrite($fp, base64_decode($_POST['vmpimg']));
            } 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")."/".$rowImgVMP['field_917'], "w+");
                fwrite($fp, base64_decode($_POST['vmpimg']));
            }
        }
		
		mysqli_close($conn);
    }
    
} else if ($checkresult->num_rows > 0) {
	echo json_encode(array("UPLOADVMP"=>"SUCCESS"));
	mysqli_close($conn);
}
?>