<!-- Bootstrap Include -->
<link rel="stylesheet" type="text/css" href="./modules/meeting/calendar/bootstrap-3.3.5-dist/css/bootstrap.min.css">
<script src="./modules/meeting/calendar/bootstrap-3.3.5-dist/js/jquery-1.11.3.min.js"></script>
<script src="./modules/meeting/calendar/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>


<link href='./modules/meeting/calendar/fullcalendar-2.4.0/fullcalendar.css' rel='stylesheet' />
<link href='./modules/meeting/calendar/fullcalendar-2.4.0/fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='./modules/meeting/calendar/fullcalendar-2.4.0/lib/moment.min.js'></script>
<script src='./modules/meeting/calendar/fullcalendar-2.4.0/fullcalendar.min.js'></script>
<script src='./modules/meeting/calendar/fullcalendar-2.4.0/lang/th.js'></script>
<!--Bootstrap selectpicker -->
<script src="./modules/meeting/calendar/js/bootstrap-select.min.js"></script>
<link href="./modules/meeting/calendar/css/bootstrap-select.min.css" rel="stylesheet" media="screen">
<!-- Bootstrap Select Picker -->
<script>
 	$(function () {
    $('.selectpicker').selectpicker()
     	})
</script>

<?php
error_reporting( error_reporting() & ~E_NOTICE );
$room_index="";
if(isset($_POST['room_index'])!=""){ 
    $room_index = $_POST['room_index'];
}else{
    $sql_room="select * from meeting_room where active='1' order by id limit 0,1 ";
    $dbquery_room = mysqli_query($connect,$sql_room);
 While ($result_room = mysqli_fetch_array($dbquery_room)){
    $room_index=$result_room['id'];
 }
}


//Database
$data = array();
$link = mysqli_connect($hostname, $user, $password, $dbname);
mysqli_set_charset($link, 'utf8');
if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

$sql_name = "select * from person_main where status='0' order by id";
$dbquery_name = mysqli_query($connect,$sql_name);
While ($result_name = mysqli_fetch_array($dbquery_name))
{
$name_ar[$result_name['person_id']]=$result_name['name']." ".$result_name['surname'];
}

 $sql = "SELECT a.id,
  CONCAT(a.book_date,'T',LPAD(a.start_time,2,'0'),':00:00')
  as start,
  CONCAT(a.book_date_end,'T',LPAD(a.finish_time,2,'0'),':00:00')
 as end,
 CONCAT(a.objective,' เวลา ',start_time,':00น. - ',finish_time,':00น. ')
 as title,
 CASE 
  WHEN approve IS NULL THEN '#FF0000'
  WHEN approve=1 THEN '#00BB00'
  ELSE ''
  END as color,
  a.book_person 
FROM meeting_main a
";


$sql = $sql." where room=$room_index AND (approve IS NULL or approve =1)  ";


$sql = $sql."  order by book_date,start_time ";

if ($result = $link->query($sql)) {
    /* fetch object array */
    while ($obj = $result->fetch_object()) {
        $obj->title=$obj->title."[".$name_ar[$obj->book_person]."]";
       $data[] = array(
                    'id' => $obj->id,
                    'title'=> $obj->title,
                    'start'=> $obj->start,
                    'end'=> $obj->end,
                    //'url'=> $obj->url,
                    'color'=>$obj->color
                    );
    }
    /* free result set */
    $result->close();
}
mysqli_close($link);
?>
<script>
    $(document).ready(function() {
        
        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,basicWeek,basicDay'
            },
            defaultDate: '<?php echo date('Y-m-d');?>',
            editable: false,
            displayEventEnd:true,
            displayEventTime:false,
            lang : 'th',
            eventLimit: false , // allow "more" link when too many events
            allDay: true ,
            events : <?php echo json_encode($data);?>
        });
        
    });
</script>
<BR>
<div class="container">
  <div class="panel panel-default">
      
     <?php
    $sql_room="select * from meeting_room where id='$room_index' ";
    $dbquery_room = mysqli_query($connect,$sql_room);
 While ($result_room = mysqli_fetch_array($dbquery_room)){
    $room_name=$result_room['room_name'];
}   
      ?>  
      <div class="panel-heading"><h3 class="panel-title">ปฏิทินการใช้ห้องประชุม : <?php echo $room_name;?></h3></div>
      <div class="panel-body">
          

 <?php
    $sql_room="select * from meeting_room where active='1'  order by id";
    $dbquery_room = mysqli_query($connect,$sql_room);

//เพิ่มการเลือกสถานะ
echo "<form class='form-inline'   name='frm1' enctype='multipart/form-data' method='POST'> ";
echo  "<div class='form-group'>";
echo "&nbsp;<Select  name='room_index' size='1' class='selectpicker'>";
//echo "<option value ='' >ทุกห้องประชุม</option>" ;
While ($result_room = mysqli_fetch_array($dbquery_room)){
    if($room_index==$result_room['id']){ $show="selected";}else{ $show=""; }
 echo "<option  value =$result_room[room_code] $show >$result_room[room_name] </option>" ;
}
echo "</select>";
echo "&nbsp;<INPUT TYPE='button' name='smb' class='btn btn-warning' value='เลือกห้องประชุม'  onclick='goto_url2(1)'>";
echo "            </div>";
echo "</form>";
?> 
            
<div class="panel-body" id='calendar'></div>
<FONT SIZE="2" COLOR="">หมายเหตุ <IMG SRC="modules/meeting/images/level1.gif" WIDTH="20" HEIGHT="11" BORDER="0" ALT="อนุมัติแล้ว">อนุมัติแล้ว&nbsp;<IMG SRC="modules/meeting/images/level4.gif" WIDTH="20" HEIGHT="11" BORDER="0" ALT="รอการอนุมัติ">รอการอนุมัติ</FONT>
      </div>
    </div>

<script>
function goto_url2(val){
callfrm("?option=meeting&task=calendar/fullcalendar");
}
</script>
