<div class="container-fluid" style="padding-bottom: 2rem;">
    <div class="row">
        <div class="col-md-6">
            <div class="card bg-light-transparent" style="margin-bottom: 2rem; height: 64.49rem;">
                <div class="card-header bg-white">
                    <div class="float-left">
                        <h5 class="card-title">Calendar</h5>
                    </div>
                </div>
                <div class="card-body">
                    <div class="card bg-white no-shadow" style="height: 58rem;">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-12">
                                    <div id="calendar"></div>
                                </div>
                                <?php //debug($type) ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card bg-light-transparent" style="margin-bottom: 2rem;">
                <div class="card-header bg-white">
                    <h5 class="card-title">Hotel Request Summary</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-12">
                            <a class="btn btn-block btn-dark add-shadow" href="<?php echo base_url('hr/serviceform/hotel_form'); ?>" style="padding-top: 1rem; padding-bottom: 1rem;">New Request</a>
                        </div>
                    </div>
                    <br>
                    <div class="card bg-white no-shadow">
                        <div class="card-body">
                            <h6>Total Hotel Request (<?php echo date('Y') ?>)</h6>
                            <br>
                            <div class="row">
                                <table id="table_hotel" class="table table-bordered table-striped table-hover mx-1">
                                    <thead>
                                        <th class="text-center">Department</th>
                                        <th class="text-center">Total Request</th>
                                    </thead>
                                    <tbody>
                                        <?php if(!empty($count_request)) foreach($count_request as $key => $count){ ?>
                                            <tr>
                                                <td><?php echo $count['dept_name'].'/'.$count['branchName']; ?></td>
                                                <td class="text-center"><?php echo $count['total_request']; ?></td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="container-fluid" style="padding-bottom: 2rem;">
    <div class="card bg-light-transparent">  
        <div class="card-header bg-white">
            <div class="float-left">
                <h4>Hotel Booking List</h4>

                <a href="<?php echo base_url('hr/serviceform/hotel_form'); ?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Add New Hotel"><i class="fa fa-plus"></i> Add New Hotel Booking</a>

                <a href="<?php echo base_url('hr/Serviceform_report/hotel_print')?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print"><i class="fa fa-print"></i>Print</a>

                <a id="btnShowFilters" type="button" class="btn btn-outline-dark btn-sm collapsed" data-trigger="hover" title="Show Filters Box" data-toggle="collapse" data-target="#collapseFilter" aria-expanded="false"><i class="fas fa-filter"></i>Show Filters</a> 
            </div>
        </div>

        <div class="collapse" id="collapseFilter">
            <div class="card-header">
               <div class="row">
                    <div class="col">
                        <div class="card no-shadow">
                            <div class="card-body">
                               <input type="hidden" name="user_id" id="user_id" value="<?php echo $_SESSION['user']['id']; ?>">
                               <?php if($_SESSION['module_data']['role'] == 'admin'){ ?>  
                                <div class="form-group row">
                                    <label for="sv_hotel_filter_dept" class="col-lg-3">
                                        Department
                                    </label>                           
                                    <div class="col-lg-4">
                                        <select class="custom-select" name="sv_hotel_filter_dept" id="sv_hotel_filter_dept">
                                         <option value="-1">[All]</option>
                                         <?php if($department) foreach($department as $row){ ?>
                                            <option value="<?php echo $row['id']; ?>"><?php echo $row['name'].' ['.$row['branch_name'].']'; ?></option> 
                                        <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <?php } ?>
                                <div class="form-group row">
                                    <label for="sv_hotel_filter_status" class="col-lg-3">
                                        Status
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <select class="custom-select" id="sv_hotel_filter_status" name="sv_hotel_filter_status">
                                            <option value="-1">[All]</option>
                                            <option value="In_progress">In Progress</option>
                                            <option value="Pending">Pending</option>
                                            <option value="Verified">Verified</option>
                                            <option value="Approved">Approved</option>
                                            <option value="Rejected">Rejected</option>
                                            <option value="Cancel">Cancel</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_hotel_filter_year" class="col-lg-3">
                                        Year
                                    </label>                           
                                    <div class="col-lg-4" >
                                      <input type="text" name="sv_hotel_filter_year" id="sv_hotel_filter_year" class="form-control datepicker-yyyy">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_hotel_filter_month" class="col-lg-3">
                                        Requested Month
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_hotel_filter_month" id="sv_hotel_filter_month" class="form-control datepicker-mm">
                                    </div>
                                </div>
                                <!-- <div class="form-group row">
                                    <label for="sv_hotel_filter_check_in" class="col-lg-3">
                                        Check In Date
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_hotel_filter_check_in" id="sv_hotel_filter_check_in" class="form-control datepicker">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_hotel_filter_check_out" class="col-lg-3">
                                        Check Out Date
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_hotel_filter_check_out" id="sv_hotel_filter_check_out" class="form-control datepicker">
                                    </div>
                                </div> -->
                                <div class="row">
                                    <div class="col d-print-none">
                                        <button class="btn btn-info" id="searchBtn">Search</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div> 

        <div class="tab-content">
            <div class="tab-pane fade active show" id="tableView" role="tabpanel" aria-labelledby="nav-table-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="filter_table_hotel" class="table table-bordered p-0 m-0" width="100%">
                                    <thead> 
                                        <th class="text-center" width="5%">#</th>
                                        <th class="text-center" width="20%">Requestor</th>
                                        <th class="text-center" width="10%">Hotel Name</th>
                                        <th class="text-center" width="27%">Location </th>
                                        <th class="text-center" width="10%">Check In Date and Time</th>
                                        <th class="text-center" width="10%">Check Out Date and Time</th>
                                        <th class="text-center" width="5%">Pax</th>
                                        <th class="text-center" width="8%">Status</th>
                                        <th class="text-center" width="5%">Action</th>           
                                    </thead>
                                    <tbody>
                                        <?php if(!empty($rsHotelList)) { ?> 
                                            <?php foreach($rsHotelList as $key => $row) {
                                                ?>
                                                <tr>
                                                    <td class="text-center"><?php echo $key+1; ?></td>
                                                    <td>
                                                        <b>Requested By : </b><br><?php echo $row['requestor'].' ['.$row['requestor_email'].']'; ?>
                                                        <br><br>
                                                        <b>Created By : </b><br><?php echo $row['created_name'].' ['.$row['created_email'].']' ?>
                                                    </td>
                                                    <td><?php echo $row['hotel_name']; ?></td>
                                                    <td><?php echo $row['location']; ?></td>
                                                    <td class="text-center"><?php echo date ('d-m-Y', strtotime($row['checkin_date'])) . ' / ' . $row['checkin_time']; ?></td>
                                                    <td class="text-center"><?php echo date ('d-m-Y', strtotime($row['checkout_date'])) . ' / ' . $row['checkout_time']; ?></td>
                                                    <td class="text-center"><?php echo $row['pax']; ?></td>
                                                    <td class="text-center">
                                                        <?php 
                                                            if(($row['status_app']==null || $row['status_app']=='Pending'))
                                                            {
                                                                $status = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                                                                $disabled = '';
                                                            }
                                                            if($row['status_app'] == 'In_progress')
                                                            {
                                                                $status = str_replace('_', ' ', $row['status_app']).' <i class="fa fa-hourglass-start" style="color: grey;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }
                                                            if($row['status_app'] == 'Verified')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;">'.$row['status_reason'].'</p>';
                                                                $disabled = '';
                                                            }
                                                            if($row['status_app'] == 'Approved')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-check-circle" style="color: green;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }
                                                            if($row['status_app'] == 'Rejected')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-times-circle" style="color: red;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }
                                                            if($row['status_app'] == 'Cancel')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-times-circle" style="color: red;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }

                                                            echo $status;
                                                        ?> 
                                                    </td>
                                                    <td class="text-center">
                                                        <div class="btn-group btn-group-sm" role="group">
                                                            <a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" 
                                                            title="View Detail" href="<?php echo base_url('hr/serviceform/hotel_form/'.$row['ID'].'/view'); ?>" target="_blank"><i class="far fa-eye"></i></a>

                                                            <?php if(($_SESSION['user']['id'] == $row['requestor_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
                                                                <?php if($disabled == ''){ ?>
                                                                    <a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit" href="<?php echo base_url('hr/serviceform/hotel_form/'.$row['ID']); ?>" target="_blank"><i class="far fa-edit"></i></a>
                                                                <?php }else{ ?>
                                                                    <button class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit" disabled style="cursor: not-allowed;"><i class="far fa-edit"></i></button>
                                                                <?php } ?>
                                                            <?php } ?>

                                                            <a type="button" class="btn btn-outline-dark" href="<?php echo base_url('hr/serviceform_report/hotel_print/').$row['ID']; ?>" target="_blank" data-trigger="hover" data-tooltip="tooltip" title="Print Detail"><i class="fa fa-print"></i></a>

                                                            <?php if(($_SESSION['user']['id'] == $row['requestor_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
                                                            <button id="deleteButtonHotel" type="button" class="btn btn-outline-dark" onclick="deleteHotel(<?php echo $row['ID']; ?>);" data-trigger="hover" data-tooltip="tooltip" title="Delete" <?php echo ($disabled == '')?'':'disabled style="cursor: not-allowed;"'; ?>><i class="far fa-trash-alt"></i></button>
                                                            <?php } ?>
                                                        </div>
                                                    </td>  
                                                </tr>
                                            <?php } ?>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>      
    </div>
</div>

<style type="text/css">
    .tooltipevent
    {
        width:auto;
        height:auto;
        background:rgba(0,0,0,0.75);
        position:absolute;
        color: #fff;
        z-index:10001;
        padding:10px 10px 10px 10px ;  
        line-height: 200%;
    }
</style>

<script>
    $(document).ready(function()
    {
        <?php $this->session->unset_userdata('sv_hotel_filter_dept'); ?>
        <?php $this->session->unset_userdata('sv_hotel_filter_status'); ?>
        <?php $this->session->unset_userdata('sv_hotel_filter_year'); ?>
        <?php $this->session->unset_userdata('sv_hotel_filter_month'); ?>
        <?php $this->session->unset_userdata('sv__hotel'); ?>
        
        $('#searchBtn').click(function(){
            getData();
        });

        $('#table_hotel').DataTable().destroy();

        var fc_event= '';
        var resource = <?php echo $resource;?>;

        var fullcalendar = $('#calendar').fullCalendar(
        {
            height: 860,
            themeSystem: 'bootstrap4',
            header: 
            {
                left: 'prev,next today',
                center: 'title',
                right: 'month,listMonth'
            },
            buttonText: {
                today: 'Today',
                month: 'Calendar',
                listMonth: 'List'
            },

            navLinks: true, // can click day/week names to navigate views
            editable: true,
            eventLimit: true, // allow "more" link when too many events
            events: <?php echo $fc_event; ?>,

            eventMouseover: function (data, event, param, resource = <?php echo $resource;?>) 
            {
                var js_res = JSON.parse(JSON.stringify(resource));
                var hover_item = js_res[data.id];
                var title = JSON.stringify(data.title);
                var checkin = moment(hover_item.checkin_date).format('DD-MM-YYYY');
                var checkout = moment(hover_item.checkout_date).format('DD-MM-YYYY');


                tooltip = '<div class="tooltipevent"> Requester : '+hover_item.requestor+' ['+hover_item.staff_id+']'+'<br/> Hotel name : '+hover_item.hotel_name+'<br/> Location : '+hover_item.location+'<br/> Check-in Date : '+checkin+'<br/> Check-out Date : '+checkout+'<br/>Department : '+hover_item.dept_name+'/'+hover_item.branchName+'</div>';

                $("body").append(tooltip);
                $(this).mouseover(function (e) 
                {
                    $(this).css('z-index', 10000);
                    $('.tooltipevent').fadeIn('500');
                    $('.tooltipevent').fadeTo('10', 1.9);
                }).mousemove(function (e) 
                {
                    $('.tooltipevent').css('top', e.pageY + 10);
                    $('.tooltipevent').css('left', e.pageX + 20);
                });
            },

            eventMouseout: function(data, event) 
            {
                $(this).css('z-index', 8);
                $('.tooltipevent').remove();
            },
        });

        fullcalendar.fullCalendar();
    });


    function deleteHotel(ID) 
    {
        validateForm("#deleteButtonHotel", "Delete this list?", function() {
            $.ajax({
                url:'<?php echo base_url('hr/serviceform/deleteHotelList') ?>',
                data: {
                    hotelID : ID,
                },
                type: 'post',
                dataType: 'json',
                success: (data) => {
                    if(data.status == "Success") {

                        localStorage.setItem("status", data.status);
                        localStorage.setItem("msg", data.msg);

                        location.reload();
                }
            }
        });
        });
    }

    function getData() {
        var table = $('#filter_table_hotel').DataTable();
        table.clear().draw();
        var frmData = new FormData();

        frmData.append('sv_hotel_filter_dept',$('#sv_hotel_filter_dept').val());
        frmData.append('sv_hotel_filter_status',$('#sv_hotel_filter_status').val());
        frmData.append('sv_hotel_filter_year',$('#sv_hotel_filter_year').val());
        // frmData.append('sv_hotel_filter_month',$('#sv_hotel_filter_month').val());
        frmData.append('sv_hotel_filter_check_in',$('#sv_hotel_filter_check_in').val());
        frmData.append('sv_hotel_filter_check_out',$('#sv_hotel_filter_check_out').val());
        frmData.append('user_id',$('#user_id').val());

        $.ajax({
            url:"<?php echo base_url('/hr/api/serviceform/get_hotel_filter'); ?>",
            type:'post',
            dataType:'json',
            data:frmData,
            processData: false,
            contentType: false,
            success:function(data){
                iziToast.success({
                    position: 'bottomRight',
                    message: 'filtering data',
                });

                let hotel = data.table;

                if(hotel != null){
                    $.each(hotel,function(key,content)
                    {
                        var i = key + 1;
                        
                        var requestor_name = '<b>Requestor : </b><br>'+content.requestor+' ['+content.requestor_email+']'
                        var creator_name = '<b>Creator : </b><br>'+content.created_name+' ['+content.created_email+']'
                        
                        if(content.checkin_date != '0000-00-00'){
                            var checkin_date = moment(content.checkin_date).format('DD-MM-YYYY');;
                        }
                        if(content.checkout_date != '0000-00-00'){
                            var checkout_date = moment(content.checkout_date).format('DD-MM-YYYY');;
                        }

                        if(content.status_app == null || content.status_app == 'Pending')
                        {
                            var status      = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                            var disabled    = '';
                        }
                        if(content.status_app == 'In_progress')
                        {
                            var status      = ((content.status_app).replace('_',' '))+' <i class="fa fa-hourglass-start" style="color: grey;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }
                        if(content.status_app == 'Verified')
                        {
                            var status      = content.status_app+' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;">'+content.status_reason+'</p>';
                            var disabled    = '';
                        }
                        if(content.status_app == 'Approved')
                        {
                            var status      = content.status_app+' <i class="fa fa-check-circle" style="color: green;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }
                        if(content.status_app == 'Rejected')
                        {
                            var status      = content.status_app+' <i class="fa fa-times-circle" style="color: red;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }
                        if(content.status_app == 'Cancel')
                        {
                            var status      = content.status_app+' <i class="fa fa-times-circle" style="color: red;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }

                        var btn_view = '<a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="View Detail" href="<?php echo base_url();?>hr/serviceform/hotel_form/'+content.ID+'/view" target="_blank"><i class="far fa-eye"></i></a>';

                        var btn_print = '<a type="button" class="btn btn-outline-dark" href="<?php echo base_url();?>hr/serviceform_report/hotel_print/'+content.ID+'" target="_blank" data-trigger="hover" data-tooltip="tooltip" title="Print Detail"><i class="fa fa-print"></i></a>';

                        if(disabled == '')
                        {
                            var btn_edit = '<a type="button" class="btn btn-outline-dark" href="<?php echo base_url();?>hr/serviceform/hotel_form/'+content.ID+'" target="_blank" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>';
                        }else{
                            var btn_edit = '<button class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit" '+disabled+'><i class="far fa-edit"></i></button>';
                        }

                        var btn_delete = '<button type="button" class="btn btn-outline-dark" onclick="delete_equipment('+content.ID+')" data-trigger="hover" data-tooltip="tooltip" title="Delete"'+disabled+'><i class="far fa-trash-alt"></i></button>';

                        if(('<?php echo $_SESSION['user']['id']; ?>' == content.requestor_id) || ('<?php echo $_SESSION['user']['id']; ?>' == content.createdBY))
                        {
                            var btn = btn_view+btn_edit+btn_print+btn_delete;
                        }else{
                            var btn = btn_view+btn_print;
                        }


                        table.row.add({
                            0   : '<div class="text-center">'+i+'</div>',
                            1   : requestor_name+'<br><br>'+creator_name,
                            2   : content.hotel_name,
                            3   : content.location,
                            4   : '<div class="text-center">'+checkin_date+' / '+content.checkin_time+'</div>',
                            5   : '<div class="text-center">'+checkout_date+' / '+content.checkout_time+'</div>',
                            6   : '<div class="text-center">'+content.pax+'</div>',
                            7   : '<div class="text-center">'+status+'</div>',
                            8	: '<div class="text-center"><div class="btn-group btn-group-sm" role="group">'+btn+'</div></div>',
                        }).draw();
                        console.log(data);

                    });                        
                }

            },
            error:function(){
                iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    }
</script>