<?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']);


 // check to see if user has been set the defualt language if he havnt we will show the defualt language
$CSS 	= 'css';
$LANG 	= 'العربية';
$DIR 	= 'rtl';


$op  = array();
if($_SESSION['language_id'] >0){

	require_once "db_connect.php";

	$res = $db->query("SELECT keyword,translated_keyword,language_name,language_dir FROM keywords KW 
						LEFT JOIN languages L ON (L.is_deleted=0 )
						LEFT JOIN languages_keywords LKW ON (LKW.is_deleted = 0  AND LKW.keyword_id_fk = KW.keyword_id AND LKW.language_id_fk = L.language_id)
						WHERE KW.is_deleted = 0 AND L.language_id =".$_SESSION['language_id']);
	if ($res) {
		
		while ($row = $res->fetch_assoc()) {
		    // do what you need.
		    	
				$result['keyword'] 				= trim($row['keyword']);
				$result['translated_keyword'] 	= trim($row['translated_keyword']);
				$result['language_name'] 		= trim($row['language_name']);
				$result['language_dir'] 		= trim($row['language_dir']);
				
				array_push($op,$result);
		}
		
		if($op[0]['language_dir'] =='ltr'){
			$CSS = 'css_ltr' ;
			$DIR 	= 'ltr';
		}

		$LANG 	= $op[0]['language_name'] ;

	}
}
	// now we should apply JavaScript to translate the keywords
?>
	
<script >
	var CSS_PATH 		 		= '<?php echo $CSS; ?>';
	var LANGUAGE_NAME 	 		= '<?php echo $LANG; ?>';
	var LANGUAGE_DIR 	 		= '<?php echo $DIR; ?>';

	var Left_Arraw				= 'l';
	var Right_Arraw				= 'r';

	if(LANGUAGE_DIR == 'ltr'){
		Left_Arraw				= 'r';
		Right_Arraw				= 'l';
	}
	

	var Keyword_Array_Js 	 	= <?php echo json_encode($op) ; ?>;

	function Auto_Translate1(){
		
		var Js_Array_len 	= Keyword_Array_Js.length;

		$(".multi_lang").each(function() {

		  	var Item_Text 	= $(this).text();
		  	if(Item_Text==null || Item_Text == undefined || Item_Text ==''){
		  		Item_Text 	= $(this).attr('placeholder');
		  	}

		  	if(Item_Text !=null && Item_Text != undefined && Item_Text !=''){
		  		


			   	for (var j = 0; j < Js_Array_len ; ++j) {
			    	
			    	if(Item_Text.trim() == Keyword_Array_Js[j].keyword){
			    		
			    		var Translated_key = Keyword_Array_Js[j].translated_keyword;
			    		if(Translated_key ==null || Translated_key == ''){
			    			Translated_key = Keyword_Array_Js[j].keyword
			    		}
			    		$(this).text(Translated_key);
			    		$(this).attr('placeholder',Translated_key);
			    		break;
			    	}
			    } // for j
		  	}	
		});
	}
	

	function Auto_Translate(){
		
		var Js_Array_len 	= Keyword_Array_Js.length;

		$(".multi_lang").each(function() {

		  	var Item_Text 	= $(this).text();
		  	if(Item_Text==null || Item_Text == undefined || Item_Text ==''){
		  		Item_Text 	= $(this).attr('placeholder');
		  	}

		  	if(Item_Text !=null && Item_Text != undefined && Item_Text !=''){
		  		
		  		var is_Item_Text_in_database = false;

			   	for (var j = 0; j < Js_Array_len ; ++j) {
			    	
			    	if(Item_Text.trim() == Keyword_Array_Js[j].keyword){

			    		is_Item_Text_in_database = true;
			    		
			    		var Translated_key = Keyword_Array_Js[j].translated_keyword;
			    		if(Translated_key ==null || Translated_key == ''){
			    			Translated_key = Keyword_Array_Js[j].keyword
			    		}
			    		$(this).text(Translated_key);
			    		$(this).attr('placeholder',Translated_key);
			    		break;
			    	}
			    } // for j

			    // now we check if is_Item_Text_in_database = false that mean this key words not in database and we should auto insert it :)

			    if(is_Item_Text_in_database == false){
			    	auto_add_keyword(Item_Text.trim());
			    	
			    }
		  	}	
		});
	}

	function Translation(msg){

		var translated_msg 	= msg;
		var is_Item_Text_in_database = false;

		if(msg !=null && msg != undefined && msg !=''){
			var len_array		= Keyword_Array_Js.length;
		    for (var j = 0; j < len_array; ++j) {
		    	if(msg.trim() == Keyword_Array_Js[j].keyword){
			    	is_Item_Text_in_database = true;

		    		var Translated_key = Keyword_Array_Js[j].translated_keyword;
		    		if(Translated_key ==null || Translated_key == ''){
		    			Translated_key = Keyword_Array_Js[j].keyword
		    		}
		    		return Translated_key;
		    	}
		    }
		}

		if(is_Item_Text_in_database == false){
			    	auto_add_keyword(msg.trim());
			    	
		}
		
		return translated_msg;
	}


	function auto_add_keyword(key_){
		

	}

	function auto_add_keyword1(key_){
		$.ajax({
            url: "models/crul_translation.php",
            type:"POST",
            dataType : "JSON",
           
            data : {keyword:key_,type:'create'}
        })
        .done(function(res) {
            //alert(res);
            if(res.is_success == true){
              
            	console.warn(res.data);

                
           
            }else{
               
            	console.error(res.data);

            }
           
        })
        .fail(function(err) {
            

            console.error(err);

            
        })
        .always(function() {
            console.log("complete");
        });

	}
	

</script>
	

