<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

// keep parameter in value
$type = escapeString($_GET['type']) ;

// active page
$active_main_menu = 'setting' ;
$active_sub_menu = 'setting-order' ;
$active_menu = 'page-auto-execution' ;

// check query exsits
$mysqli_page = $mysqli->query("SELECT post_id, post_title FROM system_post
							   WHERE post_type = 'page-auto-execution' AND post_categories = 'page-auto-execution' AND post_trash = '0' LIMIT 1") ;
								
// check if page exists
if ($mysqli_page->num_rows == 0){
	// insert into database
	$mysqli->query("INSERT INTO system_post
					(post_type, post_categories, post_date, post_modified, post_trash) VALUES
					('page-auto-execution', 'page-auto-execution', '".TODAYDATE."', '".TODAYDATE."', '0')") ;
	// set page id in variable
	$page = $mysqli->insert_id ;
	// refresh page
	header("Location:page-auto-execution.php?page=".$page."") ;
	exit ;
}else{
	// set query as array
	$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
	// set page id in variable
	$page = $row_page['post_id'] ;
}

// update database
// keep value in variable
$timeout_hour = resetString(escapeString($_POST['timeout_hour'])) ;
$timeout_minute = resetString(escapeString($_POST['timeout_minute'])) ;
	
if (isset($type) && $type == 'edit' && $_POST['hide'] == 1){
	
	$timeout = ($timeout_hour != '' && $timeout_minute != '' ? ($timeout_hour.':'.$timeout_minute) : '') ;
	
	// keep value in variable
	$page_content = resetString(escapeString($_POST['content'])) ;
	$array_timeout = array('old' => $row_page['post_title'],
						  'new' => $timeout) ;
	
	// update database
	$mysqli->query("UPDATE system_post SET
					post_title = '".$timeout."',
					post_modified = '".TODAYDATE."'
					WHERE post_id = '".$page."'") ;
	
	// refresh page
	header("Location:page-auto-execution.php?page=".$page."&success=1") ;
	exit ;
}

// start header here
include 'requires/page_header.php' ;
include 'requires/page_top.php' ;

$timeout = $row_page['post_title'] ;
$timeout_hour = $timeout_minute = '' ;

if ($timeout != ''){
	$timeout_hour = date('G', strtotime($timeout)) ;
	$timeout_minute = date('i', strtotime($timeout)) ;
}

?>
<div class="warper container-fluid">   	
	<div class="page-header"><h1>Auto Logout <small>Auto Execution</small></h1></div>
	<div class="row">
		<div class="col-md-12">
			<div class="panel panel-default">
				<div class="panel-heading"></div>
				<div class="panel-body">
					<form method="post" class="form-horizontal" id="quotationForm" action="page-auto-execution.php?page=<?= $page ?>&type=edit" novalidate="novalidate">
						<div class="form-group">
							<div class="col-sm-2 control-label">Time For Auto Logout</div>
							<div class="col-sm-7">
                            	<table border="0" cellpadding="0" cellspacing="0">
                                	<tr>
                                    	<td><?= selectForLoopNumber('timeout_hour', $timeout_hour, 1, 24, 'positive', '', 1) ; ?> </td>
                                        <td>Hour</td>
                                        <td width="20">&nbsp;</td>
                                        <td><?= selectForLoopNumber('timeout_minute', $timeout_minute, 0, 60, 'positive', '', 5) ; ?> </td>
                                        <td>Minute</td>
                                    </tr>
                                </table>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-2 control-label"></div>
							<div class="col-sm-7">
								<button type="submit" class="btn btn-purple" style="float:right">Submit</button>
								<input type="hidden" name="hide" value="1">
							</div>
						</div>   
					</form>
				</div>
			</div>
		</div>
	</div>
</div>

<?php
// footer
include 'requires/page_footer.php' ;
?>