<?php
include '../connect.php';
session_start();

$gelaran = $_POST['gelaran'];
$nama =strtoupper($_POST['nama']);
$kadPengenalan =$_POST['kadPengenalan'];
$password =$_POST['password'];
$bahagian =$_POST['bahagian'];

$gelaranNew = mysqli_real_escape_string($conn,$gelaran);
$namaNew = mysqli_real_escape_string($conn,$nama);
$passwordNew = mysqli_real_escape_string($conn,$password);

$gelaranNama= $gelaranNew." ".$namaNew;

//$ic=$_SESSION["kadPengenalan"];

date_default_timezone_set("Asia/Kuala_Lumpur");			
$date=date('Y-m-d');
$time=date('H:i:sa');


	if($gelaran =="" || $nama =="" || $kadPengenalan == "" || $password =="" || $bahagian == "" )
	{
		mysqli_commit($conn);
		echo "<script>alert('Sila lengkapkan maklumat pendaftaran. ');";
		echo "window.location.href = '../registration.php';</script>";
		exit;
		
	}
	else
	{
		$sql="SELECT `kad_pengenalan` FROM `user` WHERE `kad_pengenalan`='$kadPengenalan'";
		$fetch = mysqli_query($conn,$sql);
		$row = mysqli_num_rows($fetch);
		
		if($row == 1)
		{
			echo "<script>alert('Pendaftaran gagal. No. Kad Pengenalan sudah didaftarkan!');";
					echo "window.location.href = '../registration.php';</script>";
					exit;
		}
		else
		{
			$query="INSERT INTO `user` (`kad_pengenalan`,`nama_user`,`katalaluan_user`,`bahagian`)
			VALUES ('".$kadPengenalan."','".$gelaranNama."','".$passwordNew."','".$bahagian."')";
			$strQuery = mysqli_query($conn,$query);
			
				if($strQuery) 
				{
					$q4="INSERT INTO audit_trail(tarikh,masa,operasi,catatan)
					VALUES ('$date','$time','Daftar','Daftar user')";
					$strQuery1 = mysqli_query($conn,$q4);
					
					if($strQuery1)
					{
						echo "<script>alert('Pendaftaran berjaya!');";
						echo "window.location.href = '../index.html';</script>";
						exit;
					}
				}
				else
				{
					/*mysqli_commit($conn);
					echo "<script>alert('Sila lengkapkan maklumat pendaftaran. ');";
					echo "window.location.href = '../registration.php';</script>";
					exit;*/
					echo 'MySQL Error: ' . mysqli_error($conn); echo "<br>";
					exit; 
				}
		}
		
		
		
	}
?>

