<?php
//define this module's name and path
$mn = basename(__FILE__ , ".php");
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;
$inactiveTitle = t("η εγγραφή αυτή είναι ανενεργή");
$activeTitle = t("κάντε κλικ για επεξεργασία των χαρακτηριστικών");


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(){
	
	setSelectOptionsColor();
	$('select[data-rel="pf"]').each(function(){
		$(this).trigger("change");
	});
		
});

function setSelectOptionsColor(){
	$('select[data-rel="pf"]').each(function(){
		$(this).on("change", function(e) { 
			var span = $(this).prev().children('a').children('span');
			if(span.text() == "<?php echo t("Χωρίς επιλογή");?>"){
				span.css("color", "silver");
			}else{
				span.css("color", "black");
			}
		});
	});	
}

function saveFeatures(){
	var qs = [];
	var form_data = $('#productFeatures').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_feature_variants.php",
		data: qs+"&action=save&productID=<?php echo $productID;?>",
		success: function(response){
			response = response.trim();
			if(response.indexOf("ok") != -1){
				popup("<?php echo t("Τα χαρακτηριστικά του προϊόντος ενημερώθηκαν με επιτυχία");?>", "popup-info");
				// do other stuff on success
			}else{
				// error popup
				popup(response, "popup-warning");
			}
		}
	});	
}
</script>
<div style="position:relative">   
	<div class="admin_module_title" style="top: -70px"><h4><?php echo t("Χαρακτηριστικά προϊόντος");?></h4></div>  
</div>

<div style="text-align: center; margin:5px;">
    
    <div id="productFeatures">
<?php
        $productCategoriesArray = $content->getMultipleValues("products", "catID", $productID);
        $productCategories = implode(",", $productCategoriesArray);
        $SL_sectionID = $system->getTableSectionID('product_features');
        $SL_fieldID = $system->getFieldID('related_categories', $SL_sectionID);
        $q = "select distinct a.id, b.title 
                from product_features as a, 
                content_product_features as b, 
                selectlists as s  
                where a.id=b.mainID 
                and b.langID='$langID' 
                and a.active=1 
                and s.sectionID='$SL_sectionID' 
                and s.fieldID='$SL_fieldID' 
                and s.itemID=a.id 
                and s.optionID IN (".$productCategories.") order by a.sort";
		$features = $db->getRecords($q);
        if(!count($features)){
            $messages->addMessage(t("Δεν βρέθηκαν διαθέσιμα χαρακτηριστικά που να σχετίζονται με τις κατηγορίες αυτού του προϊόντος"));
            ?>
            <div  style="margin: 25px; padding: 4px;"><!-- do not remove --></div>
            <div style="max-width:450px; margin: 0 auto">
            <?php
            $messages->printSystemMessages();
            ?>
            </div>
            
            <?php
        }else{
			?>
			<div class="section_title" style="min-width: 270px; margin-bottom: 50px"><?php echo t("Επιλέξτε τα χαρακτηριστικά αυτού του προϊόντος");?></div>
			<?php
			$products_features_variants = $db->getRecords("select variantID from features where productID='$productID'");
			$pfv = array();
			foreach($products_features_variants as $elm){
				$pfv[] = $elm->variantID;
			}
			foreach($features as $feature){
				$featureID = $feature->id;
				$featureTitle = $feature->title;
				$feature_variants = $db->getRecords("select fv.id, cfv.title 
																		from feature_variants as fv, content_feature_variants as cfv 
																		where fv.featureID='$featureID' 
																		and fv.id=cfv.mainID 
																		and cfv.langID='$langID' 
																		order by fv.sort");
				?>
            <div class="admin_form_element">
                <div class="admin_label" style="padding-right:17px;">
                <p><?php echo $featureTitle;?> : </p>
                </div>
                <div>
                    <select data-rel="pf" 
                    id="product_features_<?php echo $featureID;?>" 
                    name="feature_variants[fv_<?php echo $featureID;?>]" 
                    data-type="advanced" 
                    class="form_control" 
                    style="width: 290px;">
	                    <option value="0" style="color:silver"><?php echo t("Χωρίς επιλογή");?></option>
                    <?php
                    foreach($feature_variants as $fv){
                        $id = $fv->id;
                        $title = $fv->title;
                        ?> 
                        <option value="<?php echo $id;?>" <?php if(in_array($id, $pfv)) echo 'selected="selected"';?>><?php echo $title;?></option>
                        <?php
                    }
                    ?>
                    </select>
                </div>
            </div>                           
                <?php
			}
			?>
            <div class="admin_form_element" style="margin-top: 20px;">
            <input class="btn btn-success" value="<?php echo t("Αποθήκευση χαρακτηριστικών");?>" type="submit" onclick="saveFeatures()">
            </div>
            <?php			 
		}
?>
	</div>    
</div>



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