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

// check permission
if ( !permissionCheck($row_user, 'setting') ){
	header('Location: index.php') ;
	exit ;
}

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

// active page
$active_main_menu = 'setting' ;
$active_sub_menu = 'setting-hr' ;
$active_menu = 'setting-hr-advance' ;

// check query exsits
$mysqli_page = $mysqli->query("SELECT post_id, post_title, post_link, post_content FROM system_post
							   WHERE post_type = 'page-advance' AND post_categories = 'page-advance' 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-advance', 'page-advance', '".TODAYDATE."', '".TODAYDATE."', '0')") ;
	// set page id in variable
	$page = $mysqli->insert_id ;
	// refresh page
	header("Location:setting-advance.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'] ;
}
	
if (isset($type) && $type == 'edit' && $_POST['hide'] == 1){
		
	// 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 = '".escapeString($_POST['advance_from'])."',
					post_link = '".escapeString($_POST['advance_to'])."',
					post_content = '".escapeString($_POST['advance_remark'])."',
					post_modified = '".TODAYDATE."'
					WHERE post_id = '".$page."'") ;
			
	// refresh page
	header("Location:setting-advance.php?page=".$page."&success=1") ;
	exit ;
}

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

?>
<div class="warper container-fluid">   	
<div class='container' style="background-color: white; border-radius: 10px;">
	<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
		<h1><?= $lang['advance'] ?> <small><?= $lang['setting'] ?></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="setting-advance.php?page=<?= $page ?>&type=edit" novalidate="novalidate">
						<div class="form-group">
							<div class="col-sm-2 control-label"><?= $lang['from'] ?></div>
							<div class="col-sm-7">
                            	<select name="advance_from" class="form-control">
                            		<?php
                            		for ( $a = 1 ; $a <= 31 ; $a++ ){
                            			echo '<option value="'.$a.'" '.($row_page['post_title'] == $a ? 'selected' : '').'>'.$a.'</option>' ;
                            		}
                            		?>
                            	</select>
							</div>
						</div>   
						<div class="form-group">
							<div class="col-sm-2 control-label"><?= $lang['to'] ?></div>
							<div class="col-sm-7">
                            	<select name="advance_to" class="form-control">
                            		<?php
                            		for ( $a = 1 ; $a <= 31 ; $a++ ){
                            			echo '<option value="'.$a.'" '.($row_page['post_link'] == $a ? 'selected' : '').'>'.$a.'</option>' ;
                            		}
                            		?>
                            	</select>
							</div>
						</div>  
						<div class="form-group">
							<div class="col-sm-2 control-label"><?= $lang['remark'] ?></div>
							<div class="col-sm-7">
								<textarea name="advance_remark" class="form-control"><?= $row_page['post_content'] ?></textarea>
							</div>
						</div>   
						<div class="form-group">
							<div class="col-sm-2 control-label"></div>
							<div class="col-sm-7">
								<button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
								<input type="hidden" name="hide" value="1">
							</div>
						</div>   
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
</div>

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