<?php

//login_action.php

include('../class/Appointment.php');

$object = new Appointment;

if(isset($_POST["admin_email_address"]))
{
	sleep(2);
	$error = '';
	$url = '';
	$data = array(
		':admin_email_address'	=>	$_POST["admin_email_address"]
	);

	$object->query = "
		SELECT * FROM admin_table 
		WHERE admin_email_address = :admin_email_address
	";

	$object->execute($data);

	$total_row = $object->row_count();

	if($total_row == 0)
	{
		$object->query = "
			SELECT * FROM arsiparis_table 
			WHERE arsiparis_email = :admin_email_address
		";
		$object->execute($data);

		if($object->row_count() == 0)
		{
			$error = '<div class="alert alert-danger">Wrong Email Address</div>';
		}
		else
		{
			$result = $object->statement_result();

			foreach($result as $row)
			{
				if($row["login_status"] == 'disabled')
				{
					$error = '<div class="alert alert-danger">Your account is Disabled, Contact Admin</div>';
				}
				else
				{
					if($_POST["admin_password"] == $row["arsiparis_password"])
					{
						$_SESSION['arsiparis_id'] = $row['arsiparis_nip'];
						$_SESSION['arsiparis_type'] = 'User';
						$url = $object->base_url . 'admin/dashboard.php';
					}
					else
					{
						$error = '<div class="alert alert-danger">Wrong Password</div>';
					}
				}
			}
		}
	}
	else
	{
		//$result = $statement->fetchAll();

		$result = $object->statement_result();

		foreach($result as $row)
		{
			if($_POST["admin_password"] == $row["admin_password"])
			{
				$_SESSION['arsiparis_id'] = $row['admin_id'];
				$_SESSION['arsiparis_type'] = 'Admin';
				$url = $object->base_url . 'admin/dashboard.php';
			}
			else
			{
				$error = '<div class="alert alert-danger">Wrong Password</div>';
			}
		}
	}

	$output = array(
		'error'		=>	$error,
		'url'		=>	$url
	);

	echo json_encode($output);
}

?>