<?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 = 'languages';
			// Table's primary key
			$primaryKey = 'language_id';
			$columns =  array(
				  			  array( 'db' => 'language_id', 'dt' => 0 ),
			    				array( 'db' => 'language_name', 'dt' => 1 ),
			    				array( 'db' => 'language_localazation', 'dt' => 2 ),
			    				array( 'db' => 'language_symbol',   'dt' => 3  ),
								array( 'db' => 'language_dir',   'dt' => 4 ),
								array( 'db' => 'is_defualt',   'dt' => 5 )
			);
			// 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 languages WHERE is_deleted = 0");
			if ($res) {
				$op  = array();
				while ($row = $res->fetch_assoc()) {
				    // do what you need.
						$result[0] = $row['language_id'];
						$result[1] = $row['language_name'];
						$result[2] = $row['language_symbol'];
						array_push($op,$result);
				}
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}


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

			$id 								= $_POST['obj_id'];
			$language_symbol		=addslashes($_POST["language_symbol"]);
            $language_name			=addslashes($_POST["language_name"]);
            $language_localazation	=addslashes($_POST["language_localazation"]);
            $language_dir			=addslashes($_POST["language_dir"]);
            $is_defualt				=addslashes($_POST["is_defualt"]);
			
			$op  = array('is_success'=>false, 'data' => '');
			$lastaction = "Update languages ";

			$res = $db->query("SELECT * FROM languages WHERE is_deleted=0 AND language_symbol = '$language_symbol' AND language_localazation = '$language_localazation' AND language_id != '$id'");

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



			$lastaction = "Update language information";
			$update_qry = "UPDATE languages SET  language_symbol = '$language_symbol', language_name='$language_name' , language_localazation='$language_localazation', language_dir='$language_dir', is_defualt='$is_defualt',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE language_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 language";


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

			$res = $db->query("SELECT * FROM languages_keywords WHERE is_deleted=0  AND language_id_fk = '$id'");
			if (mysqli_num_rows($res)>0) {
			
				$op['is_success'] = false;
				$op['data'] 	  = "لا يمكنك حذف هذه اللغة فهنالك كلمات مترجة في جدول الترجمة ";
				echo json_encode($op);
				exit;
			}

			
			

			


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

            $language_symbol		=addslashes($_POST["language_symbol"]);
            $language_name			=addslashes($_POST["language_name"]);
            $language_localazation	=addslashes($_POST["language_localazation"]);
            $language_dir			=addslashes($_POST["language_dir"]);
            $is_defualt				=addslashes($_POST["is_defualt"]);


						$res = $db->query("SELECT * FROM languages WHERE language_symbol = '$language_symbol' AND language_name ='$language_name' AND language_localazation ='$language_localazation' ");
						if (mysqli_num_rows($res)>0) {
							$op['is_success'] = false;
							$op['data'] 	  = "خطأ الاسم مكرر";
							echo json_encode($op);
							exit;
						}else{
							
							$delete = 0;
							$reg_date = date("Y-m-d h:i:s", time());
							$lastaction = "Create New language";

						$sql ="INSERT INTO languages (language_symbol,language_name,language_localazation,language_dir,is_defualt,created_by,last_action,last_action_by)
						VALUES('$language_symbol','$language_name','$language_localazation','$language_dir','$is_defualt','".$_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;
	}
?>

