<?php
session_start();
include_once("../../../../../lib/konfig.php");
include_once("../../../../../lib/function.php");
$MySQLi = new MySQLi($strDBHostName, $strDBUsername, $strDBPassword, $strDBName);
$stsEdit = 0;
if (isset($_POST['simpan'])) {
	if (isset($_FILES['strNmFile']) && $_FILES['strNmFile']['name'] <> "" && $_FILES['strNmFile']['error'] == UPLOAD_ERR_OK) {
		$errors = [];
		$allowed_types = ['image/jpeg', 'image/jpg', 'image/png', 'image/bmp'];
		$file_type = $_FILES['strNmFile']['type'];
		$file_size = $_FILES['strNmFile']['size'];
		$file_tmp = $_FILES['strNmFile']['tmp_name'];
		$file_ext = strtolower(pathinfo($_FILES['strNmFile']['name'], PATHINFO_EXTENSION));

		if (!in_array($file_type, $allowed_types))
			$errors[] = "Only JPG, JPEG, PNG, dan BMP format that allowed.";
		if (empty($errors)) {
			$strNmFile = MD5(uniqid() . date("Y-m-d H:i:s")) . "." . $file_ext;
			$destination = '../../../assets/images/lpbj_pic/' . $strNmFile;
			$thumb_destination = '../../../assets/images/lpbj_pic/thumb_' . $strNmFile;
			$strNmFile = "strNmFile='" . $strNmFile . "', ";

			$quality = 90;
			while (true) {
				compressImage($file_tmp, $destination, $quality);
				if (filesize($destination) <= 200000 || $quality <= 10)
					break;
				$quality -= 10;
			}
			createThumbnail($destination, $thumb_destination, 200);
		} else {
			foreach ($errors as $error)
				echo $error . "<br>";
			die("<a href='../../../index.php?menu=edit-lpbj-item&lpbj=" . $_POST['strKdLPBJ'] . "&id=" . $_POST['ID'] . "'>back</a>");
		}
	} else {
		$strNmFile = "";
	}

	$strEmail = $_SESSION['strEmail'];

	//die($_POST['strDeskripsi'.$i]);
	$strUnit = "";
	if ($_POST['strUnit'] == "")
		$strUnit = "PCS";
	else $strUnit = $_POST['strUnit'];

	if (strpos($_POST['strDeskripsi'], '_') !== false) {
		list($ID, $strDeskripsi) = explode("_", $_POST['strDeskripsi']);
	} else {
		$strDeskripsi = $_POST['strDeskripsi'];
	}
	$query = "SELECT * 
			FROM item 
			WHERE strDeskripsi='" . $strDeskripsi . "' AND 
					(stsAktif BETWEEN 1 AND 2)
			";
	$result = $MySQLi->query($query);
	if ($result->num_rows == 0) {
		$strKdItem = MD5(date('Y-m-d H:i:s') . $_POST['strDeskripsi']);
		$query = "INSERT INTO item 
				SET strKdItem='" . $strKdItem . "', 
					strDeskripsi='" . $_POST['strDeskripsi'] . "', 
					strUnit='" . $strUnit . "', 
					dtCreate=NOW(),
					usrCreate='" . $strEmail . "'
				";
		$MySQLi->query($query);

		$query = "SELECT * 
				FROM item 
				WHERE strKdItem='" . $strKdItem . "' AND 
						stsAktif=2
				";
		$result = $MySQLi->query($query);
	}
	$strKeterangan = "";
	if ($_SESSION['usrLevel'] == 2)
		$strKeterangan = "strKeteranganMGR='MGR : " . $_POST['strKeterangan'] . "', strKeteranganSPV='SPV : " . $_POST['strKeterangan'] . "', strKeteranganREQ='REQ : " . $_POST['strKeterangan'] . "', ";
	elseif ($_SESSION['usrLevel'] == 3)
		$strKeterangan = "strKeteranganSPV='SPV : " . $_POST['strKeterangan'] . "', strKeteranganREQ='REQ : " . $_POST['strKeterangan'] . "', ";
	elseif ($_SESSION['usrLevel'] == 4)
		$strKeterangan = "strKeteranganREQ='REQ : " . $_POST['strKeterangan'] . "', ";

	$row = $result->fetch_array();
	$item_ID = $row['ID'];
	$query = "UPDATE tpc_d_lpbj 
			SET item_ID='" . $item_ID . "', 
				item_strUnit='" . $strUnit . "', 
				" . $strNmFile . "
				" . $strKeterangan . "
				dept_ID='" . $_POST['dept_ID'] . "', 
				iJumlah='" . $_POST['iJumlah'] . "', 
				iJumlahStok='" . $_POST['iJumlahStok'] . "', 
				dtDibutuhkan='" . $_POST['dtDibutuhkan'] . "', 
				dtUpdate=NOW(), 
				usrUpdate='" . $_SESSION['strEmail'] . "' 
			WHERE ID = '" . $_POST['ID'] . "'
			"; //die($query); 
	$MySQLi->query($query);
	if ($MySQLi->affected_rows == 1) {
		$strBuffer = str_replace('"', '`', str_replace("'", "`", $query));
		$query = "INSERT INTO tpc_log 
				SET strPCHostname='" . gethostbyaddr(getIPv4()) . "', 
					strIPv4='" . getIPv4() . "', 
					strPort='" . $_SERVER['REMOTE_PORT'] . "', 
					strTable='tpc_d_lpbj', 
					strQuery='" . $strBuffer . "', 
					strKeterangan='" . $strEmail . " membuat perubahan detail lpbj pada ID : " . $_POST['ID'] . "', 
					dtCreate=NOW(), 
					usrCreate='" . $strEmail . "' 
				";
		$MySQLi->query($query);
		if ($MySQLi->affected_rows == 1)
			alertpopup("Edit berhasil disimpan", "", "../../../index.php?menu=tambah-lpbj&lpbj=" . $_POST['strKdLPBJ']);
	}
}
