<?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']) ;

// active menu bar
$active_main_menu   = 'hr' ;
$active_sub_menu    = 'hr-attendance' ;
$active_menu 		= 'hr-health' ;

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

// mode type | all list | new | edit
switch($page_mode){
	
	// edit health
	case 'new' :
	case 'edit' :

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM staff_health
									   WHERE health_id = '".$page."' LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			// keep query value in array
			$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			$submit_type = 'edit' ;
		}

		// update database
		if ( isset($type) && ( $type == 'new' || $type == 'edit' ) && $_POST['hide'] == 1 ){

			$boolean_new = false ;
			if ( $page == '' ){
				$mysqli->query("INSERT INTO staff_health ( created_at ) VALUES ( '".TODAYDATE."' )") ;
				$page = $mysqli->insert_id ;	
				$boolean_new = true ;
			}
			
			// update database
			$mysqli->query("UPDATE staff_health SET
							staff_id 			= '".escapeString($_POST['staff_id'])."',
							temperature  	    = '".escapeString($_POST['temperature'])."',
							health_reason  	    = '".escapeString($_POST['health_reason'])."'
							WHERE health_id 	= '".$page."'") ;

			// refresh page
			if ( $boolean_new ){
				header("Location:hr-health.php?page_mode=new&page=&success=1") ;
			}else{
				header("Location:hr-health.php?page_mode=edit&page=".$page."&success=1") ;
			}
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'attendance-health-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'attendance-health-update') ) ){
			header('Location: hr-health.php') ;
			exit ;
		}

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

		// get all requires 
		// get all staff
		$staff_list   = [] ;
		$mysqli_staff = $mysqli->query("SELECT staff_id, staff_name, staff_idno FROM staff
										WHERE deleted_at IS NULL ".$user_branch_permission_sql) ;
		if ( $mysqli_staff->num_rows > 0 ){
			while ( $row_staff = $mysqli_staff->fetch_assoc() ){
				$staff_list[$row_staff['staff_id']] = dataFilter($row_staff['staff_name']) . ' ( ' . dataFilter($row_staff['staff_idno']) . ' )' ;
			}
		}

		?>

		<div class="warper container-fluid">   	
			<div class="page-header"><h1><?= $lang['health']?> <small><?= $lang[$page_mode] ?></small></h1></div>
			<?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'success-updated' :
                        echo '
                        <div class="result_success">
                            '.$lang['Thank you details has been updated'].'
                        </div>' ;	
                    break ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>
            <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-health.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">

						<div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Staff']?></div>
                            <div class="col-sm-9">
                            	<select name="staff_id" id="selectuser" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" required>
                            		<option value=""><?= $lang['select']?></option>
									<?php
									if ( count($staff_list) > 0 ){
										foreach ( $staff_list as $k => $v ){
											echo '<option value="'.$k.'" '.( $k == $row_page['staff_id'] ? 'selected' : '').'>'.$v.'</option>' ;
										}
									}
									?>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Temperature']?></div>
                            <div class="col-sm-9">
                                <input type="text" name="temperature" class="form-control ui-search-input" value="<?= dataFilter($row_page['temperature']) ?>" placeholder="<?= $lang['Temperature']?>" >
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Reason']?></div>
                            <div class="col-sm-9">
                                <input type="text" name="health_reason" class="form-control ui-search-input" value="<?= dataFilter($row_page['health_reason']) ?>" placeholder="<?= $lang['Reason']?>" >
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-2 control-label"></div>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-purple" style="float:right"><?= $lang['submit']?></button>
                                <input type="hidden" name="hide" value="1">
                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
                            </div>
                        </div>   
                    </form>
                </div>
            </div>
		</div>
		<?php
		
	break ;
	
	// all hostel list
	case 'all' :
	default :
		
		// query type
		$search_query = '' ;
		
		// search query
		if ( $search != '' ){
			$search_query .= " AND ( a.degree LIKE '%".$search."%' OR b.staff_idno LIKE '%".$search."%' )" ;
		}
		
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE staff_health SET
									 deleted_at = '".TODAYDATE."'
									 WHERE health_id = " ;
					$trash_page = trashPage('health', $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 a.health_id, a.temperature, a.health_reason, a.created_at, b.staff_id, b.staff_idno, b.staff_name FROM staff_health a
						 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						 WHERE a.deleted_at IS NULL " . $search_query.$user_branch_permission_sql_b ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY a.health_id DESC 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['health']?> <small><?= $lang['list']?></small></h1>
				<?php if ( permissionCheck($row_user, 'attendance-health-new') ){ ?>
					<a href="hr-health.php?page_mode=new" class="btn btn-purple" target="_blank"><?= $lang['add_new']?></a>
				<?php } ?>
			</div>

            <form method="post">
                
                <?php if ( permissionCheck($row_user, 'attendance-health-update') ){ ?>
                <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-purple" value="<?= $lang['submit']?>" />
                    </div>
                </div>
                <?php } ?>

                <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['Action']?></th>
									<th><?= $lang['Staff']?></th>
									<th><?= $lang['Temperature']?></th>
									<th><?= $lang['Reason']?></th>
									<th><?= $lang['Created At']?></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)){

										// default variable
										$id = $row_page['health_id'] ;
		
										echo '
										 <tr class="odd gradeX">
										 	<td class="align_center">' ;
												if ( permissionCheck($row_user, 'attendance-health-update') ){
													echo '
										 			<a title="'.$lang['Edit Current'].$lang['health'].'" href="hr-health.php?page_mode=edit&page='.$id.'"><i class="fa fa-edit"></i></a>' ;
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td>'.dataFilter($row_page['staff_name']).' ( '.dataFilter($row_page['staff_idno']).' )</td>
											<td>'.dataFilter($row_page['temperature']).'</td>
											<td class="align_center">'.resetDateFormat($row_page['health_reason']).'</td>
											<td class="align_center">'.resetDateFormat($row_page['created_at']).'</td>
											<td>
												<div class="checkbox multiple_trash">
													<input type="checkbox" name="multiple_trash['.$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>
										<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' ;
?>