<?php $__env->startSection('navBar'); ?>
    <?php echo $__env->make('layouts.navBar', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('mainMenu'); ?>
    <?php echo $__env->make('layouts.mainMenu', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <div id="demo-settings" class="col-xs-12">
        <a id="demo-settings-toggler" class="fa fa-pencil"></a>
        <h5 class="header">Building Profile</h5>

        <div class="row" id="editPanel">
        </div>
    </div>

    <div class="panel-group" id="accordion-Building">
        <a class="btn-primary btn update-geolocations">
            Update Geolocation (Empty buildings)
        </a>
        <br>
        <div class="panel">
            <div class="panel-heading">
                <a class="accordion-toggle btn" data-toggle="collapse" data-parent="#accordion-Building"
                   href="#accordionAddBuilding">
                    Add New Building
                </a>
            </div>
            <!-- / .panel-heading -->
            <div id="accordionAddBuilding" class="panel-collapse collapse">
                <div class="panel-body">
                    <div id="formAddBuilding"></div>
                </div>
                <!-- / .panel-body -->
            </div>
            <!-- / .collapse -->
        </div>
        <!-- / .panel -->

        <div class="panel">
            <div class="panel-heading">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion-Building"
                   href="#panelBuildingList">
                    Building List
                </a>
            </div>
            <!-- / .panel-heading -->
            <div id="panelBuildingList" class="panel-collapse in">
                <div class="panel-body no-padding panelOverFlow" id="buildingList">
                    <div id="delete-confirmation" class="modal modal-alert modal-danger fade">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <i class="fa fa-times-circle"></i>
                                </div>
                                <div class="modal-title" id="confirmDeletionTitle"></div>
                                <div class="modal-body">Confirm to delete this building</div>
                                <div class="modal-footer">
                                    <a id="confirmDeletionLink"
                                       class="btn btn-danger" data-dismiss="modal">Confirm Deletion</a>
                                </div>
                            </div>
                            <!-- / .modal-content -->
                        </div>
                        <!-- / .modal-dialog -->
                    </div> <!-- / .modal -->

                    <div class="panel-body no-padding">
                        <table cellpadding="0" cellspacing="0" border="0" class="table table-striped"
                               id="buildingsTable">
                            <thead>
                            <tr>
                                <th><i class="fa fa-pencil"></i></th>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Building number</th>
                                <th>Address</th>
                                <th>Contact</th>
                                <th>Client</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <!-- / .panel-body -->
            </div>
            <!-- / .collapse -->
        </div>

        <div class="panel">
            <div class="panel-heading">
                <a id="aClientInfo" class="accordion-toggle" data-toggle="collapse" data-parent="#accordion-Building"
                   href="#panelClientInfo">
                    Client Info
                </a>
            </div>
            <!-- / .panel-heading -->
            <div id="panelClientInfo" class="panel-collapse collapse">
                <div class="panel-body" id="clientInfo">
                </div>
                <!-- / .panel-body -->
            </div>
            <!-- / .collapse -->
        </div>

    </div> <!-- / .panel-group -->

    <script type="text/javascript">

/*
        generateBuildingList('<?php echo e(URL::action("BuildingController@generateBuildingList")); ?>', '<?php echo e(csrf_token()); ?>');
*/

    </script>

    <script type="text/javascript">
        var $_token = "<?php echo e(csrf_token()); ?>",
            buildingTable;

        generateFormAddBuilding('<?php echo e(URL::action("BuildingController@generateFormAddBuilding")); ?>', '<?php echo e(csrf_token()); ?>');

        $(document).on('click', '.delete-building', function(){
            confirmDeletionInitPost($(this).data("url"), $(this).data("id"), $(this).data("name"), $_token, refreshBuildings)
        });
        $(document).on('click', '.edit-building', function(){
            generateBuildingDetail($(this).data("url"), $(this).data("id"), $_token)
        });
        $(document).on('click', '.edit-client', function(){
            generateClientInfo($(this).data("url"), $(this).data("id"), $_token)
        });
        $(document).on('click', '.update-geolocations', function(){
            var ahref = $(this);
            ahref.text('loading');
            loadingOverlay();
            $.post('<?php echo e(URL::action("BuildingController@updateBuildingsGeolocation")); ?>', {_token: $_token})
                    .done(function (data) {
                        ahref.text('Update Geolocation (Empty buildings)');
                        loadingRemove();
                        new PNotify({
                            title: 'Success',
                            text: "Buildings' geolocation updated successfully!",
                            type: 'success',
                            icon: 'fa fa-check'
                        });
                    })
                    .fail(function (data) {
                        ahref.text('Update Geolocation (Empty buildings)');
                        loadingRemove();
                        new PNotify({
                            title: 'Fail',
                            text: "Buildings' geolocation updating failed, try later again!",
                            type: 'error',
                            icon: 'fa fa-times'
                        });
                    });
        });
        buildingTable = $('#buildingsTable').dataTable(
        {
            processing: true,
            serverSide: true,
            bProcessing: true,
            order: [[ 1, "desc" ]],
            ajax: {
                "url": "<?php echo e(URL::action("BuildingController@generateBuildingList")); ?>",
                "type": "POST",
                "data": function (data) {
                    data._token = $_token;
                }
            },
            columns: [
                {data: 'edit', name: 'edit', orderable: false, searchable: false},
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'buildingIdentifier', name: 'buildingIdentifier'},
                {data: 'address', name: "address"},
                {data: 'contact', name: 'telephone', orderable: false},
                {data: 'client', name: 'client'}
            ],
            iDisplayLength: 25,
            drawCallback: function( settings ) {
                $('#buildingsTable').find('.geolocation-missing').closest('tr').addClass('danger');
            }
        });

        $('#buildingsTable_wrapper .table-caption').text('Buildings');
        $('#buildingsTable_wrapper .dataTables_filter input').attr('placeholder', 'Search...');


        function refreshBuildings() {
            buildingTable.api().ajax.reload();
        }
    </script>

    <script async defer
            src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAJrl9z0cjCdIfS3W47RnaaabD50bBkwG8">
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>