<?php
    global $vis_language;
    $database=new class_database();
    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
    $object=new class_setting($database);
    $setting=$object->get_settings('(title="vis_manage_portal_logo" OR title like "vis_manage_logo" OR title like "vis_manage_portal_text" or title="vis_manage_portal_text_bold" or title="vis_manage_portal_text_italic" or title="vis_manage_portal_text_colour" or title="vis_manage_portal_language") and domain_id=0', VIS_STAFF_ID, VIS_LOCATION_AREA);   
	$style = '';
    if($setting!=-1) for($i=0;$i<count($setting);$i++) {
		if($setting[$i]['title']=='vis_manage_portal_logo') {
			$poral_setting['vis_manage_portal_logo']=$setting[$i]['content'];
		}
		elseif($setting[$i]['title']=='vis_manage_portal_text') {
			$poral_setting['vis_manage_portal_text']=$setting[$i]['content'];
		}
		elseif($setting[$i]['title']=='vis_manage_logo') {
			$poral_setting['vis_manage_logo']=$setting[$i]['content'];
		}
		elseif($setting[$i]['title']=='vis_manage_portal_text_bold') {
			$poral_setting['vis_manage_portal_text_bold']=$setting[$i]['content'];
			$style .= ($setting[$i]['content'] == 1) ? 'font-weight: bold; ' : '';
		}
		elseif($setting[$i]['title']=='vis_manage_portal_text_italic') {
			$poral_setting['vis_manage_portal_text_italic']=$setting[$i]['content'];
			$style .= ($setting[$i]['content'] == 1) ? 'font-style: italic; ' : '';
		}
		elseif($setting[$i]['title']=='vis_manage_portal_text_colour') {
			$poral_setting['vis_manage_portal_text_colour']=$setting[$i]['content'];
			$style .= 'color: '.$setting[$i]['content'].'; ';
		}
		elseif($setting[$i]['title']=='vis_manage_portal_language') {
			$poral_setting['vis_manage_portal_language']=$setting[$i]['content'];
		}
	}
	$database->database_disconnect();
    include_once(VIS_PATH_TEMPLATE."header.php");?>
	<? if($poral_setting['vis_manage_logo'] == 'text') { ?><style>.vh-logo .logo-text {<?= $style; ?>}</style><? } else if($poral_setting['vis_manage_portal_logo'] != "" && file_exists(VIS_PATH_PROFILE.$poral_setting['vis_manage_portal_logo'])) { 
		$imageFile = VIS_PATH_PROFILE.$poral_setting['vis_manage_portal_logo'];				 
		$width = 300;	/* Set the width fixed at 300px; */
		$info = getimagesize($imageFile); /* Get the image info */		 
		$aspectRatio = $info[1] / $info[0]; /* Calculate aspect ratio by dividing width by height*/		 
		$newheight = (int)($aspectRatio * $width) . "px";/* Keep the width fix at 300px, but change the height according to the aspect ratio */		
		$width .= "px";
	?><style>.vh-logo .logo-image { background-image: url("<?= VIS_PATH_PROFILE.$poral_setting['vis_manage_portal_logo']; ?>") !important; width: <?= $width; ?> !important; height: <?= $newheight;?> !important; background-size: <?= $width; ?> auto !important; }</style>
	<? } ?>
