<?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 = "";
	}

	if($mod == "pegawai" AND $act == "simpan")
	{
		$pegawai_nomor			= anti_inject($_POST['pegawai_nomor']);
		$pegawai_nama			= $_POST['pegawai_nama'];
		$pegawai_gender 		= $_POST['pegawai_gender'];
		$pegawai_agama 			= $_POST['pegawai_agama'];
		$pegawai_jabatan 		= $_POST['pegawai_jabatan'];
		$pegawai_alamat			= $_POST['pegawai_alamat'];
		$pegawai_keterangan 	= $_POST['pegawai_keterangan'];
		$created_by 			= $_SESSION['login_id'];

		mysqli_query($conn,"INSERT INTO pegawai(
						pegawai_nomor,
						pegawai_nama,
						pegawai_gender,
						pegawai_agama,
						pegawai_jabatan,
						pegawai_alamat,
						pegawai_keterangan,
						created_by) 

				VALUES ('$pegawai_nomor',
						'$pegawai_nama',
						'$pegawai_gender',
						'$pegawai_agama',
						'$pegawai_jabatan',
						'$pegawai_alamat',
						'$pegawai_keterangan',
						'$created_by')") or die(mysqli_error());
		flash('example_message', 'Berhasil menambah data pegawai.' );

		log_insert($pegawai_nama,"pegawai","Menambahakan pegawai",$_SESSION['login_user'] );
		
		header("location: ../../../med.php?mod=pegawai&idmx=".$_GET['idmx']);
	}

	elseif ($mod == "pegawai" AND $act == "edit") 
	{
		$pegawai_nomor			= anti_inject($_POST['pegawai_nomor']);
		$pegawai_nama			= $_POST['pegawai_nama'];
		$pegawai_gender 		= $_POST['pegawai_gender'];
		$pegawai_agama 			= $_POST['pegawai_agama'];
		$pegawai_jabatan 		= $_POST['pegawai_jabatan'];
		$pegawai_alamat			= $_POST['pegawai_alamat'];
		$pegawai_keterangan 	= $_POST['pegawai_keterangan'];
		$update_by 			= $_SESSION['login_id'];
		mysqli_query($conn,"UPDATE pegawai 
					SET 
						pegawai_nomor 	= '$pegawai_nomor',
						pegawai_nama		= '$pegawai_nama',
						pegawai_gender	= '$pegawai_gender',
						pegawai_agama	= '$pegawai_agama',
						pegawai_jabatan	= '$pegawai_jabatan',
						pegawai_alamat	= '$pegawai_alamat',
						pegawai_keterangan 	= '$pegawai_keterangan',
						update_by 		= '$update_by' 
					WHERE pegawai_id = '$_POST[id]'") or die(mysqli_error());

		flash('example_message', 'Berhasil mengubah data pegawai.');
		log_update($pegawai_nama,"pegawai","Mengubah pegawai",$_SESSION['login_user'] );

		
		header("location: ../../../med.php?mod=pegawai&idmx=".$_GET['idmx']);
	}

	elseif ($mod == "pegawai" AND $act == "hapus") 
	{
		$query = "SELECT * FROM `pegawai` WHERE pegawai_id = '$_GET[id]'";
		$sql_kul = mysqli_query($conn,$query);	
		$m = mysqli_fetch_assoc($sql_kul);
		
		log_delete($m['pegawai_nama'],"pegawai","Menghapus pegawai",$_SESSION['login_user'] );
		mysqli_query($conn,"DELETE FROM pegawai WHERE pegawai_id = '$_GET[id]'") or die(mysqli_error());
		flash('example_message', 'Berhasil menghapus data pegawai.' );
		
		header("location: ../../../med.php?mod=pegawai&idmx=".$_GET['idmx']);	
	}

?>