<?php

$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;
    $current_catdom_module_id = $admin_catdom_module_id;

	// Get domain
	$p_catdom_id = intval($_REQUEST['catdom_id']);
	$p_domain_id = $p_catdom_id;
	$q = "select * from domaincategories where catdom_id='$p_catdom_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		if(intval($row['parent_id'])>0) $p_domain_id = intval($row['parent_id']);
	}


	$all_domains = [];
	$q = "SELECT * FROM domaincategories
          WHERE
            catdom_id!='$p_catdom_id'
            AND parent_id=0
            AND addonname NOT LIKE '%/%'
            AND active='yes'
          ORDER BY addonname ASC";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        while($row = mysql_fetch_assoc($r)) {
            $_catdom_id = $row['catdom_id'];
            $_domainname = $row['domainname'];
            $_title = trim(stripslashes($row['c_title_en']));
            $_ssl_enabled = intval($row['ssl_enabled']) > 0;

            $all_domains [$_catdom_id] = [
                'url' => ($_ssl_enabled ? 'https://': 'http://') . $_domainname,
                'title' => $_title != "" ? $_title : '*** ' . $_domainname,
                'checked' => ''
            ];
        }
    }

	// Initialize defaults

	$sp_title = null;
	$sp_list = [];
    $sp_json = null;
    $sp_hide_title = 0;
    $sp_max = 0;

}

if($show_init_form) {
	
	?>
	<style type="text/css">
		#selected_categs ul {
			margin:4px;
			padding:0;
		}
		
		#selected_categs ul li {
			list-style:none;
			margin: 3px;
            padding: 5px 10px;
		}

        #selected_categs ul li.active {
            background-color: #fbfd2d;
        }

        div.counter {
            border:1px solid #ddd;
            background-color: #efefef;
            padding: 5px 10px;
        }
	
	</style>
	<?php
	//print_r($module_params);
	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}

    //echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';
	
	if(count($sp_list) > 0) {
        //$selected_domains = array_keys($sp_list);
        foreach($all_domains as $_catdom_id=>$_data) {
            if(in_array($_catdom_id, $sp_list)) {
               $all_domains[$_catdom_id]['checked'] = 'checked="checked"';
            }
        }
    }
	?>
	<form method="POST" action="custom_module_update.php" name="side_portals_frm" id="side_portals_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="2" align="left">
					Side Linked Portals
				</th>
			</tr>
			<tr>
				<td colspan="2">
					<div class="inline_container">
						<table cellpadding="0" cellspacing="0" border="0" width="100%">
							<tr>
								<td class="itd" align="left" nowrap width="20%"><strong>Module Title:</strong></td>
								<td class="itd" align="left" width="80%"><input type="text" name="sp_title" id="sp_title" value="<?php echo $sp_title;?>" maxlength="64" style="width: 95%;"></td>
							</tr>
							<tr>
								<td class="itd" align="left" nowrap width="20%"><strong>Hide Title:</strong></td>
								<td class="itd" align="left" width="80%"><input type="checkbox" name="sp_hide_title" id="sp_hide_title" value="1" <?php echo (intval($sp_hide_title) ==1) ? 'checked="checked"' : '';?> style="vertical-align: middle;"></td>
							</tr>
							<tr>
								<td class="itd" align="left" nowrap width="20%"><strong>MAX Elements:</strong></td>
								<td class="itd" align="left" width="80%"><input type="text" name="sp_max" id="sp_max" value="<?php echo $sp_max;?>" maxlength="3" size="3"> Put 0 (zero) to show all</td>
							</tr>
						</table>	
					</div>
				</td>
			</tr>
            <tr>
                <th colspan="2" align="left">
                    Portal Selection List
                </th>
            </tr>
            <tr>
                <td colspan="2" class="itd">

                    <div class="inline_container">
                        <div class="counter">
                        <span style="color: red;">
                            <b id="counter"><?=count($sp_list);?></b> portals selected
                        </span> |
                        <button type="button" id="clear_all">Deselect All</button>
                        </div>
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td>
                                    <div id="selected_categs" style="height:360px; overflow-y:auto;">
                                        <ul>
                                            <?php
                                            foreach($all_domains as $_catdom_id=>$_data) {
                                                $li_class = $_data['checked'] ? 'active' : '';

                                                ?>
                                                <li id="all_<?=$_catdom_id;?>" class="<?=$li_class;?>">
                                                    <input name="sp_list[]" class="ck-portal" type="checkbox" value="<?=$_catdom_id;?>" <?=$_data['checked'];?>> <a href="<?=$_data['url'];?>" target="_blank"><?=$_data['title']?></a>
                                                </li>
                                                <?php
                                            }
                                            ?>
                                        </ul>
                                    </div>
                                </td>
                            </tr>
                        </table>

                    </div>
                </td>
            </tr>
			
			<tr>
				<td colspan="2" 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>

	<script type="text/javascript">
		//$(document).ready(function(){

        var loaded = null;
        var $cnt = $('#counter');

        function incCounter($cnt) {
            console.log(parseInt($cnt.text(), 10));
            var counter = parseInt($cnt.text(), 10) + 1;
            console.log(counter);
            console.log('inc 1');
            $cnt.text(counter);
        }

        function decCounter($cnt) {
            console.log(parseInt($cnt.text(), 10));
            var counter = parseInt($cnt.text(), 10) - 1;
            console.log(counter);
            console.log('dec 1');
            $cnt.text(counter);
        }

        if(loaded == null) {
            $('.ck-portal').die('click');
            $('.ck-portal').live('click', function (e) {
                var li = $(this).closest('li');
                var checked = $(this).prop('checked');

                if (checked) {
                    li.addClass('active');
                    incCounter($cnt);
                }
                else {
                    li.removeClass('active');
                    decCounter($cnt);
                }

            });

            $('#clear_all').die('click').live('click', function (e) {
                $('li.active > input').each(function (i) {
                    $(this).prop('checked', false);
                    decCounter($cnt);

                    $(this).closest('li').removeClass('active');
                });
            });

            loaded = 1;

        }
		//});
	</script>
	<?php
	
	$module_forms = array(
		"form_name" => "side_portals_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
    $sp_title = null;
    $sp_list = [];
    $sp_json = null;
    $sp_hide_title = 0;
    $sp_max = 0;
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		
		if(isset($_REQUEST['debug'])) {
			echo '<pre>', var_export($module_params), '</pre>';
		}
		
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
