<div class="row" style="padding-bottom: 2rem;">
    <div class="col-md-8">
        <div class="card bg-light-transparent" style="margin-bottom: 2rem;">
            <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">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12">
                                <div id="calendar"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card bg-light-transparent" style="margin-bottom: 2rem;">
            <div class="card-header bg-white">
                <h5 class="card-title">AV/IT Equipment 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/it_equipment_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 av/it equipment Request (<?php echo date('Y') ?>)</h6>
                        <br>
                        <div class="row">
                            <table id="table_equipment" 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><?php echo $count['total_request']; ?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </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>AV/IT Equipment Request List</h4>
                <a href="<?php echo base_url('hr/serviceform/it_equipment_form'); ?>" class="btn btn-outline-dark btn-sm" title="Add New request"><i class="fa fa-plus"></i> Add New Equipment</a>

                <a href="<?php echo base_url('hr/Serviceform_report/it_equipment_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['user']['division_id'] == 19){ ?>
                                    <div class="form-group row">
                                        <label for="sv_equipment_filter_dept" class="col-lg-3">
                                            Department
                                        </label>                           
                                        <div class="col-lg-4" >
                                            <select class="custom-select" name="sv_equipment_filter_dept" id="sv_equipment_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_equipment_filter_status" class="col-lg-3">
                                        Status
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <select class="custom-select" id="sv_equipment_filter_status" name="sv_equipment_filter_status">
                                            <option value="-1">[All]</option>
                                            <option value="Pending">Pending</option>
                                            <option value="In_progress">In Progress</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_equipment_filter_year" class="col-lg-3">
                                        Year
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_equipment_filter_year" id="sv_equipment_filter_year" class="form-control datepicker-yyyy">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_equipment_filter_month" class="col-lg-3">
                                        Requested Month
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_equipment_filter_month" id="sv_equipment_filter_month" class="form-control datepicker-mm">
                                    </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_equipment" class="table table-bordered p-0 m-0" width="100%">
                                    <thead>
                                        <th>#</th>
                                        <th>Department</th>
                                        <th>Hardware</th>
                                        <th>Software</th>
                                        <th>Check in date</th>
                                        <th>Check out date</th>
                                        <th>Status</th>
                                        <th>Action</th>           
                                    </thead>
                                    <tbody>
                                        <?php $i=1; if(!empty($equipment_list)) foreach($equipment_list as $key => $row){ ?>
                                            <tr>
                                                <td class="text-center"><?php echo $i++; ?></td>
                                                <td><?php echo $row['dept_name'].' ['.$row['branchName'].']'; ?></td>
                                                <!-- <td><?php //echo $row['hw_reason']; ?></td> -->
                                                <td><?php echo $row['hardware']; ?></td>
                                                <td><?php echo $row['software']; ?></td>
                                                <!-- <td><?php //echo $row['sw_reason']; ?></td> -->
                                                <td class="text-center"><?php echo date('d-m-Y',strtotime($row['check_in'])); ?></td>
                                                <td class="text-center"><?php echo date('d-m-Y',strtotime($row['check_out'])); ?></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">
                                                        <button type="button" class="btn btn-outline-dark" data-toggle="modal" data-target="#viewDetails" data-trigger="hover" data-tooltip="tooltip" 
                                                        title="View Detail" 
                                                        data-name="<?php echo $row['borrower_name']; ?>" 
                                                        data-dept="<?php echo $row['dept_name'].'/'.$row['branchName']; ?>" 
                                                        data-datereq="<?php echo date('d-m-Y',strtotime($row['createdTS'])); ?>" 
                                                        data-hw="<?php echo $row['hardware']; ?>" 
                                                        data-hwreason="<?php echo $row['hw_reason']; ?>" 
                                                        data-sw="<?php echo $row['software']; ?>" 
                                                        data-swreason="<?php echo $row['sw_reason']; ?>" 
                                                        data-checkin="<?php echo date('d-m-Y',strtotime($row['check_in'])); ?>" 
                                                        data-checkout="<?php echo date('d-m-Y',strtotime($row['check_out'])); ?>" 
                                                        onclick="open_view_modal(this)">
                                                        <i class="far fa-eye"></i></button>

                                                        <?php if($_SESSION['user']['division_id'] == 19 || $_SESSION['user']['division_id'] == 10){ ?>
                                                            <a type="button" class="btn btn-outline-dark" href="<?php echo base_url('hr/serviceform/it_equipment_form/').$row['ID']; ?>" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>
                                                        <?php }else{ ?>
                                                            <?php if($disabled == ''){ ?>
                                                                <a type="button" class="btn btn-outline-dark" href="<?php echo base_url('hr/serviceform/it_equipment_form/').$row['ID']; ?>" data-trigger="hover" data-tooltip="tooltip" title="Edit"><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/it_equipment_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['borrower_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
                                                            <button id="deleteButton" type="button" class="btn btn-outline-dark" onclick="delete_equipment(<?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 } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>  
    </div>   
</div>

<!-- modal view detail -->

<div class="modal fade d-print-none" id="viewDetails" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">AV/IT Equipment Request Detail</h5>&emsp;
                <!-- <a href="javascript:window.print()" class="btn btn-outline-dark btn-sm" title="Print"><i class="fa fa-print"></i>Print</a> -->
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <div class="row">
                    <div class="col-4">
                        <label>Borrower Name</label>
                        <input type="text" class="form-control" name="modal_view_name" id="modal_view_name" disabled>
                    </div>
                    <div class="col-4">
                        <label>Department</label>
                        <input type="text" class="form-control" name="modal_view_dept" id="modal_view_dept" disabled>
                    </div>
                    <div class="col-4">
                        <label>Date Request</label>
                        <input type="text" class="form-control" name="modal_view_date_req" id="modal_view_date_req" disabled>
                    </div>
                </div>
                <div class="row">
                    <div class="col-6">
                        <label>Hardware</label>
                        <input type="text" class="form-control" name="modal_view_hardware" id="modal_view_hardware" disabled>
                    </div>
                    <div class="col-6">
                        <label>Reason</label>
                        <input type="text" class="form-control" name="modal_view_hw_reason" id="modal_view_hw_reason" disabled>
                    </div>
                </div>

                <div class="row">
                    <div class="col-6">
                        <label>Software</label>
                        <input type="text" class="form-control" name="modal_view_software" id="modal_view_software" disabled>
                    </div>
                    <div class="col-6">
                        <label>Reason</label>
                        <input type="text" class="form-control" name="modal_view_sw_reason" id="modal_view_sw_reason" disabled>
                    </div>
                </div>

                <div class="row">
                    <div class="col-6">
                        <label>Check In</label>
                        <input type="text" class="form-control" name="modal_view_checkin" id="modal_view_checkin" disabled>
                    </div>
                    <div class="col-6">
                        <label>Check Out</label>
                        <input type="text" class="form-control" name="modal_view_checkout" id="modal_view_checkout" disabled>
                    </div>
                </div>
                <br>
                <div class="float-right">
                </div>
            </div>
        </div>
    </div>
</div>

<!-- modal view detail -->


<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>
    function delete_equipment(ID)
    {
        validateForm("#deleteButton", "Delete this equipment list?", function() {
            $.ajax({
                url:'<?php echo base_url('hr/serviceform/it_equipment_form_delete') ?>',
                data: {
                    equipment_id : ID,
                },
                type: 'post',
                dataType: 'json',
                success: (data) => {
                    if(data.status == "Success") {

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

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

    $(document).ready(function(){
        $('#table_equipment').DataTable().destroy();
        var fc_event= '';
        var resource = <?php echo $resource;?>;

        var fullcalendar = $('#calendar').fullCalendar({ 
            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];
                console.log(hover_item);
                var title = JSON.stringify(data.title);
                var checkin = moment(hover_item.check_in).format('DD-MM-YYYY');
                var checkout = moment(hover_item.check_out).format('DD-MM-YYYY');

              	// if(hover_item.room_id == 7)
              	// {
              	// 	var room = hover_item.other_room;
              	// }else {
              	// 	var room = hover_item.room;
              	// }

                // console.log(hover_item.room_id);

                tooltip = '<div class="tooltipevent"> Requester : '+hover_item.borrower_name+'['+hover_item.staff_id+']'+'<br/> Email : '+hover_item.email+'<br/> Hardware : '+hover_item.hardware+'<br/> Software : '+hover_item.software+'<br/> Check-in date : '+checkin+'<br/> Check-out date : '+checkout+'<br/>Department : '+hover_item.dept_name+'/'+hover_item.branchName+'</div>';

                // console.log(tooltip);
                $("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();

        $('#searchBtn').click(function(){
            getData();
        });

        <?php $this->session->unset_userdata('sv_equipment_filter_dept'); ?>
        <?php $this->session->unset_userdata('sv_equipment_filter_year'); ?>
        <?php $this->session->unset_userdata('sv_equipment_filter_month'); ?>
        <?php $this->session->unset_userdata('sv_equipment_filter_status'); ?>
        <?php $this->session->unset_userdata('sv__it__equipment'); ?>
    });


    function open_view_modal(elem) 
    {
        let name        = $(elem).data('name');
        let dept        = $(elem).data('dept');
        let datereq     = $(elem).data('datereq');
        // console.log(dept);
        let hw          = $(elem).data('hw');
        let hwreason    = $(elem).data('hwreason');
        let sw          = $(elem).data('sw');
        let swreason    = $(elem).data('swreason');
        let checkin     = $(elem).data('checkin');
        let checkout    = $(elem).data('checkout');

        /* view modal */
        $('#modal_view_name').val(name);
        $('#modal_view_dept').val(dept);
        $('#modal_view_date_req').val(datereq);
        $('#modal_view_hardware').val(hw);
        $('#modal_view_hw_reason').val(hwreason);
        $('#modal_view_software').val(sw);
        $('#modal_view_sw_reason').val(swreason);
        $('#modal_view_checkin').val(checkin);
        $('#modal_view_checkout').val(checkout);
    }

    function getData()
    {
        var table = $('#filter_table_equipment').DataTable();
        // $('#table_equipment').DataTable().destroy();
        table.clear().draw();
        var frmData = new FormData();

        frmData.append('sv_equipment_filter_dept',$('#sv_equipment_filter_dept').val());
        frmData.append('sv_equipment_filter_status', $('#sv_equipment_filter_status').val());
        frmData.append('sv_equipment_filter_year',$('#sv_equipment_filter_year').val());
        frmData.append('sv_equipment_filter_month',$('#sv_equipment_filter_month').val());
        frmData.append('user_id',$('#user_id').val());

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

                iziToast.success({
                    position: 'bottomRight',
                    message: 'filtering data',
                });

                let equipment = data.table;

                if(equipment != null){
                    var index = 1;
                    $.each(equipment,function(key,content)
                    {
                        var i = index + key;

                        if(content.check_out != '0000-00-00' && content.check_out != undefined)
                        {
                            var check_out = moment(content.check_out).format('DD-MM-YYYY');
                        }
                        if(content.check_in != '0000-00-00' && content.check_in != undefined)
                        {
                            var check_in = moment(content.check_in).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;"';
                        }

                        if("<?php echo $_SESSION['user']['division_id']; ?>" == 19)
                        {
                            var btn_edit = '<a type="button" class="btn btn-outline-dark" href="<?php echo base_url();?>hr/serviceform/it_equipment_form/'+content.ID+'" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>';
                        } else {    
                            if(disabled == '')
                            {
                                var btn_edit = '<a type="button" class="btn btn-outline-dark" href="<?php //echo base_url();?>hr/serviceform/it_equipment_form/'+content.ID+'" 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_view = '<button type="button" class="btn btn-outline-dark" data-toggle="modal" data-target="#viewDetails" data-trigger="hover" data-tooltip="tooltip" title="View Detail" data-name="'+content.borrower_name+'" data-dept="'+content.dept_name+'/'+content.branchName+'" data-datereq="'+moment(content.createdTS).format('DD-MM-YYYY')+'" data-hw="'+content.hardware+'" data-hwreason="'+content.hw_reason+'" data-sw="'+content.software+'" data-swreason="'+content.sw_reason+'" data-checkin="'+check_in+'" data-checkout="'+check_out+'" onclick="open_view_modal(this)"> <i class="far fa-eye"></i> </button>';

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

                        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.borrower_id) || ('<?php echo $_SESSION['user']['id']; ?>' == content.createdBY))
                        {
                            var btn = btn_view+btn_edit+btn_print+btn_delete;
                        }else{
                            var btn = btn_view+btn_edit+btn_print;
                        }
                        
                        table.row.add({
                            0   : i,
                            1   : content.dept_name+'/'+content.branchName,
                            2   : content.hardware,
                            3   : content.software,
                            4	: '<div class="text-center">'+check_in+'</div>',
                            5	: '<div class="text-center">'+check_out+'</div>',
                            6	: '<div class="text-center">'+status+'</div>',
                            7	: '<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>