<?php
	date_default_timezone_set("Asia/Jakarta");
	include "config/fungsi_resize.php";
	
	$busimg_status	= $_POST['busimg_status'];
	$armada_id				= $_POST['armada_id'];
	$armada_nama			= $_POST['armada_nama'];
	$armada_seo				= strtolower(str_replace(' ','-',$armada_nama));
	$busimg_date		= date("Y-m-d H:i:s");
	$busimg_update	= date("Y-m-d H:i:s");
		
	$code					= md5(uniqid(rand()));
	$gencode				= substr($code, 0, 3);
	$replace				= strtolower(str_replace(' ','_',$armada_nama));
		
	$nama_file 				= $_FILES["busimg_image"]["name"];
	$tipe_file 				= $_FILES["busimg_image"]["type"];
	$alamat 				= $_FILES["busimg_image"]["tmp_name"];
	$nama_baru 				= $replace."_".$gencode.".".end((explode(".", $nama_file)));
	$tujuan 				= "assets/images/galeri/$nama_baru";
	
	if(empty($nama_file))
	{
		mysqli_query($koneksi, "INSERT INTO cmas_busimg VALUES('','$busimg_status','$armada_id','$armada_nama','$armada_seo','$busimg_date','$busimg_update','')") or die ("<script>alert('INPUT DATA GAGAL, SILAHKAN COBA KEMBALI !');history.go(-1);</script>");
		echo "<meta http-equiv='refresh' content='0; url=?m=busimg_index'>";
	}
	elseif (!empty($nama_file))
	{
		if ($tipe_file != "image/gif" && $tipe_file != "image/jpg" && $tipe_file != "image/jpeg" && $tipe_file != "image/png") {
		
			echo "<script>alert('MAAF, FILE TIDAK SUPPORT');history.go(-1);</script>";
		
		} else {
			
			$gambar_baru = move_uploaded_file($alamat, $tujuan);
			resize_crop_image(800, 600, $tujuan, $tujuan);
			mysqli_query($koneksi, "INSERT INTO cmas_busimg VALUES('','$busimg_status','$armada_id','$armada_nama','$armada_seo','$busimg_date','$busimg_update','$nama_baru')") or die ("<script>alert('INPUT DATA GAGAL, SILAHKAN COBA KEMBALI !');history.go(-1);</script>");
			echo "<meta http-equiv='refresh' content='0; url=?m=busimg_index'>";
			
		}
	}
?>