<?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";
	
	$stringreplace 		= array ('/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+',' ','_');
	$bus_id				= mysqli_real_escape_string($koneksi, $_POST['bus_id']);
	$bus_nama			= mysqli_real_escape_string($koneksi, $_POST['bus_nama']);
	$bus_sdesc			= mysqli_real_escape_string($koneksi, $_POST['bus_sdesc']);
	$bus_desc			= mysqli_real_escape_string($koneksi, $_POST['bus_desc']);
	$bus_model			= mysqli_real_escape_string($koneksi, $_POST['bus_model']);
	$bus_status			= mysqli_real_escape_string($koneksi, $_POST['bus_status']);
	$bus_seo			= strtolower(str_replace($stringreplace,'-',$bus_nama));
	$bus_metatitle		= mysqli_real_escape_string($koneksi, $_POST['bus_metatitle']);
	$bus_metadesc		= mysqli_real_escape_string($koneksi, $_POST['bus_metadesc']);
	$bus_metakey		= mysqli_real_escape_string($koneksi, $_POST['bus_metakey']);
	
	$code		   		= md5(uniqid(rand()));
	$gencode			= substr($code, 0, 5);
	$replace			= strtolower(str_replace($stringreplace,'_',$bus_nama));
	$nama_file 			= $_FILES["bus_image"]["name"];
	$tipe_file 			= $_FILES["bus_image"]["type"];
	$alamat 			= $_FILES["bus_image"]["tmp_name"];
	$nama_baru 			= "bus_".$replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 			= "assets/images/bus/$nama_baru";
	
	$updated			= date("Y-m-d H:i:s");
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "UPDATE cmas_bus SET 	
		bus_nama			= '$bus_nama',
		bus_sdesc			= '$bus_sdesc',
		bus_desc			= '$bus_desc',
		bus_model			= '$bus_model',
		bus_status			= '$bus_status',
		bus_seo				= '$bus_seo',
		bus_metatitle		= '$bus_metatitle',
		bus_metadesc		= '$bus_metadesc',
		bus_metakey			= '$bus_metakey',
		updated				= '$updated',
		updated_id			= '$udt[updated_id]',
		updated_username	= '$udt[updated_username]',
		updated_nama		= '$udt[updated_nama]'
		WHERE bus_id		= '$bus_id'") or die (mysqli_error($koneksi));
		
		mysqli_query($koneksi, "UPDATE cmas_busgaleri SET bus_nama = '$bus_nama', bus_seo = '$bus_seo' WHERE bus_id = '$bus_id'");
		
		echo "<meta http-equiv='refresh' content='0; url=?m=bus_index&us=us'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" && $tipe_file != "image/jpg" && $tipe_file != "image/jpeg" && $tipe_file != "image/png") {
		
			echo "<meta http-equiv='refresh' content='0; url=?m=bus_edit&id=$bus_id&us=us'>";
		
		} else {
			
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			// resize_crop_image(1200, 900, $tujuan, $tujuan);
			mysqli_query($koneksi, "update cmas_bus set 	
			bus_nama			= '$bus_nama',
			bus_sdesc			= '$bus_sdesc',
			bus_desc			= '$bus_desc',
			bus_model			= '$bus_model',
			bus_status			= '$bus_status',
			bus_seo				= '$bus_seo',
			bus_metatitle		= '$bus_metatitle',
			bus_metadesc		= '$bus_metadesc',
			bus_metakey			= '$bus_metakey',
			bus_image			= '$nama_baru',
			bus_imageid			= '1',
			updated				= '$updated',
			updated_id			= '$udt[updated_id]',
			updated_username	= '$udt[updated_username]',
			updated_nama		= '$udt[updated_nama]'
			WHERE bus_id		= '$bus_id'") or die (mysqli_error($koneksi));
			
			mysqli_query($koneksi, "UPDATE cmas_busgaleri SET bus_nama = '$bus_nama', bus_seo = '$bus_seo' WHERE bus_id = '$bus_id'");
			
			echo "<meta http-equiv='refresh' content='0; url=?m=bus_index&us=us'>";
		}
	}
?>