<?php
//define this module's name and path
$mn = "odyshop_admin_forbidden_combinations";
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

$marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
$marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
$marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
$marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
$paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
$paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
$paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
$paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
$useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
$widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];

if(empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)){
	$Mmargin = "0";
}else{
	$Mmargin = $marginTop." ".$marginRight." ".$marginBottom." ".$marginLeft." ";
}
if(empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)){
	$Mpadding = "0";
}else{
	$Mpadding = $paddingTop." ".$paddingRight." ".$paddingBottom." ".$paddingLeft." ";
}

global $row; // access to product data
if(!$row){
	echo "Critical Error! No product data found";
	return;
}
$productID = $row->id;

if($useModuleContainer ){
?>
<div id="<?php echo $mn.$sufixID;?>" data-editable="editable">
<?php
}
?>

<style  type="text/css">
<?php
if($useModuleContainer ){
	?>
#<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php 
}	

if($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...

if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php 
}
}
?>
}
<?php
}
?>

</style>
<link href="../modules/<?php echo $mn;?>/css/styles.css" rel="stylesheet" type="text/css">

<script type="text/javascript">

$(document).ready(function(){
	
	$('#open_create_forbidden_combination').fancybox({
		afterShow: function(){
			jQuery.ajax({
				type: "POST",
				url: "ajax_forbidden_combination_create_form.php",
				data: "productID=<?php echo $productID;?>",
				async: true,
				success: function(response){
					$('#create_forbidden_combinations_wrapper').html(response);
				}
			});
		},
		afterClose: function(){
			$('#create_forbidden_combinations_wrapper').html('<img src="images/loading_page.gif" width="50" height="50" alt="loading..." style="margin:100px">');
		},
		width: 900,
		arrows: false,
		helpers : {
			overlay : {
				css : {
					'background' : 'rgba(0, 0, 0, 0.8)'
				}
			},
			title: null
		}
	});
	
});

function massForbiddenCombinationsControls(action){
	if(action == "show"){
		$('#no_forbidden_combinations_content').hide();
		$('.no_forbidden_combinations_content').show();
		$('#massForbiddenCombinationButtons').show();
	}
	if(action == "hide"){
		if(!$('#forbidden-combinations-list li').length) $('#no_forbidden_combinations_content').show();
		$('.no_forbidden_combinations_content').hide();
		$('#massForbiddenCombinationButtons').hide();	
	}		
}

function selectAllForbiddenCombinations(elm){
	setTimeout(function(){
		$('#forbidden-combinations-list').find('[name^=selectedForbiddenCombinationID]').each(function(){
			$(this).prop("checked", !$(this).prop("checked"));
		});
	}, 50);		
}

function getForbiddenCombinationOptions(){
	jQuery.ajax({
		type: "POST",
		url: "ajax_forbidden_combination_options.php",
		data: "action=get&productID=<?php echo $productID;?>",
		async: false,
		success: function(response){
			//console.log(response);
			response = jQuery.parseJSON(response);
			var mode = (!response.length) ? "hide" : "show";
			massForbiddenCombinationsControls(mode);
			if(typeof(response) == "object"){
				var totalForbiddenCombinations = 0;
				for(i in response){
					var record = response[i];
					var id = record.id;
					var title = record.title.replace(/,/g, "<br>");
					var combination = record.combination;
					var exists = false;	
					$('#forbidden-combinations-list').find('li').each(function(){
						if($(this).attr('data-forbidden-combination') == combination){
							exists = true;
							return false;
						}
					});
					if(!exists){
						totalForbiddenCombinations++;
						var template=$("#tplForbiddenCombinations").html();					
						var elm = $('#forbidden-combinations-list').prepend(
							template.format(
								id,
								title,
								combination
							)
						);						
					}
				}

			}else{
				popup("Error: " + response, "popup-warning"); // ajax error
			}			
		}
	});	
}

function manageClick(elm){
	var chk = $(elm).parent().find('input[type="checkbox"]');
	chk.prop("checked", !chk.prop("checked"));
}


