<?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
					product_id,
					product_color_status_id,
					product_name,
					erbil_code,
					turkish_code,
					status_name,
					status_color
					
				FROM product_color_status PCS
					
					LEFT JOIN product P ON (PCS.product_id_fk = P.product_id )
					LEFT JOIN color_status CS ON (PCS.color_status_id_fk = CS.color_status_id )
					
				WHERE
					( PCS.is_deleted = 0 ) 
			) AS ALLTBL';

			// Table's primary key
			$primaryKey = 'product_color_status_id';
			$columns =  array(
				  			    array( 'db' => 'product_id', 'dt' => 0 ),
			    				array( 'db' => 'erbil_code',  'dt' => 1 ),
			    				array( 'db' => 'turkish_code',   'dt' => 2 ),
								array( 'db' => 'status_name',   'dt' => 3 ),
								array( 'db' => 'status_color',   '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") {


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

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

			if ($res) {
				while ($row = $res->fetch_assoc()) {
						$result['color_status_id'] 	= $row['color_status_id'];
						$result['status_color'] 	= $row['status_color'];
						$result['status_name'] 		= $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']=="print") {



			$id = $_POST['color_id'];

			$cond = "";
			if ($id > 0) {
				$cond = " AND CS.color_status_id =$id";
				
			}

			$res = $db->query("SELECT
								product_id,
								erbil_code,
								turkish_code,
								status_name,
								status_color
								
							FROM product_color_status PCS
								
								LEFT JOIN product P ON (PCS.product_id_fk = P.product_id )
								LEFT JOIN color_status CS ON (PCS.color_status_id_fk = CS.color_status_id )
								
							WHERE
								PCS.is_deleted = 0   $cond 
								ORDER BY erbil_code ASC");

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

			if ($res) {
				while ($row = $res->fetch_assoc()) {
						$result['product_id'] 		= $row['product_id'];
						$result['erbil_code'] 		= $row['erbil_code'];
						$result['turkish_code'] 	= $row['turkish_code'];
						$result['status_name'] 		= $row['status_name'];
						$result['status_color'] 	= $row['status_color'];
						array_push($op['invoice'],$result);
				}
				$op['is_success'] = true;
				echo json_encode($op);
				exit;
			}else{
				$op['is_success'] = false;
				$op['invoice'] = $db->error;
				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'] == "save") {

			
			
			$product_id	= addslashes($_POST["product_id"]);
            $color_id	= addslashes($_POST["color_id"]);

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

			$res = $db->query("DELETE FROM product_color_status WHERE   product_id_fk = '$product_id'");

			




			$lastaction = "Update Color";
			$sql ="INSERT INTO product_color_status(
											product_id_fk,
											color_status_id_fk,
											created_by,
											last_action,
											last_action_by)
									VALUES( '$product_id',
											'$color_id',
											'".$_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;
			}
		}


		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 product_color_status SET is_deleted = 1 ,deleted_at = '$reg_date',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE product_id_fk=$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") {

            $color_id			= addslashes($_POST["color_status_id_fk"]);
            $product_id_fk		= ($_POST["product_id_fk"]);


            for ($i=0; $i <count($product_id_fk) ; $i++) { 
            	$product_id = $product_id_fk[$i];
            
			
				$res = $db->query("DELETE FROM product_color_status WHERE   product_id_fk = '$product_id'");

			




				$lastaction = "Update Color";
				$sql ="INSERT INTO product_color_status(
											product_id_fk,
											color_status_id_fk,
											created_by,
											last_action,
											last_action_by)
									VALUES( '$product_id',
											'$color_id',
											'".$_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;
	}
?>

