<?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_nama 		= mysqli_real_escape_string($koneksi, $_POST['admin_nama']);
	$admin_username		= mysqli_real_escape_string($koneksi, $_POST['admin_username']);
	$admin_password		= md5(mysqli_real_escape_string($koneksi, $_POST['admin_password']));
	$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']);
	
	$postdated 			= date("Y-m-d H:i:s");
	$updated 			= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "INSERT INTO cmas_admin (admin_id, admin_nama, admin_username, admin_password, admin_email, admin_status, admin_image, admin_imageid, admincat_id, admincat_nama, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$admin_nama', '$admin_username', '$admin_password', '$admin_email', '$admin_status', '$admin_image', '$admin_imageid', '$admincat_id', '$admincat_nama', '$postdated', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')") 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, 'Input', 'Admin: $admin_nama', '$notification_ip', '1', '$postdated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=admin_index&is=is'>";
	}
	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, 'Input & 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_input&ufe=ufe'>";
		} else {
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			resize_crop_image(200, 200, $tujuan, $tujuan);
			mysqli_query($koneksi, "INSERT INTO cmas_admin (admin_id, admin_nama, admin_username, admin_password, admin_email, admin_status, admin_image, admin_imageid, admincat_id, admincat_nama, postdated, updated, posted_id, posted_username, posted_nama, updated_id, updated_username, updated_nama) VALUES(NULL, '$admin_nama', '$admin_username', '$admin_password', '$admin_email', '$admin_status', '$nama_baru', '1', '$admincat_id', '$admincat_nama', '$postdated', '$updated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')") 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, 'Input & Upload File', 'Admin: $admin_nama', '$notification_ip', '1', '$postdated', '$udt[admin_id]', '$udt[admin_username]', '$udt[admin_nama]')");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=admin_index&is=is'>";
		}
	}
?>