<?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 = 'setting' ;
$active_sub_menu  = 'setting-hr' ;
$active_menu      = 'setting-hr-working' ;

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

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM setting_working_group
									   WHERE group_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 ){

			// keep value in variable
			$page_title      = escapeString($_POST['title']) ;
			$page_title      = ($page_title != '' ? $page_title : 'No Title') ;

			$boolean_add_working = false ;
			if ( $page == '' ){
				$mysqli->query("INSERT INTO setting_working_group (created_at) VALUES ('".TODAYDATE."')") ;
				$page = $mysqli->insert_id ;	
				$boolean_add_working = true ;
			}
			
			// update database
			$mysqli->query("UPDATE setting_working_group SET
							group_name = '".$page_title."',
							updated_at = '".TODAYDATE."'
							WHERE group_id = '".$page."'") ;

			$working_hours = $_POST['working_hours'] ;
            foreach ( $working_hours as $key => $value ){

                $working_day                = $value ;
                $working_on                 = ( !empty($_POST['working_on'][$value]) ? 'yes' : 'no' ) ;
                $working_if_flexi			= ( !empty($_POST['working_if_flexi'][$value]) ? 'yes' : 'no' ) ;
                $working_if_include_rest	= ( !empty($_POST['working_if_include_rest'][$value]) ? 'yes' : 'no' ) ;
                $working_if_ot				= ( !empty($_POST['working_if_ot'][$value]) ? 'yes' : 'no' ) ;
                $working_if_fixed_work		= ( !empty($_POST['working_if_fixed_work'][$value]) ? 'yes' : 'no' ) ;
                $working_direct_day			= ( !empty($_POST['working_direct_day'][$value]) ? 'yes' : 'no' ) ;
                $working_if_ot_morning		= ( !empty($_POST['working_if_ot_morning'][$value]) ? 'yes' : 'no' ) ;
                $working_if_offduty			= ( !empty($_POST['working_if_offduty'][$value]) ? 'yes' : 'no' ) ;
                $working_if_deduct_offduty  = ( !empty($_POST['working_if_deduct_offduty'][$value]) ? 'yes' : 'no' ) ;
                $working_day_calculation    = $_POST['working_day_calculation'][$value] ;
                $working_morning_start      = $_POST['working_morning_start'][$value] ;
                $working_morning_end        = $_POST['working_morning_end'][$value] ;
                $working_period_before      = $_POST['working_period_before'][$value] ;
                $working_break_start        = $_POST['working_break_start'][$value] ;
                $working_break_end          = $_POST['working_break_end'][$value] ;
                $working_break_end_include_ot = $_POST['working_break_end_include_ot'][$value] ;
                $working_afternoon_start    = $_POST['working_afternoon_start'][$value] ;
                $working_afternoon_end      = $_POST['working_afternoon_end'][$value] ;
                $working_shortbreak_start   = $_POST['working_shortbreak_start'][$value] ;
                $working_shortbreak_end     = $_POST['working_shortbreak_end'][$value] ;
                $working_night_start        = $_POST['working_night_start'][$value] ;
                $working_night_end          = $_POST['working_night_end'][$value] ;
                $working_ot_start           = $_POST['working_ot_start'][$value] ;
                $working_total_hours        = $_POST['working_total_hours'][$value] ;
                $working_total_hours        = ( $working_total_hours <= '24:00' ? $working_total_hours : '24:00' ) ;
                $working_total_rest_hours   = $_POST['working_total_rest_hours'][$value] ;
                $working_total_rest_hours   = ( $working_total_rest_hours <= '24:00' ? $working_total_rest_hours : '24:00' ) ;
                $working_rest_range_from    = $_POST['working_rest_range_from'][$value] ;
                $working_rest_range_from 	= ( $working_rest_range_from != '' ? $working_rest_range_from : NULL ) ;
                $working_rest_range_to      = $_POST['working_rest_range_to'][$value] ;
                $working_rest_range_to 		= ( $working_rest_range_from != '' && $working_rest_range_to != '' ? $working_rest_range_to : $working_rest_range_from ) ;
                $working_rest_include_ot    = $_POST['working_rest_include_ot'][$value] ;
                $working_rest_include_ot 	= ( $working_rest_include_ot != '' ? $working_rest_include_ot : NULL ) ;
                //second
                $working_total_rest_hours2  = $_POST['working_total_rest_hours2'][$value] ;
                $working_total_rest_hours2  = ( $working_total_rest_hours2 <= '24:00' ? $working_total_rest_hours2 : '24:00' ) ;
                $working_rest_range_from2   = $_POST['working_rest_range_from2'][$value] ;
                $working_rest_range_from2	= ( $working_rest_range_from2 != '' ? $working_rest_range_from2 : NULL ) ;
                $working_rest_range_to2     = $_POST['working_rest_range_to2'][$value] ;
                $working_rest_range_to2		= ( $working_rest_range_from2 != '' && $working_rest_range_to2 != '' ? $working_rest_range_to2 : $working_rest_range_from2 ) ;

                $working_rounding_ot  		= $_POST['working_rounding_ot'][$value] ;

                $working_count_offduty   	= $_POST['working_count_offduty'][$value] ;
                $working_count_offduty   	= ( $working_count_offduty <= '24:00' ? $working_count_offduty : '24:00' ) ;

                $working_max_ot    			= $_POST['working_max_ot'][$value] ;
                $working_max_ot 			= ( $working_max_ot != '' ? $working_max_ot : NULL ) ;

                // check if exsits
                $check_working = $mysqli->query("SELECT * FROM setting_working
                								 WHERE group_id = '".$page."' AND working_day = '".$value."' LIMIT 1") ;
                if ( $check_working->num_rows == 0 ){
                	$mysqli->query("INSERT INTO setting_working
                					(created_at) VALUES 
                					('".TODAYDATE."')") ;
                	$working_id  = $mysqli->insert_id ;
                }else{
                	$row_working = $check_working->fetch_assoc() ;
                	$working_id  = $row_working['working_id'] ;
                }

                // if morning end > morning start
                $working_next_day = 'no' ;
                if ( $working_break_start > $working_break_end ){
                	$working_next_day = 'yes' ;
                }

                $working_morning_end = date('H:i:s', strtotime($working_morning_start.' -1 seconds')) ;

                $mysqli->query("UPDATE setting_working SET
                				group_id 					= '".$page."',
                				working_day 				= '".$working_day."',
                				working_on 					= '".$working_on."',
                				working_next_day 			= '".$working_next_day."',
                				working_if_include_rest  	= '".$working_if_include_rest."',
                				working_if_ot 				= '".$working_if_ot."',
                				working_direct_day 			= '".$working_direct_day."',
                				working_if_ot_morning  		= '".$working_if_ot_morning."',	
                				working_if_flexi 			= '".$working_if_flexi."',
                				working_if_fixed_work		= '".$working_if_fixed_work."',
                				working_day_calculation     = '".$working_day_calculation."',
                				working_morning_start 		= '".$working_morning_start."',
                				working_morning_end 		= '".$working_morning_end."',
                				working_period_before 		= '".$working_period_before."',
                				working_break_start 		= '".$working_break_start."',
                				working_break_end 			= '".$working_break_end."',
                				working_break_end_include_ot = '".$working_break_end_include_ot."',
                				working_afternoon_start 	= '".$working_afternoon_start."',
                				working_afternoon_end 		= '".$working_afternoon_end."',
                				working_shortbreak_start 	= '".$working_shortbreak_start."',
                				working_shortbreak_end 		= '".$working_shortbreak_end."',
                				working_night_start 		= '".$working_night_start."',
                				working_night_end 			= '".$working_night_end."',
                				working_ot_start 			= '".$working_ot_start."',
                				working_total_hours 		= '".$working_total_hours."',
                				working_total_rest_hours 	= '".$working_total_rest_hours."',
                				working_total_rest_hours 	= '".$working_total_rest_hours."',
                				working_rest_range_from 	= ".( $working_rest_range_from != '' ? "'".$working_rest_range_from."'" : "NULL").",
                				working_rest_range_to 		= ".( $working_rest_range_to != '' ? "'".$working_rest_range_to."'" : "NULL").",
                				working_rest_include_ot     = '".$working_rest_include_ot."',
                				working_total_rest_hours2	= '".$working_total_rest_hours2."',
                				working_rest_range_from2 	= ".( $working_rest_range_from2 != '' ? "'".$working_rest_range_from2."'" : "NULL").",
                				working_rest_range_to2 		= ".( $working_rest_range_to2 != '' ? "'".$working_rest_range_to2."'" : "NULL").",
                				working_rounding_ot			= '".$working_rounding_ot."',
                				working_if_offduty 			= '".$working_if_offduty."',
                				working_count_offduty 		= '".$working_count_offduty."',
                				working_if_deduct_offduty 	= '".$working_if_deduct_offduty."',
                				working_max_ot 			    = ".( $working_max_ot != '' ? "'".$working_max_ot."'" : "NULL").",
                				updated_at 					= '".TODAYDATE."'
                				WHERE working_id 			= '".$working_id."'") ;

            }
		
			// add system log
			$array_remark = array('old' => array('title' => $row_page['working_name']),
					  			  'new' => array('title' => $page_title)) ;
			// refresh page
			header("Location:setting-working.php?page_mode=edit&page=".$page."&success=1") ;
			$_SESSION['system_result'] = 'success-updated' ;
			exit ;
		}

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

		?>

		<script>
		$(document).ready(function(){

			$('.copy_paste').click(function(){

				var set_working_day_calculation       = $('.set_working_day_calculation').first().val() ;
				var set_working_morning_start         = $('.set_working_morning_start').first().val() ;
				var set_working_period_before         = $('.set_working_period_before').first().val() ;
				var set_working_break_start           = $('.set_working_break_start').first().val() ;
				var set_working_break_end             = $('.set_working_break_end').first().val() ;
				var set_working_break_end_include_ot  = $('.set_working_break_end_include_ot').first().val() ;
				var set_working_total_hours           = $('.set_working_total_hours').first().val() ;
				var set_working_total_rest_hours      = $('.set_working_total_rest_hours').first().val() ;
				var set_working_rest_range_from       = $('.set_working_rest_range_from').first().val() ;
				var set_working_rest_range_to         = $('.set_working_rest_range_to').first().val() ;
				var set_working_rest_include_ot       = $('.set_working_rest_include_ot').first().val() ;
				var set_working_total_rest_hours2     = $('.set_working_total_rest_hours2').first().val() ;
				var set_working_rest_range_from2      = $('.set_working_rest_range_from2').first().val() ;
				var set_working_rest_range_to2        = $('.set_working_rest_range_to2').first().val() ;
				var set_working_rounding_ot			  = $('.set_working_rounding_ot').first().val() ;
				var set_working_count_offduty   	  = $('.set_working_count_offduty').first().val() ;
				var set_working_max_ot	  	  		  = $('.set_working_max_ot').first().val() ;

				$('.set_working_day_calculation').val( set_working_day_calculation ) ;
				$('.set_working_morning_start').val( set_working_morning_start ) ;
				$('.set_working_period_before').val( set_working_period_before ) ;
				$('.set_working_break_start').val( set_working_break_start ) ;
				$('.set_working_break_end').val( set_working_break_end ) ;
				$('.set_working_break_end_include_ot').val( set_working_break_end_include_ot ) ;
				$('.set_working_total_hours').val( set_working_total_hours ) ;
				$('.set_working_total_rest_hours').val( set_working_total_rest_hours ) ;
				$('.set_working_rest_range_from').val( set_working_rest_range_from ) ;
				$('.set_working_rest_range_to').val( set_working_rest_range_to ) ;
				$('.set_working_rest_include_ot').val( set_working_rest_include_ot ) ;
				$('.set_working_total_rest_hours2').val( set_working_total_rest_hours2 ) ;
				$('.set_working_rest_range_from2').val( set_working_rest_range_from2 ) ;
				$('.set_working_rest_range_to2').val( set_working_rest_range_to2 ) ;
				$('.set_working_rounding_ot').val( set_working_rounding_ot ) ;
				$('.set_working_count_offduty').val( set_working_count_offduty ) ;
				$('.set_working_max_ot').val( set_working_max_ot ) ;

				if ( $('.set_working_on').first().attr('checked') ){
					$('.set_working_on').prop("checked", true) ;
				}else{
					$('.set_working_on').prop("checked", false) ;
				}

				if ( $('.set_working_if_flexi').first().attr('checked') ){
					$('.set_working_if_flexi').prop("checked", true) ;
					$('.div_working_hours, .div_reset_hours_range, .div_working_direct_day').hide() ;
				}else{
					$('.set_working_if_flexi').prop("checked", false) ;
					$('.div_working_hours, .div_reset_hours_range, .div_working_direct_day').show() ;
				}

				if ( $('.set_working_if_fixed_work').first().attr('checked') ){
					$('.set_working_if_fixed_work').prop("checked", true) ;
				}else{
					$('.set_working_if_fixed_work').prop("checked", false) ;
				}

				if ( $('.set_working_if_include_rest').first().attr('checked') ){
					$('.set_working_if_include_rest').prop("checked", true) ;
				}else{
					$('.set_working_if_include_rest').prop("checked", false) ;
				}

				if ( $('.set_working_if_ot').first().attr('checked') ){
					$('.set_working_if_ot').prop("checked", true) ;
					$('.div_ot_box').show() ;
				}else{
					$('.set_working_if_ot').prop("checked", false) ;
					$('.div_ot_box').hide() ;
				}

				if ( $('.set_working_if_ot_morning').first().attr('checked') ){
					$('.set_working_if_ot_morning').prop("checked", true) ;
				}else{
					$('.set_working_if_ot_morning').prop("checked", false) ;
				}

				if ( $('.set_working_count_format').first().attr('checked') ){
					$('.set_working_count_format').prop("checked", true) ;
				}else{
					$('.set_working_count_format').prop("checked", false) ;
				}

				if ( $('.set_working_if_offduty').first().attr('checked') ){
					$('.set_working_if_offduty').prop("checked", true) ;
				}else{
					$('.set_working_if_offduty').prop("checked", false) ;
				}

				if ( $('.set_working_if_deduct_offduty').first().attr('checked') ){
					$('.set_working_if_deduct_offduty').prop("checked", true) ;
				}else{
					$('.set_working_if_deduct_offduty').prop("checked", false) ;
				}

				return false ;
			}) ;

			$('.set_working_if_flexi').change(function(){
				var thisKey    = $(this),
					thisParent = thisKey.parents('.div_setting_working_box') ;
				if ( thisKey.is(':checked') ){
					thisParent.find('.div_working_hours').hide() ;
					thisParent.find('.div_reset_hours_range').hide() ;
					thisParent.find('.div_working_direct_day').hide() ;
				}else{
					thisParent.find('.div_working_hours').show() ;
					thisParent.find('.div_reset_hours_range').show() ;
					thisParent.find('.div_working_direct_day').show() ;
				}
			}) ;

			$('.set_working_rounding_ot').keyup(function(){
				var thisKey = $(this),
					thisVal = thisKey.val() ;
				if ( thisVal != '' ){
					thisVal = parseInt(thisVal) ;
					if ( thisVal > 60 ){
						thisKey.val( '60' ) ;
					}else{
						thisKey.val( thisVal ) ;
					}
				}	
			}) ;

			$('.set_working_if_ot').change( function(){
				var thisKey = $(this) ;
				if ( thisKey.attr('checked') ){
					thisKey.parents('.div_setting_working_box').find('.div_ot_box').show() ;
				}else{
					thisKey.parents('.div_setting_working_box').find('.div_ot_box').hide() ;
				}
			}) ;

		}) ;
		</script>

		<style>
		#map-container, #canvas, #existMap {
		    height: 400px;
		}
		</style>

		<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;">
				<div class="row">
                	<div class="pull-left col">
                		<h1><?= $lang['working_hours'] ?> <small><?= $lang[$page_mode] ?></small></h1>
                	</div>
                </div>
			<?php
            if ($_SESSION['system_result'] != ''){
                switch($_SESSION['system_result']){
                    case 'success-updated' :
                        echo '
                        <div class="result_success">
                        	'.$lang['thank_you_your_working_has_been_updated'].' 
                        </div>' ;	
                    break ;
                }
                unset($_SESSION['system_result']) ;
            }
            ?>
        	</div>
            <div class="panel panel-default">
                <div class="panel-heading" style="text-align:right;">
                	<a href="#" class="copy_paste" style="font-size:12px;"><?= $lang['copy_paste'] ?></a>
                </div>
                <div class="panel-body">
                    <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="setting-working.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['working_title'] ?></div>
                            <div class="col-sm-9">
                                <input type="text" name="title" class="form-control ui-search-input" value="<?= dataFilter($row_page['group_name']) ?>" placeholder="<?= $lang['working_title'] ?>" >
                            </div>
                        </div>

                        <?php

						$day_list = [ '1' => $lang['monday'],
									  '2' => $lang['tuesday'],
									  '3' => $lang['wednesday'],
									  '4' => $lang['thursday'],
									  '5' => $lang['friday'],
									  '6' => $lang['saturday'],
									  '7' => $lang['sunday'] ] ;

						if ( $page != '' ){
					        $set_workings = [] ;
					        $get_query = $mysqli->query("SELECT * FROM setting_working
					        							 WHERE group_id = '".$page."'") ;
							if ( $get_query->num_rows > 0 ){
								while ( $value = $get_query->fetch_assoc() ){
									$set_workings[$value['working_day']] = [
					                    'working_on'                => $value['working_on'],
					                    'working_if_flexi' 			=> $value['working_if_flexi'],
					                    'working_next_day'			=> $value['working_next_day'],
					                    'working_if_include_rest'   => $value['working_if_include_rest'],
					                	'working_if_ot'				=> $value['working_if_ot'],
					                	'working_if_fixed_work' 	=> $value['working_if_fixed_work'],
					                	'working_day_calculation'   => $value['working_day_calculation'],
					                    'working_morning_start'     => $value['working_morning_start'],
					                    'working_morning_end'       => $value['working_morning_end'],
					                    'working_period_before'     => $value['working_period_before'],
					                    'working_break_start'       => $value['working_break_start'],
					                    'working_break_end'         => $value['working_break_end'],
					                    'working_break_end_include_ot' => $value['working_break_end_include_ot'],
					                    'working_afternoon_start'   => $value['working_afternoon_start'],
					                    'working_afternoon_end'     => $value['working_afternoon_end'],
					                    'working_shortbreak_start'  => $value['working_shortbreak_start'],
					                    'working_shortbreak_end'    => $value['working_shortbreak_end'],
					                    'working_night_start'       => $value['working_night_start'],
					                    'working_night_end'         => $value['working_night_end'],
					                    'working_ot_start'          => $value['working_ot_start'],
					                    'working_direct_day'        => $value['working_direct_day'],
					                    'working_if_ot_morning'     => $value['working_if_ot_morning'],
					                    'working_total_hours'       => $value['working_total_hours'],
					                    'working_total_rest_hours'  => $value['working_total_rest_hours'],
					                    'working_rest_range_from'   => $value['working_rest_range_from'],
					                    'working_rest_range_to'     => $value['working_rest_range_to'],
					                    'working_rest_include_ot'   => $value['working_rest_include_ot'],
					                    'working_total_rest_hours2' => $value['working_total_rest_hours2'],
					                    'working_rest_range_from2'  => $value['working_rest_range_from2'],
					                    'working_rest_range_to2'    => $value['working_rest_range_to2'],
					                    'working_rounding_ot' 		=> $value['working_rounding_ot'],
					                    'working_if_offduty'  		=> $value['working_if_offduty'],
					                    'working_count_offduty'  	=> $value['working_count_offduty'],
					                    'working_if_deduct_offduty' => $value['working_if_deduct_offduty'],
					                    'working_max_ot'       		=> $value['working_max_ot']
					                ] ;
								}
							}
						}else{
							$set_workings = [] ;
					        for ( $a = 1 ; $a <= 7 ; $a++ ){

					        	$set_workings[$a] = [
					                'working_on'                => 'yes',
					                'working_if_flexi' 			=> 'no',
					                'working_next_day'			=> 'no',
					                'working_if_include_rest'   => 'no',
					                'working_if_ot'				=> 'no',
					                'working_direct_day'	    => 'no',
					                'working_if_ot_morning'     => 'no',
					                'working_if_fixed_work' 	=> 'no',
					                'working_day_calculation'   => '00:01:00',
					                'working_morning_start'     => '08:00:00',
					                'working_morning_end'       => '08:00:00',
					                'working_period_before' 	=> '0',
					                'working_break_start'       => '09:00:00',
					                'working_break_end'         => '18:00:00',
					                'working_break_end_include_ot' => '00:00:00',
					                'working_total_hours'       => '08:00:00',
					                'working_total_rest_hours'  => '01:00:00',
					                'working_rest_range_from' 	=> '01:00:00',
					                'working_rest_range_to' 	=> '01:00:00',
					                'working_rest_include_ot'   => '00:00:00',
					                'working_total_rest_hours2' => '01:00:00',
					                'working_rest_range_from2' 	=> '01:00:00',
					                'working_rest_range_to2' 	=> '01:00:00',
				                    'working_rounding_ot'  		=> '1',
				                    'working_if_offduty'  		=> 'no',
				                    'working_count_offduty'  	=> '00:00:00',
				                    'working_if_deduct_offduty' => 'no',
				                    'working_max_ot'            => '00:00'
					            ] ;

					        }
						}

						foreach ( $set_workings as $key => $value ){

							?>

							<hr />
							<div class="div_setting_working_box">

								<div class="form-group">
									<label class="col-md-12 col-sm-1 col-xs-12 text-center" style="font-size: 16px; text-decoration: underline;">
										<input type="checkbox" value="yes" name="working_on[<?= $key ?>]" class="set_working_on" <?= $value['working_on'] == 'yes' ? 'checked' : '' ?> style="margin-right:10px" />
										<b><?= $day_list[$key] ?></b>
									</label>
									<input type="hidden" name="working_hours[]" value="<?= $key ?>" />
								</div>

								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['flexible_hours'] ?></label>
									<div class="col-md-10 col-sm-10 col-xs-4" >
										<label style="margin-top:3px;">
											<input type="checkbox" value="yes" name="working_if_flexi[<?= $key ?>]" class="set_working_if_flexi"  <?= $value['working_if_flexi'] == 'yes' ? 'checked' : '' ?> /> <?= $lang['check_if_flexible_hours'] ?>
										</label>
									</div>			
								</div>
								<?php if (EXCELDETAIL == "YES"){ ?>
								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['work_day_calculation'] ?></label>
									<div class="col-md-10 col-sm-10 col-xs-4" >
										<input class="form-control set_working_day_calculation" type="text" name="working_day_calculation[<?= $key ?>]" id="set_working_day_calculation" placeholder="00:00" value="<?= !empty($value['working_day_calculation']) ? substr($value['working_day_calculation'], 0, 5) : '' ?>" data-timepicker required />
									</div>			
								</div>
							<?php } ?>
								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['working_start'] ?></label>
									<div class="col-md-5 col-sm-5 col-xs-4" noted="几点开始打卡，属于今天的打卡时间" >
										<input class="form-control set_working_morning_start" type="text" name="working_morning_start[<?= $key ?>]" id="working_morning_start" placeholder="00:00" value="<?= !empty($value['working_morning_start']) ? substr($value['working_morning_start'], 0, 5) : '' ?>" data-timepicker required />
									</div>
									<div class="col-md-5 col-sm-5 col-xs-4" >
										<input class="form-control set_working_period_before" type="text" name="working_break_end_include_ot[<?= $key ?>]" id="working_break_end_include_ot" placeholder="0" value="<?= !empty($value['working_break_end_include_ot']) ? $value['working_break_end_include_ot'] : '0' ?>" />
										<label style="margin-top:3px;">* <?= $lang['set_the_X_hours_before_the_ending_working_hours'] ?></label>
									</div>
								</div>
								<div class="form-group div_working_hours" <?= ( $value['working_if_flexi'] == 'yes' ? 'style="display:none;"' : '' ) ?> >
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['working_hours'] ?></label>
									<div class="col-md-5 col-sm-5 col-xs-4" >
										<input class="form-control set_working_break_start" type="text" name="working_break_start[<?= $key ?>]" id="working_break_start" placeholder="00:00" value="<?= !empty($value['working_break_start']) ? substr($value['working_break_start'], 0, 5) : '' ?>" data-timepicker required />
									</div>
									<div class="col-md-5 col-sm-5 col-xs-4" >
										<input class="form-control set_working_break_end" type="text" name="working_break_end[<?= $key ?>]" id="working_break_end" placeholder="00:00" value="<?= !empty($value['working_break_end']) ? substr($value['working_break_end'], 0, 5) : '' ?>" data-timepicker required />
										<label style="margin-top:3px;">
											<input type="checkbox" value="yes" name="working_if_fixed_work[<?= $key ?>]" class="set_working_if_fixed_work" <?= $value['working_if_fixed_work'] == 'yes' ? 'checked' : '' ?> /> Tick if fixed working hours?
										</label>
									</div>
								</div>	
								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['total_working_hours'] ?></label>
									<div class="col-md-10 col-sm-10 col-xs-4" >
										<input class="form-control set_working_total_hours" type="text" name="working_total_hours[<?= $key ?>]" id="working_total_hours" placeholder="00:00" value="<?= !empty($value['working_total_hours']) ? substr($value['working_total_hours'], 0, 5) : '' ?>" data-timepicker required />
									</div>	
								</div>
								<div style="background-color: #f9f9f9a1">
									<div class="form-group">
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['total_rest_hours'] ?> 1</label>
										<div class="col-md-10 col-sm-10 col-xs-4" >
											<input class="form-control set_working_total_rest_hours" type="text" name="working_total_rest_hours[<?= $key ?>]" id="working_total_rest_hours" placeholder="00:00" value="<?= !empty($value['working_total_rest_hours']) ? substr($value['working_total_rest_hours'], 0, 5) : '' ?>" data-timepicker required />
										</div>			
									</div>
									<div class="form-group div_reset_hours_range" <?= ( $value['working_if_flexi'] == 'yes' ? 'style="display:none;"' : '' ) ?> >
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['rest_hours_range'] ?> 1</label>
										<div class="col-md-5 col-sm-5 col-xs-4" >
											<input class="form-control set_working_rest_range_from" type="text" name="working_rest_range_from[<?= $key ?>]" id="working_rest_range_from" placeholder="00:00" value="<?= !empty($value['working_rest_range_from']) ? substr($value['working_rest_range_from'], 0, 5) : '' ?>" data-timepicker />
										</div>
										<div class="col-md-5 col-sm-5 col-xs-4" >
											<input class="form-control set_working_rest_range_to" type="text" name="working_rest_range_to[<?= $key ?>]" id="working_rest_range_to" placeholder="00:00" value="<?= !empty($value['working_rest_range_to']) ? substr($value['working_rest_range_to'], 0, 5) : '' ?>" data-timepicker />
										</div>				
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right">Over X Hours Deduct Rest 1</label>
									<div class="col-md-10 col-sm-10 col-xs-4" >
										<input class="form-control set_working_rest_include_ot" type="text" name="working_rest_include_ot[<?= $key ?>]" id="working_rest_include_ot" placeholder="00:00" value="<?= !empty($value['working_rest_include_ot']) ? substr($value['working_rest_include_ot'], 0, 5) : '' ?>" data-timepicker required />
										<label style="margin-top:3px;">* Only affected for half day working</label>
									</div>	
								</div>
								
								<div style="background-color: #f9f9f9a1">
									<div class="form-group">
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['total_rest_hours'] ?> 2</label>
										<div class="col-md-10 col-sm-10 col-xs-4" >
											<input class="form-control set_working_total_rest_hours2" type="text" name="working_total_rest_hours2[<?= $key ?>]" id="working_total_rest_hours2" placeholder="00:00" value="<?= !empty($value['working_total_rest_hours2']) ? substr($value['working_total_rest_hours2'], 0, 5) : '' ?>" data-timepicker required />
										</div>			
									</div>
									<div class="form-group div_reset_hours_range" <?= ( $value['working_if_flexi'] == 'yes' ? 'style="display:none;"' : '' ) ?> >
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['rest_hours_range'] ?> 2</label>
										<div class="col-md-5 col-sm-5 col-xs-4" >
											<input class="form-control set_working_rest_range_from2" type="text" name="working_rest_range_from2[<?= $key ?>]" id="working_rest_range_from2" placeholder="00:00" value="<?= !empty($value['working_rest_range_from2']) ? substr($value['working_rest_range_from2'], 0, 5) : '' ?>" data-timepicker />
										</div>
										<div class="col-md-5 col-sm-5 col-xs-4" >
											<input class="form-control set_working_rest_range_to2" type="text" name="working_rest_range_to2[<?= $key ?>]" id="working_rest_range_to2" placeholder="00:00" value="<?= !empty($value['working_rest_range_to2']) ? substr($value['working_rest_range_to2'], 0, 5) : '' ?>" data-timepicker />
										</div>				
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right">Count as Late</label>
									<div class="col-md-10 col-sm-10 col-xs-4" >
										<input class="form-control set_working_period_before" type="text" name="working_period_before[<?= $key ?>]" id="working_period_before" placeholder="0" value="<?= !empty($value['working_period_before']) ? $value['working_period_before'] : '0' ?>" />
										<label style="margin-top:3px;" >
											* Set the X minutes to count as late
										</label>
									</div>
								</div>
							
								<!-- <div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['working_day_include'] ?></label>
									<div class="col-md-10 col-sm-10 col-xs-4">
										<label style="margin-top:3px;">
											<input type="checkbox" value="yes" name="working_if_include_rest[<?= $key ?>]" class="set_working_if_include_rest" <?= $value['working_if_include_rest'] == 'yes' ? 'checked' : '' ?> /> <?= $lang['check_if_count_day_include_rest_hours'] ?>
										</label>
									</div>
								</div> -->
								<div class="form-group">
									<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['ot'] ?></label>
									<div class="col-md-10 col-sm-10 col-xs-4">
										<label style="margin-top:3px;">
											<input type="checkbox" value="yes" name="working_if_ot[<?= $key ?>]" class="set_working_if_ot" <?= $value['working_if_ot'] == 'yes' ? 'checked' : '' ?> /> Tick if have OT
										</label>
									</div>
								</div>
								<div class="div_ot_box" style="display:<?= $value['working_if_ot'] == 'yes' ? 'block;' : 'none;' ?>" >
									<div class="form-group div_working_direct_day" <?= ( $value['working_if_flexi'] == 'yes' ? 'style="display:none;"' : '' ) ?> >
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['ot_count_format'] ?></label>
										<div class="col-md-10 col-sm-10 col-xs-4">
											<!-- <label style="margin-top:3px;">
												<input type="checkbox" value="yes" name="working_direct_day[<?= $key ?>]" class="set_working_count_format" <?= $value['working_direct_day'] == 'yes' ? 'checked' : '' ?> /> Tick if combine all working hours as OT hours
											</label><br /> -->
											<label style="margin-top:3px;">
												<input type="checkbox" value="yes" name="working_if_ot_morning[<?= $key ?>]" class="set_working_if_ot_morning" <?= $value['working_if_ot_morning'] == 'yes' ? 'checked' : '' ?> /> Tick if morning count as OT?
											</label>
										</div>
									</div>
									<div class="form-group">
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['ot_rounding'] ?></label>
										<div class="col-md-10 col-sm-10 col-xs-8" >
											<input class="form-control set_working_rounding_ot" type="text" name="working_rounding_ot[<?= $key ?>]" id="working_rounding_ot" placeholder="0" value="<?= !empty($value['working_rounding_ot']) ? $value['working_rounding_ot'] : '' ?>" max="60" />
											<font color="red" style="font-size: 11px;">* <?= $lang['x_minutes_for_ot_rounding_number_format'] ?></font>
										</div>
									</div>
									<div class="form-group">
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['off_duty'] ?></label>
										<div class="col-md-10 col-sm-10 col-xs-4">
											<label style="margin-top:3px;">
												<input type="checkbox" value="yes" name="working_if_offduty[<?= $key ?>]" class="set_working_if_offduty"  <?= $value['working_if_offduty'] == 'yes' ? 'checked' : '' ?> /> Tick if off duty?
											</label>

											<div>
												<input class="form-control set_working_count_offduty" type="text" name="working_count_offduty[<?= $key ?>]" id="working_count_offduty" placeholder="00:00" value="<?= !empty($value['working_count_offduty']) ? substr($value['working_count_offduty'], 0, 5) : '' ?>" data-timepicker />
												<font color="red" style="font-size: 11px;">* <?= $lang['x_minutes_later_clock_out_counted_as_ot'] ?></font>
											</div>			

											<label style="margin-top:3px;">
												<input type="checkbox" value="yes" name="working_if_deduct_offduty[<?= $key ?>]" class="set_working_if_deduct_offduty" <?= $value['working_if_deduct_offduty'] == 'yes' ? 'checked' : '' ?> /> <?= $lang['deduct_the_off_duty_x_minutes_later'] ?>
											</label>
										</div>
									</div>
									<div class="form-group">
										<label class="control-label col-md-2 col-sm-2 col-xs-4 text-right"><?= $lang['maximum_OT_hours'] ?></label>
										<div class="col-md-10 col-sm-10 col-xs-4">
											<div>
												<input class="form-control set_working_max_ot" type="text" name="working_max_ot[<?= $key ?>]" id="working_max_ot" placeholder="00:00" value="<?= !empty($value['working_max_ot']) ? substr($value['working_max_ot'], 0, 5) : '' ?>" data-timepicker />
											</div>
										</div>
									</div>
								</div>

							</div>
							
							<?php
						}
						?>

						<?php if ( ( $page_mode == 'new' && permissionCheck($row_user, 'hr-working-hours-new') ) || ( $page_mode == 'edit' && permissionCheck($row_user, 'hr-working-hours-edit') ) ){ ?>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label"></div>
	                            <div class="col-sm-9">
	                                <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_status" value="<?= $row_page['post_status'] ?>" />
	                                <input type="hidden" name="hide" value="1">
	                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
	                            </div>
	                        </div>   
	                    <?php } ?>
                    </form>
                </div>
            </div>
        </div>
		</div>
		<script src="/scripts/timepicker.js"></script><!-- timepicker -->

		<?php
		
	break ;
	
	// all working list
	case 'all' :
	default :
		

		// check permission
		if ( !permissionCheck($row_user, 'hr-working-hours-view') ){
			echo '<script>alert("Sorry You Don\'t Have The Permission.")</script>';
			
			header('Location: index.php') ;
			exit ;
		}
		// query type
		$search_query = '' ;
		
		// search query
		if ($search != ''){
			$search_query .= " AND (group_name LIKE '%".$search."%')" ;
		}
		
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			// trash item
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE " . setting_working_group . " SET
									deleted_at = '".TODAYDATE."'
									WHERE group_id = " ;
					$trash_page = trashPage('working', $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 setting_working_group
						 WHERE deleted_at IS NULL " . $search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY group_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='container' style="background-color: white; border-radius: 10px;">
			<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
				<div class="row">
                	<div class="pull-left col">
						<h1><?= $lang['working_hours'] ?> <small><?= $lang['list'] ?></small></h1>
					</div>
					<div class="pull-right col">
				<?php if ( permissionCheck($row_user, 'hr-working-hours-new') ){ ?>
					<a href="setting-working.php?page_mode=new" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['add_new'] ?></a>
				<?php } ?>
				<a href="hr-timetable.php?page_mode=user" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['by_user'] ?></a>
				<a href="hr-timetable.php?page_mode=dep" class="btn" style="color:white;background-color: #5e5bd0;" target="_blank"><?= $lang['by_department'] ?></a>
				<a href="hr-timetable.php?page_mode=schedule" class="btn" style="color:white;background-color: #5e5bd0;"  target="_blank"><?= $lang['by_schedule'] ?></a>
				</div>
			</div>
		</div>

            <form method="post">
                <?php if ( permissionCheck($row_user, 'hr-working-hours-trash') ){ ?>
	                <!-- 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" style="color:white; background-color:#5e5bd0; width:100px;" value="<?= $lang['submit'] ?>" />
	                    </div>
	                </div>
	                <!-- end trash -->
	            <?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['title'] ?></th>
									<th><?= $lang['date'] ?></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['group_id'] ;
										$title = dataFilter($row_page['group_name']) ;
		
										echo '
										 <tr class="odd gradeX">
											<td><a href="setting-working.php?page_mode=edit&page='.$id.'">'.$title.'</a></td>
											<td>'.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>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		</div>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>