function massForbiddenCombinationActions(action){
	var IDs = [];
	$('#forbidden-combinations-list input[type="checkbox"]').each(function(){
		if($(this).prop('checked')){
			var id = $(this).attr("id").split("forbiddenCombinationID")[1];
			IDs.push(id);
			$(this).prop("checked", false);
			$('#selectAllForbiddenCombinationscb').prop("checked", false);
		}
	});
	if(!IDs.length){
		popup("<?php echo t("Δεν έχετε επιλέξει καμία εγγραφή");?>", "popup-warning");
		return false;
	}
	
	if(action == "delete"){
		if(IDs.length > 1){
			if(!confirm("<?php echo t("Θέλετε να αφαιρέσετε αυτές τις απαγορευμένες εκδόσεις του προϊόντος;");?>")) return false;
		}else{
			if(!confirm("<?php echo t("Θέλετε να αφαιρέσετε αυτή την απαγορευμένη έκδοση του προϊόντος;");?>")) return false;
		}
	}	
	IDs = IDs.join(",");
	jQuery.ajax({
		type: "POST",
		url: "ajax_forbidden_combination_options.php",
		data: "productID=<?php echo $productID;?>&action="+action+"&IDs="+IDs,
		async: false,
		success: function(response){
			IDs= IDs.split(",");
			response = response.trim();
			//console.log(response);
			if(response == "ok"){
				for(var n=0; n<IDs.length; n++){
					var id=IDs[n];
					$('a[data-id="'+id+'"]').each(function(){
						if(action == "delete"){
							$(this).parents('.record_row').remove();
						}						
					});
				}
				if(!$('#forbidden-combinations-list li').length){
					massForbiddenCombinationsControls('hide');
				}
			}else{
				popup(response, "popup-warning");
			}
		}
	});	
}

function addProductForbiddenCombination(){
	var qs = [];
	var form_data = $('#createForbiddenCombinations').find("*").serializeArray();
	for(i in form_data) qs.push(form_data[i].name + '=' + form_data[i].value);
	qs=qs.join("&");	
	jQuery.ajax({
		type: "POST",
		url: "ajax_forbidden_combination_options.php",
		data: qs+"&action=create&productID=<?php echo $productID;?>",
		success: function(response){
			//console.log(response);
			response = response.trim();
			if(response == "ok"){
				getForbiddenCombinationOptions();
				$.fancybox.close();
			}else{
				if(response){
					alert(response); // alert whatever went wrong
				}else{
					$.fancybox.close();  // close silently cause combination already exists
				}
			}
		}
	});
}


</script>
<script type="text/template" id="tplForbiddenCombinations">
	<li class="record_row" data-forbidden-combination="{2}">
		<div>
			<div style="clear:both"></div>
			<div class="chk_container">
				<input type="checkbox" id="forbiddenCombinationID{0}" name="selectedForbiddenCombinationID[]" value="{0}">
			</div>
			<a href="javascript:void(0)" onclick="manageClick(this)" data-id="{0}" class="edit_category edit_category_forbidden">{1}</a>
		</div>
	</li>
</script>
<div style="position:relative">   
	<div class="admin_module_title" style="top: -65px;"><h4><?php echo t("Απαγορευμένοι Συνδυασμοί");?></h4></div>  
</div>

<div style="max-width:520px; margin: 20px auto 40px auto; padding:3px;">
<?php
echo t("Ορίστε τους συνδυασμούς των προϊόντων που θα πρέπει να θεωρούνται απαγορευμένοι ώστε να μην δίδεται η δυνατότητα επιλογής τους");
?>
</div>

<div id="create_forbidden_combination">
    <a id="open_create_forbidden_combination" href="#createForbiddenCombinations" onclick="this.blur();"><?php echo t("Δημιουργία απαγορευμένου συνδυασμού");?></a>
</div> 

<div id="productForbiddenCombinations">
    <div id="no_forbidden_combinations_content"><?php echo t("Δεν υπάρχει κανένας απαγορευμένος συνδυασμός");?></div>
    <script type="text/javascript">jQuery(function($){getForbiddenCombinationOptions()});</script>	
   
    
    <div class="admin_form_element no_forbidden_combinations_content">
        <div class="admin_checkbox_wrapper">
        <input id="selectAllForbiddenCombinationscb" type="checkbox" onclick="selectAllForbiddenCombinations(this);" style="margin-top: 1px !important">
        <p style="font-size: .8em; color: grey; font-style:italic"><?php echo t("Επιλογή όλων");?></p>
        </div>
    </div>        
    <div id="forbidden_results_info"></div>
	<ul id="forbidden-combinations-list"></ul>   
</div>
<div id="massForbiddenCombinationButtons"><?php echo t("με τα επιλεγμένα");?>:
    <span style="white-space: nowrap">
    <input type="button" 
    id="massForbiddenCombinationsDelete" 
    value="<?php echo t("Αφαίρεση");?>" 
    class="btn btn-danger btn-medium" 
    onClick="massForbiddenCombinationActions('delete')">        
    </span>
</div>
 


<div id="createForbiddenCombinations">
     <div class="section_title" style="min-width: 270px; margin-bottom: 30px"><?php echo t("Δημιουργία απαγορευμένου συνδυασμού");?></div>
     <div id="create_forbidden_combinations_wrapper">
     	<img src="images/loading_page.gif" width="50" height="50" alt="loading..." style="margin:100px">
     </div>
</div>   

<?php
if($useModuleContainer ){
?>
</div>
<?php 
}
?>
