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

// active menu bar
$active_main_menu 	= 'setting' ;
$active_sub_menu 	= 'setting-hr' ;
$active_menu 		= 'setting-employment-schedule-date' ;

// check query exsits
$page_id 			= '' ;
$weekdays 			= [] ;
$submit_type 		= 'new' ;
$mysqli_page 		= $mysqli->query("SELECT * FROM setting_employment_schedule
									  WHERE deleted_at IS NULL LIMIT 1") ;
if ( $mysqli_page->num_rows == 0 ){
	$mysqli->query( "INSERT INTO setting_employment_schedule
					 ( created_at ) VALUES
					 ( '".TODAYDATE."' )" ) ;
	$page_id 		= $mysqli->insert_id ;
}else{
	$row_page       = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
	$page_id 		= $row_page['schedule_id'] ;
	$weekdays 		= json_decode( $row_page['weekdays'] ) ;
}

// update database
if ( $_POST['hide'] == 1 ){
	
	// update database
	$mysqli->query("UPDATE setting_employment_schedule SET
					weekdays          = '".json_encode($_POST['weekdays'])."'
					WHERE schedule_id = '".$page_id."'") ;
	
	// refresh page
	header("Location:?page_mode=edit&success=1") ;
	$_SESSION['system_result'] = 'success-updated' ;
	exit ;
}

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

?>

<div class="warper container-fluid">   	
	<div class="page-header"><h1>Schedule Interview Date</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="?page_mode=edit&type=edit" novalidate="novalidate">

                <div class="form-group">
                    <div class="col-sm-2 control-label">Weekday / Time</div>
                    <div class="col-sm-9">
                        <select name="weekdays[]" class="form-control ui-search-input select2-basic-single" multiple>
                            <?php foreach ( [
                                '0' => 'Sunday',
                                '1' => 'Monday',
                                '2' => 'Tuesday',
                                '3' => 'Wednesday',
                                '4' => 'Thursday',
                                '5' => 'Friday',
                                '6' => 'Saturday'
                            ] as $kweekday => $vweekday ){ ?>
                                <?php for ( $a = 7 ; $a <= 19 ; $a++ ){ ?>
                                    <?php foreach ( [ '00', '30' ] as $k => $v ){ $time = $a.':'.$v ; $weekday = $kweekday.'/'.$time ; ?>
                                        <option value="<?= $weekday ?>" <?= in_array( $weekday ,$weekdays ) ? 'selected' : '' ?> ><?= $vweekday . ' / ' . $time ?></option>
                                    <?php } ?>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    </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">
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>

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