<?php

ini_set ("display_errors", "0");
error_reporting(E_ALL);
require("../connect.php");
include './funcConvertToSecond.php';
date_default_timezone_set('Asia/Jakarta');

$sqlcheck = "SELECT * FROM app_entity_86 WHERE field_1837 = '" . $_POST['nodoc'] . "';";

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

if ($checkresult->num_rows == 0) {
    $stmtChoiceUOM = mysqli_query($conn, "SELECT id FROM app_global_lists_choices WHERE name = '" . $_POST['uom'] . "' AND lists_id = 8;");
    $rowChoiceUOM = mysqli_fetch_assoc($stmtChoiceUOM);

    // Preparing datetime and img filename
    $verifdate = strtotime($_POST['inputdate']);
    $upkeepdate = strtotime($_POST['upkeepdate']);
    $timeupkeep = timeToSeconds($_POST['inputtime']);
    $pointMapVMR = "[map]" . $_POST['latupkeep'] . "," . $_POST['longupkeep'] . "(" . $_POST['nodoc'] . ")[/map]";
    $filename_db = time() . "_" . "RWT" . $_POST['addedby'] . $_POST['tphid'] . $_POST['empid'] . date("YmdHis", time()) . ".png";

    // Preparing insert query
    $queryInsertVMR = "INSERT INTO app_entity_86 (parent_item_id, date_added,
    created_by, field_1824, field_1837, field_1812, field_1829, field_1833,
    field_1834, field_1810, field_1811, field_1820, field_1818, field_1832,
    field_1822, field_1823, field_1835, field_1836, field_1825, field_1826,
    field_1827, field_1828) VALUES (?, UNIX_TIMESTAMP(), ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    // Sample input
    // parent_item_id, tphid, 35178
    // created_by, uploadby, 39
    // field_1824, addedby, 118
    // field_1837, nodoc, RWT/DC-I/40291022/131321
    // field_1812, $upkeepdate, 1666976400
    // field_1829, $timeupkeep, 793
    // field_1833, $verifdate, 1666976400
    // field_1834, worktype, 149
    // field_1810, divisionid, 9
    // field_1811, block, 37
    // field_1820, tphid, 35178
    // field_1818, empid, 126
    // field_1832, $rowChoiceUOM['id'], 232
    // field_1822, activityid, 26231947
    // field_1823, activityresult, 0.2
    // field_1835, materialid, 11747
    // field_1836, materialqty, l0 BUAH
    // field_1825, latupkeep, 4.270796666666667
    // field_1826, longupkeep, 98.151793333333
    // field_1827, $pointMapVMR, [map]4.670796666666667,98.151793333333(RWT/DC-I/40291022/131321)[/map]
    // field_1828, $filename_db, 981667171337_RWT12951781620221031060838.png

    // base64 for photo
    // upkeepimg,
    $stmtInsertVMR = mysqli_prepare($conn, $queryInsertVMR);
    mysqli_stmt_bind_param(
        $stmtInsertVMR,
        'iiissssiiiiiiisssssss',
        $_POST['tphid'],
        $_POST['uploadby'],
        $_POST['addedby'],
        $_POST['nodoc'],
        $upkeepdate,
        $timeupkeep,
        $verifdate,
        $_POST['worktype'],
        $_POST['divisionid'],
        $_POST['blockid'],
        $_POST['tphid'],
        $_POST['empid'],
        $rowChoiceUOM['id'],
        $_POST['activityid'],
        $_POST['activityresult'],
        $_POST['materialid'],
        $_POST['materialqty'],
        $_POST['latupkeep'],
        $_POST['longupkeep'],
        $pointMapVMR,
        $filename_db
    );

    if (mysqli_stmt_execute($stmtInsertVMR)) {
        echo json_encode(array("UPLOADUPKEEPACT" => "SUCCESS"));

        $stmtPhotoUpkeep = mysqli_query($conn, "SELECT field_1828 FROM app_entity_86
		WHERE field_1837 = '" . $_POST['nodoc'] . "' ORDER BY id DESC LIMIT 1;");
        $rowPhotoUpkeep = mysqli_fetch_assoc($stmtPhotoUpkeep);

        if (!is_dir("../uploads/attachments/" . date("Y"))) {
            mkdir("../uploads/attachments/" . date("Y"));
        }

        if (!is_dir("../uploads/attachments/" . date("Y") . "/" . date("m"))) {
            mkdir("../uploads/attachments/" . date("Y") . "/" . date("m"));
        }

        if (!is_dir("../uploads/attachments/" . date("Y") . "/" . date("m") . "/" . date("d"))) {
            mkdir("../uploads/attachments/" . date("Y") . "/" . date("m") . "/" . date("d"));
        }

        // Upload image
        $fp = fopen("../uploads/attachments/" . date("Y") . "/" . date("m") . "/" . date("d") . "/" . $rowPhotoUpkeep['field_1828'], "w+");
        fwrite($fp, base64_decode($_POST['upkeepimg']));

        mysqli_close($conn);
    }
} else {
    echo json_encode(array("UPLOADUPKEEPACT" => "SUCCESS"));
    mysqli_close($conn);
}
