<?php
	session_start();
	include"../../../lib/conn.php";
	include"../../../lib/all_function.php";
	include"../../../lib/fungsi_sqltgl.php";

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

	if(isset($_GET['mod']) && isset($_GET['act']))
	{
		$mod = $_GET['mod'];
		$act = $_GET['act'];
	}
	else
	{
		$mod = "";
		$act = "";
	}
	$linkaksi = 'med.php?mod=user&idmx='.$_GET['idmx'];
	if($mod == "user" AND $act == "simpan")
	{

		$password 		= md5($_POST['password']);
		$username 		= anti_inject($_POST['username']);
		$nama_lengkap 	= anti_inject($_POST['nama_lengkap']);
		$email 			= anti_inject($_POST['email']);
		$no_telp		= anti_inject($_POST['no_telp']);
		$level 			= anti_inject($_POST['level']);
		$aktif 			= $_POST['aktif'];
		$foto_user      = basename($_FILES["file"]["name"]);
	    $size_file      = $_FILES['file']['size'];
	    $type_file      = $_FILES['file']['type'];
	    $uploaddir 		= '../../../assets/images/img_user/';
	    $alamatfile 	= $uploaddir . $foto_user ;
	    move_uploaded_file($_FILES['file']['tmp_name'], $alamatfile);	
		$created_by 	= $_SESSION['login_user'];
		$tokox 			= $_POST['toko'];
		$count = count($tokox);
		for ($i=0; $i < $count ; $i++) { 
			$tokodp[] = $tokox[$i];
		}

		$toko = implode(",", $tokodp);

		if(!empty($_POST['password']))
		{
			if ($_POST['password'] == $_POST['rpassword']) {				
				mysqli_query($conn,"INSERT INTO users(id_identitas,username,password,nama_lengkap,email,no_telp,level,aktif,foto_user,created_by) VALUES ('$toko','$username', '$password', '$nama_lengkap', '$email', '$no_telp', '$level', '$aktif', '$foto_user', '$created_by')") or die(mysqli_error());
				log_insert($username,"users","Menambahakan Data User",$_SESSION['login_user'] );
				flash('example_message', 'Berhasil menambah data user.' );				

				header("location:../../../".$linkaksi);
			}
			else
			{
				flash('example_message', 'Password tidak sama..', 'w3-red');


				header("location:../../../".$linkaksi);
			}
		}
		else
		{
			flash('example_message', 'Password Kosong', 'w3-yellow');


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

	elseif ($mod == "user" AND $act == "edit") 
	{
		$username		= $_POST['username'];		
		$nama_lengkap	= $_POST['nama_lengkap'];
		$email 			= $_POST['email'];
		$no_telp		= $_POST['no_telp'];
		$level 			= $_POST['level'];
		$aktif 			= $_POST['aktif'];
		if ($_FILES['file']['name'] == ""){
			$file_name 	= $_POST['file1'];
		}else{
			$file_name  = basename($_FILES['file']['name']);
		}
	    $size_file      = $_FILES['file']['size'];
	    $type_file      = $_FILES['file']['type'];
	    $uploaddir 		= '../../../assets/images/img_user/';
	    $alamatfile 	= $uploaddir . $file_name;
	    move_uploaded_file($_FILES['file']['tmp_name'], $alamatfile);
		$update_by 			= $_SESSION['login_user'];
		$tokox 			= $_POST['toko'];
		$count = count($tokox);
		for ($i=0; $i < $count ; $i++) { 
			$tokodp[] = $tokox[$i];
		}

		$toko = implode(",", $tokodp);

		if($_POST['password'] == "")
		{
			mysqli_query($conn,"UPDATE users SET user_id = '$_POST[id]',
										id_identitas = '$toko',
										username = '$_POST[username]',
										nama_lengkap 	= '$nama_lengkap', 
									   	email 			= '$email', 
									   	no_telp 		= '$no_telp',
									   	level 			= '$level',
									   	aktif 			= '$aktif',
									   	foto_user 		= '$file_name',
									   	update_by 		= '$update_by'
						WHERE user_id = '$_POST[id]'") or die(mysqli_error());
			flash('example_message', 'Berhasil menambah data user.' );

			log_update($username,"users","Mengubah Data User",$_SESSION['login_user'] );

			

			header("location:../../../".$linkaksi);
		}
		else
		{
			if ($_POST['password'] == $_POST['rpassword']) {
				$pass = md5($_POST['password']);
				mysqli_query($conn,"UPDATE users SET user_id = '$_POST[id]',
										id_identitas = '$toko',
										username = '$_POST[username]',
										password 	= '$pass',
										nama_lengkap 	= '$nama_lengkap', 
									   	email 			= '$email', 
									   	no_telp 		= '$no_telp',
									   	level 			= '$level',
									   	aktif 			= '$aktif',
									   	foto_user 		= '$file_name',
									   	update_by 		= '$update_by'
						WHERE user_id = '$_POST[id]'") or die(mysqli_error());
				flash('example_message', 'Berhasil mengubah data user.' );
				log_update($username,"users","Mengubah Data User",$_SESSION['login_user'] );

				echo"<script>
					window.history.go(-2);
				</script>";
			}
			else
			{
				flash('example_message', 'Password tidak sama..');

				

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

	elseif ($mod == "user" AND $act == "hapus") 
	{

		$sql = "select * from users where user_id = '$_GET[id]'";
		$result = mysqli_query($conn, $sql);
		if(mysqli_num_rows($result) > 0){
			$row = mysqli_fetch_assoc($result);
			$image = $row['foto_user'];
	    	$uploaddir 		= '../../../assets/images/img_user/';
			unlink($uploaddir.$image);
			mysqli_query($conn,"DELETE FROM users WHERE user_id = '$_GET[id]'") or die(mysqli_error());
			log_delete($row['username'],"users","Menghapus Data User",$_SESSION['login_user'] );
			flash('example_message', 'Berhasil menghapus data user.' );
			header("location:../../../".$linkaksi);
		}	
	}

?>