<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 12/3/2015
 * Time: 10:55 AM
 */
$show_init_form = false;	/* To display or not the initialization form */

/* Try to see if the call is from administration panel */
if(isset($admin_catdom_module_id) && $admin_catdom_module_id>0 && isset($_GET['action']) && $_GET['action'] == "custom_module") {
	$show_init_form = true;
	// Initialize defaults
	$ribbon_promo_pages = array();
	$ribbon_promo_pages_link = [];
	$ribbon_promo_pages_icons = 0;
    $ribbon_promo_settings = [
        "slides_per_view" => 5,
        "effect" => null
    ];
	$current_catdom_module_id = $admin_catdom_module_id;
}

if($show_init_form) {

	//echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';

	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}

	$_catdom_id = 0;
	$_parent_id = 0;

	if(!isset($admin_catdom_id)) {

		$q = "select * from left_content where catdom_module_id='$current_catdom_module_id';";
		//echo $q;
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r)>0) {
			$row = mysql_fetch_assoc($r);

			$_catdom_id = intval($row['catdom_id']);
		}
	}
	else {
		$_catdom_id = $admin_catdom_id;
	}
	$categ_title = '';
	if($_catdom_id > 0) {
		$q = "select * from domaincategories where catdom_id='$_catdom_id';";
		//echo "<br/>$q";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) == 1) {
			$row = mysql_fetch_assoc($r);
			$_parent_id = intval($row['parent_id']);


			$categ_title = stripslashes($row['title_en']);


			if($_parent_id == 0) {
				$_parent_id = $_catdom_id;
			}
		}


	}

	$promo_pages = array();
	$q = "SELECT * FROM domaincategories WHERE parent_id='$_parent_id' AND active='yes' AND is_promo_cat='1' ORDER BY catdom_group ASC, order_seq ASC, title_el ASC";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while($row = mysql_fetch_assoc($r)) {
			$promo_pages[$row['catdom_id']] = array_map('stripslashes', $row);
		}
	}
	?>

	<style type="text/css">
		.inline_container input:disabled, .inline_container select:disabled, .inline_container textarea:disabled {
			background-color: #efefef;
			border-color: #ccc;
			border-style: solid;
			border-width: 1px;
			color: #ccc;
			font-size: 11px;
			margin: 1px 0;
		}
	</style>

	<div class="inline_container">
		<form method="POST" action="custom_module_update.php" name="ribbon_promo_buttons_frm" id="ribbon_promo_buttons_frm">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<th colspan="3" align="left">
						Promo Pages Ribbon Customization
					</th>
				</tr>

				<tr>
					<td colspan="3">
						Select Promo Pages to place on
						<b style="color: #ff0000;"><?=$categ_title;?></b> page
						<hr>
					</td>
				</tr>
				<tr>
					<td class="itdh">Use Icons?</td>
					<td colspan="2">
						<input type="radio" name="ribbon_promo_pages_icons" value="1" <?php echo (intval($ribbon_promo_pages_icons) == 1 ? 'checked' : '');?>> YES
						<input type="radio" name="ribbon_promo_pages_icons" value="0" <?php echo (intval($ribbon_promo_pages_icons) == 0 ? 'checked' : '');?>> NO
					</td>
				</tr>

				<?php
				if(count($promo_pages) > 0) {
					$group = '';
					foreach($promo_pages as $extra_catdom_id=>$page_data) {
						$catdom_group = trim($page_data['catdom_group']);
						if($catdom_group != $group) {
							$group = $catdom_group;
							echo '<tr><th colspan="3"><b style="color:#ff0000;">'.$group.'</b></th> </tr>';
							?>
							<tr>
								<td class="itdh">#</td>
								<td class="itdh" width="50%">Page Title</td>
								<td class="itdh" width="50%">Link Title</td>
							</tr>
							<?php
						}
						$page_title = $page_data['title_en'];
						//$page_title .= ' | ' . preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $page_data['title_el']));
						$link_title = "";//preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $page_data['title_el']));
						//$checked = count($ribbon_promo_pages) == 0 ? 'checked="checked"' : '';
						$checked = '';
						$disabled = 'disabled="disabled"';
						if(in_array($extra_catdom_id, $ribbon_promo_pages)) {
							$checked = 'checked="checked"';
							$disabled = '';
							if(isset($ribbon_promo_pages_link[$extra_catdom_id])) {
								$link_title = $ribbon_promo_pages_link[$extra_catdom_id];
							}
						}
						?>
						<tr>
							<td class="itdh"><input type="checkbox" name="ribbon_promo_pages[<?=$extra_catdom_id?>]" value="<?=$extra_catdom_id?>" <?=$checked?> class="xpck" data-catdom_id="<?=$extra_catdom_id?>"></td>
							<td class="itd_lb" width="50%"><span><?=$page_title?></span></td>
							<td class="itd_lb" width="50%">
								<input type="text" name="ribbon_promo_pages_link[<?=$extra_catdom_id;?>]" <?=$disabled;?> id="ribbon_promo_pages_link_<?=$extra_catdom_id;?>" value="<?=$link_title;?>" style="width: 100%;">
							</td>
						</tr>
						<?php
					}
				}
				?>

                <tr>
                    <td colspan="2" class="itdh">Ribbon Settings</td>
                </tr>

                <tr>
                    <td class="itdh">Slides Per View</td>
                    <td class="itd_lb"><input type="number" name="ribbon_promo_settings[slides_per_view]" value="<?=$ribbon_promo_settings['slides_per_view'];?>" style="width:160px;"> </td>
                </tr>

				<tr>
					<td nowrap class="itdh">Use defaults?</td>
					<td class="itd" colspan="2">
						<input type="radio" name="use_defaults" value="1" style="vertical-align:middle;"/> Yes
						<input type="radio" name="use_defaults" value="0" checked style="vertical-align:middle;"/> No
					</td>
				</tr>
				<tr>
					<td colspan="3" align="right" class="footer">
						<input type="reset" value="Reset" name="submit" class="submit_button"/>
						<input type="submit" value="Save" name="submit" class="submit_button"/>
						<input type="hidden" value="<?php echo $current_catdom_module_id; ?>" name="admin_catdom_module_id" id="admin_catdom_module_id"/>
					</td>
				</tr>
			</table>
		</form>
	</div>

	<script type="text/javascript">
		$('.xpck').bind('click', function(e){
			var $ck = $(this);
			var that = this;
			var is_selected = that.checked;
			var catdom_id = $ck.data('catdom_id');
			var $el_edit = $('#ribbon_promo_pages_link_' + catdom_id);

			if(is_selected) {
				$el_edit.attr('disabled', false);
			}
			else {
				$el_edit.attr('disabled', true);
			}

			console.log();
		});
	</script>
	<?php

	$module_forms = array(
		"form_name" => "ribbon_promo_buttons_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$ribbon_promo_pages = array();
    $ribbon_promo_pages_link = [];
    $ribbon_promo_settings = [
        "slides_per_view" => 5,
        "effect" => null
    ];

	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
