<?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 = 'hr' ;
		$active_menu = 'hr-warning-letter' ;
		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>
		<div class="warper container-fluid">   	
			<div class="page-header"><h1>Warning Letter <small>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-warning-letter.php?page_mode=edit&page=&type=edit" novalidate="novalidate">
								<div class="form-group">
									<label class="col-sm-2 control-label">Employment</label>
									<div class="col-sm-7">
                                    	<?php
										$array_employment = listAllEmployment('', '', 'employment', '', 'required') ;
										if ($array_employment['result']){
											echo $array_employment['select'] ;	
										}
										?>
									</div>
								</div>
                                <div class="form-group">
									<label class="col-sm-2 control-label">Reason</label>
									<div class="col-sm-7">
										<input type="text" name="reason" class="form-control ui-search-input" required placeholder="Reason" >
									</div>
								</div>
                                <div class="form-group">
									<label class="col-sm-2 control-label">Remark</label>
									<div class="col-sm-7">
                                    	<textarea name="remark" class="form-control ui-search-input" placeholder="Remark"></textarea>
									</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">Submit</button>
										<input type="hidden" name="hide_status" />
										<input type="hidden" name="hide" value="1">
                                        <input type="hidden" name="page_status" value="new">
									</div>
								</div>   
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	break ;
	// edit category
	case 'edit' :
		// active page
		$active_main_menu = 'hr' ;
		$active_menu = 'hr-warning-letter' ;
		// if new insert into database
		$page_status = $_POST['page_status'] ;
		if ($page_status == 'new'){
			$mysqli->query("INSERT INTO system_letter (letter_type, letter_date, letter_modified, letter_trash) VALUES ('warning', '".TODAYDATE."', '".TODAYDATE."', '0')");
			$page = $mysqli->insert_id;
		}
		// check query exsits
		$mysqli_page = $mysqli->query("SELECT * FROM system_letter
									   WHERE letter_id = '".$page."' AND letter_type = 'warning' AND letter_trash = '0' LIMIT 1") ;
		if ($mysqli_page->num_rows > 0){
			// update database
			if (isset($type) && $type == 'edit' && $_POST['hide'] == 1){
				// keep value in variable
				$letter_employment_id = escapeString($_POST['employment']) ;
				$letter_title = escapeString($_POST['reason']) ;
				$letter_remark = escapeString($_POST['remark']) ;
				// update database
				$mysqli->query("UPDATE system_letter SET
								letter_employment_id = '".$letter_employment_id."',
								letter_title = '".$letter_title."',
								letter_remark = '".$letter_remark."',
								letter_modified = '".TODAYDATE."'
								WHERE letter_id = '".$page."'") ;
				// refresh page
				header("Location:hr-warning-letter.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>Warning Letter <small>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-warning-letter.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
									<div class="form-group">
                                        <label class="col-sm-2 control-label">Employment</label>
                                        <div class="col-sm-7">
                                            <?php
                                            $array_employment = listAllEmployment('', '', 'employment', $row_page['letter_employment_id'], 'required') ;
                                            if ($array_employment['result']){
                                                echo $array_employment['select'] ;	
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">Reason</label>
                                        <div class="col-sm-7">
                                            <input type="text" name="reason" class="form-control ui-search-input" value="<?= dataFilter($row_page['letter_title']) ?>" required placeholder="Reason" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">Remark</label>
                                        <div class="col-sm-7">
                                            <textarea name="remark" class="form-control ui-search-input" placeholder="Remark"><?= dataFilter($row_page['letter_remark']) ?></textarea>
                                        </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">Submit</button>
											<input type="hidden" name="hide_status" value="<?= $row_page['post_status'] ?>" />
											<input type="hidden" name="hide" value="1">
                                            <input type="hidden" name="page_status" value="edit">
										</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 (a.letter_title LIKE '%".$search."%')" ;
		}
		// active page
		$active_main_menu = 'hr' ;
		$active_menu = 'hr-warning-letter' ;
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE " . system_letter . " SET
									 letter_trash = '1'
									 WHERE letter_id = " ;
					$trash_page = trashPage('letter', $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_letter a
						 LEFT JOIN staff_employment b ON (a.letter_employment_id = b.employment_id)
						 WHERE a.letter_type = 'warning' AND a.letter_trash = '0'".$search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY a.letter_id 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>Warning Letter <small>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="">Select</option>
                            <option value="trash">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="Submit" />
                    </div>
                </div>
                <!-- end trash -->
                <div class="panel panel-default">
                    <div class="panel-heading"></div>
                    <div class="panel-body">
                    	<a href="hr-warning-letter.php?page_mode=new&type=new" class="btn btn-purple margin_bottom_10">Create Warning Letter</a>
						<table cellpadding="0" cellspacing="0" border="0" class="responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th>Title</th>
                                    <th>Employment</th>
									<th>Date</th>
                                    <th width="50">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['letter_title']) ;
										echo '
										 <tr class="odd gradeX">
											<td><a href="hr-warning-letter.php?page_mode=edit&page='.$row_page['letter_id'].'">'.$title.'</a></td>
											<td>'.dataFilter($row_page['employment_name']).'</td>
											<td>'.date('d M Y', strtotime($row_page['letter_date'])).'</td>
											<td style="vertical-align:middle">
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$row_page['letter_id'].']" class="trash_button" value="1">
													<label for="checkbox1"></label>
												</div>
											</td>
										</tr>';
									}
								}else{
									echo '
									<tr class="odd gradeX">
										<td class="border_none">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' ;
?>