<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
if(!isset($_SESSION['is_login']) ){
header("location: login.php");
?>
<script>
window.location="login.php";
</script>
<?php
}

// $permission  = explode(',',$_SESSION['permission_id']);
// if(!in_array(19,$permission) && !in_array(1,$permission)  ){

// $op['is_success'] = false;
// $op['data'] 	  = "عذرا انت غير مخول للوصل الى هذه الصفحة";
// echo json_encode($op);
// exit;

// }

date_default_timezone_set($_SESSION['SYSTimezone']);

if (isset($_POST['type']) || isset($_GET['type'])){
		require_once "db_connect.php";

		if( isset($_GET['type']) && ($_GET['type'] == "load")){
			// DB table to use
			$table = 'notes';
			// Table's primary key
			$primaryKey = 'note_id';
			$columns =  array(
				  			  array( 'db' => 'note_id', 'dt' => 0 ),
			    				array( 'db' => 'note', 'dt' => 1 ),
			    				array( 'db' => 'created_at',   'dt' => 2 , 'formatter'=> function($d){
									return date('Y-m-d',strtotime($d));
									 } ),
								array( 'db' => 'is_deleted',   'dt' => 3 )
			);
			// SQL server connection information
			$sql_details = array(
			    'user' => $mysql_user,
			    'pass' => $mysql_password,
			    'db'   => $mysql_db,
			    'host' => $mysql_server
			);
			require( 'ssp.class.php' );
			echo json_encode(
			    SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns ,null, " is_deleted=0" )
			);
			exit;
		}

		if ($_POST['type']=="get") {

			//$user_id = $_POST['cusid'];

			$res = $db->query("SELECT * FROM category WHERE is_deleted = 0");
			if ($res) {
				$op  = array();
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result[0] = $row['category_id'];
						$result[1] = $row['category'];
						$result[2] = $row['category_description'];
						array_push($op,$result);
				}
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}

		if ($_POST['type']=="get_outcome") {

			//$user_id = $_POST['cusid'];

			$res = $db->query("SELECT * FROM category WHERE is_deleted = 0 AND category_type=1");
			if ($res) {
				$op  = array();
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result[0] = $row['category_id'];
						$result[1] = $row['category'];
						$result[2] = $row['category_type'];
						array_push($op,$result);
				}
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}

		if ($_POST['type']=="get_income") {

			//$user_id = $_POST['cusid'];

			$res = $db->query("SELECT * FROM category WHERE is_deleted = 0 AND category_type=0");
			if ($res) {
				$op  = array();
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result[0] = $row['category_id'];
						$result[1] = $row['category'];
						$result[2] = $row['category_type'];
						array_push($op,$result);
				}
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}

		if ($_POST['type'] == "update") {

			$id = $_POST['obj_id'];
			$name=addslashes($_POST["name"]);
			// $cat_desc=addslashes($_POST["category_description"]);

			$op  = array('is_success'=>false, 'data' => '');

			$res = $db->query("SELECT * FROM notes WHERE is_deleted=0 AND note = '$name' AND note_id != '$id'");

			if (mysqli_num_rows($res)>0) {
				$op['is_success'] = false;
				$op['data'] 	  = "اسم الفئة مكرر";
				echo json_encode($op);
				exit;
			}




			$lastaction = "Update Note";
			$update_qry = "UPDATE notes SET  note = '$name', last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE note_id = $id";


			$res = $db->query($update_qry);
			if ($res) {
					$op['is_success'] = true;
					$op['data'] 	 	  = "تم تحديث الفئة بنجاح";
					echo json_encode($op);
					exit;
			}else{
				$op['is_success'] = false;
				$op['data'] 	  	= "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
				echo json_encode($op);
				exit;
			}
		}


		if ($_POST['type']=="delete") {
			$id=addslashes($_POST["obj_id"]);
			$reg_date = date("Y-m-d h:i:s", time());
			$lastaction = "Delete Note";


			$op  = array('is_success'=>false, 'data' => '');

			
			

			


	      $res = $db->query("UPDATE notes SET is_deleted = 1 ,deleted_at = '$reg_date',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE note_id=$id");
	     	if ($res){
	     		$op['is_success'] = true;
				$op['data'] 	  = "تمت عملية الحذف بنجاح";
				echo json_encode($op);
				exit;
	     	}
	      else{
	      		$op['is_success'] = false;
				$op['data'] 	  = "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
				echo json_encode($op);
				exit;
	     	}
		}

		if ($_POST['type'] == "create") {
            
            $name=addslashes($_POST["name"]);
			
			$res = $db->query("SELECT * FROM notes WHERE note = '$name' AND is_deleted=0");
			if (mysqli_num_rows($res)>0) {
				$op['is_success'] = false;
				$op['data'] 	  = "خطأ اسم  مكرر";
				echo json_encode($op);
				exit;
			}else{
				// $category_description=addslashes($_POST["category_description"]);

				$delete = 0;
				$reg_date = date("Y-m-d h:i:s", time());
				$lastaction = "Create New Note";

						$sql ="INSERT INTO notes( 	note,
													created_by,
													last_action,
													last_action_by)
											VALUES('$name',
													'".$_SESSION['user_id']."',
													'$lastaction',
													'".$_SESSION['user_id']."')";
						$res = $db->query($sql);
							if ($res) {
								$op['is_success'] = true;
								$op['data'] 	  = "تم اضافة  بنجاح";
								echo json_encode($op);
								exit;
							}
							else{
								$op['is_success'] = false;
								$op['data'] 	  = "حصل خطأ: الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
								echo json_encode($op);
								exit;
							}
						}

		}
	}
	else{
		header("Location:../");
		exit;
	}
?>

