<?php 
include_once 'api/config.php';
// if(isset($_POST['register']))
// {
	// $email=$_POST['email'];
	// $user_name=$_POST['user_name'];
	// $pw=$_POST['pw'];
	// $rpw=$_POST['rpw'];
	// $gender=$_POST['gender'];
	// $contact=$_POST['contact'];
	// $address=$_POST['address'];
	// if($pw!=$rpw)
	// {
		// echo "<div class='alert alert-warning'><strong>Errors </strong> Password Mismatch (Confirm Password  is not the same as Password)</div>";
		// exit();
	// }

	// $sql="SELECT * FROM user WHERE email='$email' ";
	// $result = $conn->query($sql);
	// if ($result->num_rows > 0)
	// {
		// echo "<div class='alert alert-warning'><strong>Errors </strong> Email already exists.</div>";
		// exit();
	// }
	// // inserting
	// $pw=md5($pw);
	// $sql="INSERT INTO user (email,name,pw,gender,contact,address,user_type) VALUES ('$email','$user_name','$pw' , '$gender' ,'$contact', '$address','1') ";
	// if($conn->query($sql)===TRUE)
	// {
		// $id=$conn->insert_id;
		// mkdir("files/".$id, 0700);
		// $file_path=$parent_dir=$home_dir="/".$id."/";
		// $created_at=date("Y-m-d H:i:s");
		// // inssert record in files
		// $sql="INSERT INTO files (user_nav,home_dir,user_id,is_home,is_dir,file_name,file_title,parent_dir,file_path,created_at) VALUES ('Home/','$home_dir','$id','1','1','$id','Home','$parent_dir','$file_path','$created_at' )";
		// $conn->query($sql);
		// echo "<div class='alert alert-success'> <strong>Success ! </strong> Registration Successfull. Please Login...Redirecting...!</div>".
			// "<script>setTimeout(function(){ window.location.href='login.php'; }, 2000);</script>";		
		// exit();
	// }
	// else
	// {
		// echo "<div class='alert alert-danger'> <strong>Error ! </strong> Error in inserting record.</div>";
	// }

// }
// login
if(isset($_POST['login']))
{
	$loginId=$_POST['loginId'];
	$pw=$_POST['pw'];
	$pw=md5($pw);

	$sql="SELECT * FROM user WHERE login_id='$loginId' AND pw='$pw' ";
	$result = $link->query($sql);
	if ($result->num_rows > 0)
	{
		$row = $result->fetch_assoc();
		if($row['user_status']==0)
		{
			echo "<div class='alert alert-warning'><strong>Attention </strong> Your account is Blocked.</div>";
			exit();
		}
		$_SESSION['SESS_UID']=$row['user_id'];		
		$_SESSION['SESS_LOGIN']=$row['login_id'];
		$_SESSION['SESS_NAME']=$row['name'];
		$_SESSION['USER_TYPE']=$row['user_type'];
		$_SESSION['USER_DISPLAY']=($row['user_type']==0?"Admin":($row['user_type']==1?"User":"Premium"));
		
		echo "<div class='alert alert-success'> <strong>Success ! </strong> Login Successfull....Redirecting...!</div>".
			"<script>setTimeout(function(){ window.location.href='index.php'; }, 3000);</script>";
		exit();
	}
	else
	{
		echo "<div class='alert alert-danger'> <strong>Error ! </strong> Invalid User ID / Password.</div>";

	}
}
if(isset($_POST['editId']))
{
	$user_id=$_POST['editId'];
	$name=$_POST['name'];
	$cpw=$_POST['cpw'];
	$npw=$_POST['npw'];
	$cpw=md5($cpw);
	$npw=md5($npw);

	$sql="SELECT * FROM user WHERE user_id='$user_id' AND pw='$cpw' ";
	$result = $link->query($sql);
	if ($result->num_rows == 0)
	{
		echo "<div class='alert alert-warning'><strong>Attention </strong> Current Password is NOT correct.</div>";
		exit();
		$row = $result->fetch_assoc();
		if($row['user_status']==0)
		{
			echo "<div class='alert alert-warning'><strong>Attention </strong> Your account is Blocked.</div>";
			exit();
		}
	}
	else
	{
		$sql="UPDATE user SET name='$name',pw='$npw' WHERE  user_id='$user_id'";
		if($link->query($sql)===TRUE)
		{
			$_SESSION['SESS_NAME']=$name;
			echo "<div class='alert alert-success'> <strong>Success ! </strong> Password Changed Successfully...Redirecting...!</div>".
				"<script>setTimeout(function(){ window.location.reload(); }, 3000);</script>";		
			exit();
		}
		else
		{
			echo "<div class='alert alert-danger'> <strong>Error ! </strong> Error in updating record.</div>";
		}

	}
}


?>