<?php
	session_start();
	include"../../lib/conn.php";
	include"../../lib/all_function.php";
  	$upload_dir = '../../images/identitas/';
  	$upload_bukti = '../../images/bukti/';

	if(!isset($_SESSION['login_user'])){
		header('location: ../../login.php'); // Mengarahkan ke Home Page
	}


	if(isset($_SESSION['identitas']) AND $_SESSION['identitas'] <> 'TRUE')
	{
		?>
		  <div class="alert alert-danger alert-dismissible" id="succsess-alert">
	        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
	        <h4><i class="icon fa fa-ban"></i> Alert!</h4>
	        Dilarang mengakses file ini.
	      </div>
		<?php
		die();
	}

	if(isset($_GET['mod']) && isset($_GET['act']))
	{
		$mod = $_GET['mod'];
		$act = $_GET['act'];
	}
	else
	{
		$mod = "";
		$act = "";
	}

	$linkaksi = 'med.php?mod=identitas&idmx='.$_GET['idmx'];
	if ($mod == "identitas" AND $act == "edit") {
				//echo "string";
				$nama= anti_inject($_POST['nama']);
				$pemilik= anti_inject($_POST['pemilik']);
				$alamat= anti_inject($_POST['alamat']);
				$provinsi= anti_inject($_POST['provinsi']);
				$kabupaten= anti_inject($_POST['kabupaten']);
				$kecamatan= anti_inject($_POST['kecamatan']);
				$kelurahan= anti_inject($_POST['kelurahan']);
				$telepon= anti_inject($_POST['telepon']);
				$email= anti_inject($_POST['email']);
				$website= anti_inject($_POST['website']); 

				$foto = anti_inject($_POST['img']);
				$imgName = $_FILES['foto']['name'];
				$imgTmp = $_FILES['foto']['tmp_name'];
				$imgSize = $_FILES['foto']['size'];
				if($imgName){
					$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));

					$allowExt  = array('jpeg', 'jpg', 'png', 'gif');

					$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;

					if(in_array($imgExt, $allowExt)){

						if($imgSize < 5000000){
							unlink($upload_dir.$foto);
							move_uploaded_file($imgTmp ,$upload_dir.$userPic);
						}else{
							$errorMsg = 'Image too large';
							flash('example_message', $errorMsg );

							header("location: ../../".$linkaksi);
						}
					}
				}else{

					$userPic = $foto;
				}
			mysqli_query($conn,"UPDATE `tb_identitas` SET `nama`='$nama',`pemilik`='$pemilik',`provinsi`='$provinsi',`kabupaten`='$kabupaten',`kecamatan`='$kecamatan',`kelurahan`='$kelurahan',`alamat`='$alamat',`tlp`='$telepon',`email`='$email',`website`='$website',`logo`='$userPic' WHERE `id`= '$_POST[id]'") or die(mysqli_error());
			flash('example_message', 'Berhasil menghapus data identitas.' );
			log_update($nama,"tb_identitas","Mengubah Data Toko",$_SESSION['login_user'] );
			header("location: ../../".$linkaksi);
		
	}
	elseif($mod == "identitas" AND $act == "simpan")
	{
				$nama= anti_inject($_POST['nama']);
				$pemilik= anti_inject($_POST['pemilik']);
				$alamat= anti_inject($_POST['alamat']);
				$provinsi= anti_inject($_POST['provinsi']);
				$kabupaten= anti_inject($_POST['kabupaten']);
				$kecamatan= anti_inject($_POST['kecamatan']);
				$kelurahan= anti_inject($_POST['kelurahan']);
				$telepon= anti_inject($_POST['telepon']);
				$email= anti_inject($_POST['email']);
				$website= anti_inject($_POST['website']);

				$imgName = $_FILES['foto']['name'];
				$imgTmp = $_FILES['foto']['tmp_name'];
				$imgSize = $_FILES['foto']['size'];
				$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));

				$allowExt  = array('jpeg', 'jpg', 'png', 'gif');

				$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;

				if(in_array($imgExt, $allowExt)){

					if($imgSize < 5000000){
						move_uploaded_file($imgTmp ,$upload_dir.$userPic);
					}else{
						$errorMsg = 'Image too large';
						flash('example_message', $errorMsg );

						header("location: ../../".$linkaksi);
					}
				}
			
				mysqli_query($conn,"INSERT INTO `tb_identitas`(`nama`, `pemilik`, `provinsi`, `kabupaten`, `kecamatan`, `kelurahan`, `alamat`, `tlp`, `email`, `website`, `logo`, `status`) VALUES ('$nama','$pemilik','$provinsi','$kabupaten','$kecamatan','$kelurahan','$alamat','$telepon','$email','$website','$userPic','Pending')") or die(mysqli_error());
				flash('example_message', 'Berhasil menambah data identitas.' );
				log_insert($nama,"tb_identitas","Menambahakan Data Toko",$_SESSION['login_user'] );
				header("location:../../$linkaksi&act=bayar");		
			
		
	}
	elseif ($mod == "identitas" AND $act == "hapus") 
	{
		$query = "SELECT * FROM `tb_identitas` WHERE id = '$_GET[id]'";
		$sql_kul = mysqli_query($conn,$query);	
		$m = mysqli_fetch_assoc($sql_kul);
		log_delete($m['nama'],"tb_identitas","Menghapus Data Toko",$_SESSION['login_user'] );
		mysqli_query($conn,"DELETE FROM tb_identitas WHERE id = '$_GET[id]'");
		flash('example_message', 'Berhasil menghapus data identitas.' );
		header('Location: ' . $_SERVER["HTTP_REFERER"] );
	}elseif ($mod == "identitas" AND $act == "upload") {
				$imgName = $_FILES['foto']['name'];
				$imgTmp = $_FILES['foto']['tmp_name'];
				$imgSize = $_FILES['foto']['size'];
				$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));

				$allowExt  = array('jpeg', 'jpg', 'png', 'gif');

				$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;

				if(in_array($imgExt, $allowExt)){

					if($imgSize < 5000000){
						move_uploaded_file($imgTmp ,$upload_bukti.$userPic);
					}else{
						$errorMsg = 'Image too large';
						flash('example_message', $errorMsg );

						header('Location: ' . $_SERVER["HTTP_REFERER"] );
					}
				}
		log_insert($_POST['id'],"tb_pembayaran_buka_cabang","Upload Bukti Buka Cabang",$_SESSION['login_user'] );
		mysqli_query($conn,"INSERT INTO `tb_pembayaran_buka_cabang`(`id_identitas`, `file`, `keterangan`, `status`) VALUES ('$_POST[id]','$userPic','Belum Aktif Tunggu Approval Kami','Pending')") or die(mysqli_error());
			flash('example_message', 'Berhasil Upload Bukti Pembayaran .' );
			header('Location: ' . $_SERVER["HTTP_REFERER"] );
	}
?>