<style type="text/css">
@media print
{
.noprint {display:none;}
}
</style>
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <button type="button" onclick="openaddnew();" class="btn btn-custom waves-effect waves-light" >Add <span class="m-l-5"><i class="fa fa-plus"></i></span></button>
                    
                </div>
                <h4 class="page-title">Floors</h4>
            </div>
            
            
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box table-responsive">
                    
                    <h4 class="header-title m-t-0 m-b-30">&nbsp;</h4>

                    <table id="datatable-buttons" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                
                                <th>ID</th>
                                <th>Floor Name</th>
                                <th>Workstations</th>                                
                                <th>Floor Manager</th>
                                
                                <th class='noprint'>Action</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php if(isset($floors)) { foreach ($floors as $floor){ ?>
                            <tr>
                                
                                <td><?php echo $floor['id_business_floors']; ?></td>
                                <td><?php echo $floor['floor_name']; ?></td>
                                <td><span class="label label-pink text-inverse" style="font-size: 12px;"><?php echo $floor['floor_workstations']; ?></span></td>
                                <td>
                                    <?php echo $floor['floormanager_name']; ?>
                                </td>
                                <td class='noprint' > 
                                    <button id='btnedit' onclick="openupdate(<?php echo $floor['id_business_floors']; ?>);" class="btn btn-icon waves-effect waves-light btn-info m-b-5"> <i class="fa fa-keyboard-o"></i> </button> 
                                </td>
                            </tr>
                            <?php }} ?>
                        </tbody>
                    </table>
                </div>
            </div><!-- end col -->
        </div>
        <!-- end row -->
        
        <!--Modals-->
        
        <!--Add Service Category Modal-->
        <div id="addfloor" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addfloor" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Add a new Floor</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtfloorname" class="control-label">Floor Name</label>
                                    <input type="text" class="form-control" placeholder="Floor Name" id="txtfloor_name" name="txtfloor_name">
                                </div> 
                            </div> 
                        </div>
                         <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtfloormanager_id" class="control-label">Floor Manager</label>
                                    <select class="form-control" id="txtfloormanager_id" name="txtfloormanager_id">
                                        <?php if(isset($staffs)){foreach($staffs as $staff){?>
                                        <option value="<?php echo $staff['id_staff']; ?>"><?php echo $staff['staff_fullname'].' - '.$staff['floor']; ?></option>
                                        <?php } } ?>
                                    </select>                                    
                                </div> 
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtfloor_workstations" class="control-label">Floor Workstations</label>
                                    <input type="number" min="1" class="form-control" placeholder="Floor Workstations" id="txtfloor_workstations" name="txtfloor_name" value="1">
                                </div> 
                            </div> 
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button onclick="addnew();" type="button" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!--End Service Category Modal-->
        
        <!--Edit Businenss Floor Modal-->
        <div id="editbusinessfloor" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editbusinessfloor" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Edit Business Floor</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditbusiness_floor_id" class="control-label">ID</label>
                                    <input readonly="readonly" type="text" class="form-control" id="txteditbusiness_floor_id" name="txteditbusiness_floor_id">
                                </div> 
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditfloor_name" class="control-label">Floor Name</label>
                                    <input type="text" class="form-control" placeholder="Floor Name" id="txteditfloor_name" name="txteditfloor_name">
                                </div> 
                            </div> 
                        </div>
                         <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditfloormanager_id" class="control-label">Floor Manager</label>
                                    <select class="form-control" id="txteditfloormanager_id" name="txteditfloormanager_id">
                                        <?php if(isset($staffs)){foreach($staffs as $staff){?>
                                        <option value="<?php echo $staff['id_staff']; ?>"><?php echo $staff['staff_fullname'].' - '.$staff['floor']; ?></option>
                                        <?php } } ?>
                                    </select>                                    
                                </div> 
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditfloor_workstation" class="control-label">Floor Workstations</label>
                                    <input type="number" min="1" class="form-control" placeholder="Floor Workstations" id="txteditfloor_workstation" name="txteditfloor_workstation" >
                                </div> 
                            </div> 
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button onclick="update();" type="button" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!--End Edit Service Category Modal-->
        
        
        
        <script>
           $(document).ready(function() {

                $('#datatable-buttons').DataTable({
                    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    stateSave: true,
                    dom: "Bfrtlip",
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"}, {
                            extend: "excel",
                            className: "btn-sm btn-warning btn-trans"
                        }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                    responsive: !0
                });
                
                toastr.options = {
                   "closeButton": true,
                   "debug": true,
                   "newestOnTop": true,
                   "progressBar": true,
                   "positionClass": "toast-bottom-center",
                   "preventDuplicates": true,
                   "onclick": null,
                   "showDuration": "300",
                   "hideDuration": "1000",
                   "timeOut": "5000",
                   "extendedTimeOut": "1000",
                   "showEasing": "swing",
                   "hideEasing": "linear",
                   "showMethod": "fadeIn",
                   "hideMethod": "fadeOut"
               }
                
            });
            
            
            function openaddnew(){
                //alert('ok');
                $("#addfloor").modal('show');
            }
            
            
            function addnew(){
                if($("#txtfloorname").val()!== ""){
                $.ajax({
                        type: 'POST',
                        url: '<?php echo base_url();?>floor_controller/add_floor',
                        data: {
                            floor_name: $("#txtfloor_name").val(),
                            floormanager_id: $("#txtfloormanager_id option:selected").val(),
                            floormanager_name: $("#txtfloormanager_id option:selected").text(),
                            floor_workstations: $("#txtfloor_workstations").val()                            
                        },
                        success: function(data) {
                            var result = data.split("|");
                            if (result[0] === "success") {
                                toastr.success(result[1], ' New Floor Added');
                                location.reload();
                            }
                        }
                    });
                }
            }
            
            
            function openupdate(id_business_floors){
                $("#txteditfloormanager_id").eq(1).prop('selected', true);
                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url(); ?>floor_controller/get_a_floor',
                    data: {floor_id: id_business_floors},
                    dataType: "json",
                    cache: false,
                    async: true,
                    success: function(data){
                        //console.log(data);
                        for(x=0; x<data.length;x++){
                            //console.log(data[x]);
                            $("#txteditbusiness_floor_id").val(data[x].id_business_floors);
                            $("#txteditfloor_name").val(data[x].floor_name);
                            $("#txteditfloormanager_id").val(data[x].floormanager_id);
                            $("#txteditfloor_workstation").val(data[x].floor_workstations);
                        }
                        $("#editbusinessfloor").modal('show');
                        
                    }
                });
            }
            function update(){
                if($("#txteditbusiness_floor_id").val()!== ""){
                $.ajax({
                        type: 'POST',
                        url: '<?php echo base_url(); ?>floor_controller/update_floor',
                        data: {
                            id_business_floors: $("#txteditbusiness_floor_id").val(), 
                            floor_name: $("#txteditfloor_name").val(), 
                            floormanager_id: $("#txteditfloormanager_id :selected").val(),
                            floormanager_name: $("#txteditfloormanager_id :selected").text(),
                            floor_workstations: $("#txteditfloor_workstation").val()
                        },
                        success: function(data) {
                            var result = data.split("|");
                            if (result[0] === "success") {
                                toastr.success(result[1], ' Floor Updated');
                                location.reload();
                            }
                        }
                    });
                }
            }
            
            function upload_image(id_service_type, image) {
                image = image || null;
                if (id_service_type && id_service_type != null) {
                    $("#org_image").val('');
                    $("#id_service_type_image").val('');
                    $("#id_service_type_image").val(id_service_type);
                    if (image && image != null) {
                        $("#org_image").val(image);
                        $("#msg #img").attr('src', 'assets/images/servicetype/' + image);
                    } else {
                        $("#msg #img").attr('src', 'assets/images/category/nu.jpg');
                    }
                    $("#addtypeimage").modal("show");
                }
            }
            

        </script>