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



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")){
			
			$table  =  '(
				SELECT
					color_status_id,
					status_color,
					status_name,

					is_deleted,

					(
						 COALESCE((SELECT COUNT(*)  FROM product_color_status PCS  WHERE is_deleted =0 AND PCS.color_status_id_fk= C.color_status_id ),0)

					) AS cnt

				FROM
					color_status C
					
				WHERE
					( C.is_deleted = 0 ) 
			) AS ALLTBL';

			// Table's primary key
			$primaryKey = 'color_status_id';
			$columns =  array(
				  			    array( 'db' => 'color_status_id', 'dt' => 0 ),
			    				array( 'db' => 'status_color', 'dt' => 1 ),
			    				array( 'db' => 'status_name',  'dt' => 2 ),
			    				array( 'db' => 'cnt',   'dt' => 3 ),
								array( 'db' => 'is_deleted',   'dt' => 4 )
			);
			// SQL server connection information
			$sql_details = array(
			    'user' => $mysql_user,
			    'pass' => $mysql_password,
			    'db'   => $mysql_db,
			    'host' => $mysql_server
			);
			
			
			require( 'ssp.php' );
			echo json_encode(
			    SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns ,null, "" )
			);
			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_edit") {

			$id = $_POST['itm_id'];

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


			$res = $db->query("SELECT * FROM color_status WHERE is_deleted = 0 AND color_status_id=$id");
			if ($res) {
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result[0] = $row['color_status_id'];
						$result[1] = $row['status_color'];
						$result[2] = $row['status_name'];
						array_push($op['data'],$result);
				}

				$op['is_success'] = true;
				echo json_encode($op);
				exit;
			}else{
				$op['is_success'] = false;
				$op['data'] = $db->error;
				exit;
			}
		}

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

			$id = $_POST['obj_id'];
			
			$status_color	= addslashes($_POST["status_color"]);
            $status_name	= addslashes($_POST["status_name"]);

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

			$res = $db->query("SELECT * FROM color_status WHERE is_deleted=0 AND status_name = '$status_name' AND color_status_id != '$id'");

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




			$lastaction = "Update Color";
			$update_qry = "UPDATE color_status SET  
												status_color 	= '$status_color', 
												status_name		='$status_name',
												last_action		='$lastaction',
												last_action_by  ='".$_SESSION['user_id']."' 
							WHERE color_status_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 Color";


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

			
			

			


	      $res = $db->query("UPDATE color_status SET is_deleted = 1 ,deleted_at = '$reg_date',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE color_status_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") {

            $status_color	= addslashes($_POST["status_color"]);
            $status_name	= addslashes($_POST["status_name"]);

			$res = $db->query("SELECT * FROM color_status WHERE status_name = '$status_name' AND is_deleted=0");

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

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

			$sql ="INSERT INTO color_status(
											status_color,
											status_name,
											created_by,
											last_action,
											last_action_by)
									VALUES( '$status_color',
											'$status_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;
	}
?>

