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

// include the class
include 'requires/class_resize.php' ;
// keep parameter in value
$page = escapeString($_GET['page']) ;
$page_mode = escapeString($_GET['page_mode']) ;
$type = escapeString($_GET['type']) ;
$search = escapeString($_GET['search']) ;
$search_category = escapeString($_GET['search_category']) ;
// mode type | all list | new | edit
switch($page_mode){
	// new position
	case 'new' :
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'hr-position-new' ;
		// if new insert into database
		if (isset($type) && $type == 'new'){
			$mysqli->query("INSERT INTO system_post (post_type, post_categories, post_date, post_modified) VALUES ('hr-position', 'hr-position', '".TODAYDATE."', '".TODAYDATE."')");
			$last_id = $mysqli->insert_id;
		}
		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>
		<div class="warper container-fluid">   	
			<div class="page-header"><h1><?= $lang['Designation']?> <small><?= $lang['new']?></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" enctype="multipart/form-data" action="hr-position.php?page_mode=edit&page=<?= $last_id ?>&type=edit" novalidate="novalidate">
								<div class="form-group">
									<label class="col-sm-2 control-label"><?= $lang['Designation']?></label>
									<div class="col-sm-7">
										<input type="text" name="title" class="form-control ui-search-input" placeholder="<?= $lang['Designation']?>" >
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label"></label>
									<div class="col-sm-7">
										<button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit']?></button>
										<input type="hidden" name="hide_status" />
										<input type="hidden" name="hide" value="1">
									</div>
								</div>   
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	break ;
	// edit category
	case 'edit' :
		// active page
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'hr-position' ;
		// check query exsits
		$mysqli_page = $mysqli->query("SELECT * FROM system_post
										WHERE post_id = '".$page."' AND post_type = 'hr-position' AND post_categories = 'hr-position' LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			// update database
			if (isset($type) && $type == 'edit' && $_POST['hide'] == 1){
				// keep value in variable
				$page_title = escapeString($_POST['title']) ;
				$page_title = ($page_title != '' ? $page_title : 'No Title') ;
				$apply_to_form = escapeString($_POST['apply_to_form']) ;
				// check if name already exists.
				$check_status = checkTitle($page_title, $page) ;
				$title = $check_status['title'] ;
				$status = $check_status['status'] ;
				// update database
				$mysqli->query("UPDATE system_post SET
								post_title = '".$page_title."',
								post_status = '".$status."',
								post_link = '".$title."',
								".$image_query."
								post_modified = '".TODAYDATE."',
								post_trash = '0'
								WHERE post_id = '".$page."'") ;
				// refresh page
				header("Location:hr-position.php?page_mode=edit&page=".$page."&success=1") ;
				exit ;
			}
			// start header here
			include 'requires/page_header.php';
			include 'requires/page_top.php';
			// keep query value in array
			$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			?>
			<div class="warper container-fluid">   	
				<div class="page-header"><h1><?= $lang['Designation']?> <small><?= $lang['edit']?></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" enctype="multipart/form-data" action="hr-position.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
									<div class="form-group">
										<label class="col-sm-2 control-label"><?= $lang['Designation']?></label>
										<div class="col-sm-7">
											<input type="text" name="title" class="form-control ui-search-input" value="<?= dataFilter($row_page['post_title']) ?>" placeholder="<?= $lang['Designation']?>" >
										</div>
									</div>
									<div class="form-group">
										<label class="col-sm-2 control-label"></label>
										<div class="col-sm-7">
											<button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit']?></button>
											<input type="hidden" name="hide_status" value="<?= $row_page['post_status'] ?>" />
											<input type="hidden" name="hide" value="1">
										</div>
									</div>   
								</form>
							</div>
						</div>
					</div>
				</div>
			</div>
			<?php
		}else{
			header("Location: hr-position.php?page_mode=all") ;
			exit ;	
		}
	break ;
	// all category list
	case 'all' :
	default :
		// query type
		$search_query = '' ;
		// search query
		if ($search != ''){
			$search_query .= " AND (post_title LIKE '%".$search."%')" ;
		}
		if ($search_category != ''){
			$search_query .= " AND post_parent = '".$search_category."'" ;
		}
		// active page
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'hr-position' ;
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// sortable first
			$sortable = $_POST['sortable'] ;
            if (is_array($sortable) && count($sortable) > 0){
                foreach($sortable as $key => $value){
                    $mysqli->query("UPDATE system_post SET
                                    post_order = '".$value."'
                                    WHERE post_id = '".$key."'") ;
                }
            }
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE " . system_post . " SET
									post_trash = '1'
									WHERE post_id = " ;
					$trash_page = trashPage('post', $mysqli, $mysqli_query, $_POST['multiple_trash']) ;
				break;
			}
		}
		// pagination
		if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
		$start_from = ($product_page - 1) * LIMIT ; //end next and prev page
		// set search url
		$search_url = 'search='.$search ;
		// page query
		$mysqli_query = "SELECT * FROM system_post
						WHERE post_title != '' AND post_type = 'hr-position' AND post_categories = 'hr-position' AND post_trash = '0'".$search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY post_order LIMIT $start_from, " . LIMIT) ;
		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query);
		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>
		<!-- Header Ends -->
		<div class="warper container-fluid">   	
			<div class="page-header"><h1><?= $lang['Designation']?> <small><?= $lang['list']?></small></h1></div>
            <form method="post">
                <!-- trash -->
                <div class="panel panel-default">
                    <div class="panel-body">
                        <select name="page_action" class="form-control selectpicker">
                            <option value=""><?= $lang['select']?></option>
                            <option value="trash"><?= $lang['move_to_trash']?></option>
                        </select>
                        <input type="hidden" name="hide" value="1" />
                        <input type="hidden" name="hide_status" value="action" />
                        <input type="submit" class="btn btn-default" value="<?= $lang['submit']?>" />
                    </div>
                </div>
                <!-- end trash -->
                <div class="panel panel-default">
                    <div class="panel-heading"></div>
                    <div class="panel-body">
						<table cellpadding="0" cellspacing="0" border="0" class="responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th><?= $lang['title']?></th>
									<th><?= $lang['date']?></th>
                                    <th width="100"><?= $lang['sortable']?></th>
                                    <th width="50"><?= $lang['trash']?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($mysqli_page->num_rows > 0){
									while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
										// title
										$title = dataFilter($row_page['post_title']) ;
										echo '
										 <tr class="odd gradeX">
											<td><a href="hr-position.php?page_mode=edit&page='.$row_page['post_id'].'">'.$title.'</a></td>
											<td>'.date('d M Y', strtotime($row_page['post_date'])).'</td>
											<td><input type="text" name="sortable['.$row_page['post_id'].']" value="'.$row_page['post_order'].'" class="responsive_width text_center" /></td>
											<td style="vertical-align:middle">
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$row_page['post_id'].']" class="trash_button" value="1">
													<label for="checkbox1"></label>
												</div>
											</td>
										</tr>';
									}
								}else{
									echo '
									<tr class="odd gradeX">
										<td class="border_none">'.$lang['no_data'].'</td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>