<?php
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	include "libs/functionresize.php";
	
	$admin_id	 		= mysqli_real_escape_string($koneksi, $_POST['admin_id']);
	$admin_nama 		= mysqli_real_escape_string($koneksi, $_POST['admin_nama']);
	$admin_username		= mysqli_real_escape_string($koneksi, $_POST['admin_username']);
	$admin_email 		= mysqli_real_escape_string($koneksi, $_POST['admin_email']);
	$admin_status 		= mysqli_real_escape_string($koneksi, $_POST['admin_status']);
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_');
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$admin_nama));
	$nama_file 			= $_FILES["admin_image"]["name"];
	$tipe_file 			= $_FILES["admin_image"]["type"];
	$alamat 			= $_FILES["admin_image"]["tmp_name"];
	$nama_baru 			= "admin_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/admin/$nama_baru";
	
	$admincat_id	 	= mysqli_real_escape_string($koneksi, $_POST['admincat_id']);
	$admincat_nama 		= mysqli_real_escape_string($koneksi, $_POST['admincat_nama']);
	
	$updated 			= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "UPDATE cmas_admin SET 
		admin_nama			= '$admin_nama',
		admin_username		= '$admin_username',
		admin_email			= '$admin_email',
		admin_status		= '$admin_status',
		admincat_id			= '$admincat_id',
		admincat_nama		= '$admincat_nama',
		updated				= '$updated',
		updated_id			= '$udt[admin_id]',
		updated_username	= '$udt[admin_username]',
		updated_nama		= '$udt[admin_nama]'
		WHERE admin_id		= '$admin_id'") or die (mysqli_error($koneksi));
		
		$notification_ip = $_SERVER['REMOTE_ADDR'];
		mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Update', 'Admin: $admin_nama', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=admin_index&us=us'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" AND $tipe_file != "image/jpg" AND $tipe_file != "image/jpeg" AND $tipe_file != "image/png") {
			$notification_ip = $_SERVER['REMOTE_ADDR'];
			mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Update & Upload File', 'Admin: $admin_nama, Error!', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=admin_edit&id=$admin_id&ufe=ufe'>";
		} else {
			$db = mysqli_query($koneksi, "SELECT * FROM cmas_admin WHERE admin_id='$admin_id'");
			if (mysqli_num_rows($db) > 0 ) {
				$dt = mysqli_fetch_array($db);
				@unlink('assets/images/admin/'.$dt['admin_image']);
			}
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			resize_crop_image(200, 200, $tujuan, $tujuan);
			mysqli_query($koneksi, "UPDATE cmas_admin SET 
			admin_nama			= '$admin_nama',
			admin_username		= '$admin_username',
			admin_email			= '$admin_email',
			admin_status		= '$admin_status',
			admin_image			= '$nama_baru',
			admin_imageid		= '1',
			admincat_id			= '$admincat_id',
			admincat_nama		= '$admincat_nama',
			updated				= '$updated',
			updated_id			= '$udt[admin_id]',
			updated_username	= '$udt[admin_username]',
			updated_nama		= '$udt[admin_nama]'
			WHERE admin_id		= '$admin_id'") or die (mysqli_error($koneksi));
			
			$notification_ip = $_SERVER['REMOTE_ADDR'];
			mysqli_query($koneksi, "INSERT INTO cmas_notification (notification_id, notification_status, notification_description, notification_ip, notification_read, postdated, posted_id, posted_username, posted_nama) VALUE(NULL, 'Update & Upload File', 'Admin: $admin_nama', '$notification_ip', '1', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=admin_index&us=us'>";

		}
	}
?>