<body>
    <section class="vbox">
      <section id="vis_main_panel_div">
    <div class="container login_aside_xl">
		<?
		if($poral_setting['vis_manage_logo']=='text') ?>
        <div class="vh-logo margin-v">
        	<h1><a class="logo-<?= ($poral_setting['vis_manage_logo']=='text') ? 'text' : 'image'; ?>" href=""><?= ($poral_setting['vis_manage_logo']=='text')?$poral_setting['vis_manage_portal_text']:''; ?></a></h1>
        </div>
        <section class="m-b-lg">
            <header class="vis_login_head">
				<div id="vis_login_msg_div" class="alert" style="display:none;"><strong></strong></div>	
                <? if(isset($_SESSION['successmessage']) && $_SESSION['successmessage']!=''){ ?>
                    <div id="vis_login_msg_div" class="alert alert-success"><strong><?= $_SESSION['successmessage']; ?></strong></div>	
                <? unset($_SESSION['successmessage']); } ?>
			</header>
            <form method="post" id="vis_login_form">
				<div id="vis_login_table">
                    <div class="list-group">
						<div class="login-list-group-item">
							<input type="text" name="vis_txtusername" id="vis_txtusername" class="form-control no-border" placeholder="<? echo $vis_language['TXTUSERNAME']; ?>">
						</div>
					</div>
                    <div class="list-group">
						<div class="login-list-group-item">
							<input type="password" name="vis_txtuserpass" id="vis_txtuserpass" class="form-control no-border" placeholder="<? echo $vis_language['TXTPASSWORD']; ?>">
						</div>
					</div>
                    <div class="row m-b-lg m-t-lg">
                        <div class="col-xs-12"><button id="btn_login" type="submit" data-loading-text="Loading..." data-complete-text="finished!" class="btn btn-lg btn-primary btn-block"><? echo $vis_language['TXTLOGIN']; ?></button></div>
                    </div>
					<div class="m-t m-b-lg" style="line-height: 30px;">
						<div class="vis_fl">
							<a class="link" href="#" onClick="$('#vis_login_table').hide(); $('.alert.alert-danger').hide();$('#vis_forget_pass_table').show();"><small><? echo $vis_language['TXTFORGOTPASSWORD']; ?>?</small></a>
						</div>
						<div>
							<div id="divlang" class="btn-group vis_fr<? if(count($languages) > 5) { echo ' dropup'; } ?>">
								<button id="vis_language_button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown"><?php echo $vis_language['TXTLANGUAGE']; ?> <span class="caret"></span></button>
								<ul class="dropdown-menu">
								<?php for($i=0;$i<count($languages);$i++) { ?>
									<li>
										<a href="javascript:void(0);" onClick="$('#vis_language').val(<? echo $languages[$i]['language_id']; ?>); $('#vis_language_button').text('<? echo $languages[$i]['language_name']; ?>').append('<? echo " <span class=\'caret\'></span>"; ?>')"><? echo $languages[$i]['language_name']; ?></a>
									</li>
								<? } ?>
							  </ul>
							</div>
						</div>
					</div>
                </div>
                <div id="vis_forget_pass_table" style="display:none">
					<div class="list-group">
						<div class="login-list-group-item">
							<input type="text" name="vis_useremail" id="vis_useremail" placeholder="<? echo $vis_language['TXTEMAIL']; ?>" class="form-control no-border">
						</div>
					</div>
					<button id="btn_forget_pass" data-loading-text="Loading..." type="submit" class="btn btn-lg btn-primary btn-block"><? echo $vis_language['TXTSEND']; ?></button>
					<div class="text-center m-t m-b"><a class="link" href="#" onClick="$('#vis_forget_pass_table').hide(); $('#vis_login_table').show()"><small><? echo $vis_language['TXTBACKTOLOGIN']; ?></small></a></div>
                </div>
                <div class="line line-dashed"></div>
                <input type="hidden" name="vis_login_check" id="vis_login_check" value="1" />
                <input type="hidden" name="vis_language" id="vis_language" value="<? echo (isset($poral_setting['vis_manage_portal_language']))?$poral_setting['vis_manage_portal_language']:1; ?>" />
            </form>
        </section>
    </div>
    <!-- footer -->
	<footer id="footer" class="m-t-lg">
		<div class="text-center padder">
			<p>
				<small><? echo VIS_BRANDING_LINK; ?></small>
			</p>
		</div>
	</footer>
</section>
</section>
<script>
    $("#vis_main_panel_div").addClass('scrollable');
    $("div.vis_main_panel").removeClass("vis_main_panel");
    $("#vis_login_area_anim").hide();
	$(document).ready(function(){ 	
		<?if($_COOKIE['OP_LOGOUT']==1){?>
		$("#divlang").click(function()
		{
			if($( "#divlang" ).hasClass('open')==true){
				$("#divlang").removeClass('open');
			}else $("#divlang").addClass('open');
		}); <? } ?>
		$("#vis_login_form").submit(function(e)
		{	
			var vis_operation = btn_id = btn_title = '';
			if( $("#vis_login_table").is(":visible") ){
				vis_operation = 'vis_login_check';
				btn_id = 'btn_login';
				btn_title = '<? echo $vis_language['TXTLOGIN']; ?>';
			}
			else if( $("#vis_forget_pass_table").is(":visible") ){
				vis_operation = 'forgot_password';
				btn_id = 'btn_forget_pass';
				btn_title = '<? echo $vis_language['TXTUPDATE']; ?>';
			}
			var btn = $("#"+btn_id);
			btn.button('loading');
			$.ajax({
				type: "POST",
				url: 'index.php?vis_tmpst=new Date().getTime()',
				data: { 'vis_action': $("#vis_login_form").serialize(), 'vis_module': 'home', 'vis_operation': vis_operation, 'vis_form': 'vis_login_form' },			  
				success: function(data){
					if(data != '') {
						btn.removeClass('disabled').removeAttr('disabled').html(btn_title);
						var arr = [ '<?= $vis_language['MSGINCOMPLETEINFO']?>', '<?= $vis_language['MSGINVALIDLOGINDETAILS']?>','<?= $vis_language['MSGPASSSET']?>', '<?= $vis_language['MSGEMAILMISS'] ?>', '<?= $vis_language['MSGIPBAN'] ?>'];					
						if( (match = jQuery.inArray( data, arr )) != -1 ){
							$("#vis_login_form")[0].reset();
							var msg_class = (match == '2') ? 'alert-success' : 'alert-danger'; 
							$("#vis_login_msg_div strong").html(data).parent().addClass(msg_class).show().fadeOut(15000);
							setTimeout( function(){ $("#vis_login_msg_div").removeClass(msg_class); }, 15000);
						}
						else
							window.location.reload();
					}
				}
			});
			return false; // avoid to execute the actual submit of the form.
		});
	});
</script>
<? include_once(VIS_PATH_TEMPLATE."footer.php"); ?>
