{% extends 'RxGrabBundle:Layouts:ajax_content.html.twig' %}

{% block content %}

    <div class="row">
        <div class="xxlarge-12 xlarge-12 large-12 medium-12 small-12 columns">
            <label>Available</label>
            <table class="display responsive" id="accountHealthFacilities-available-table-data-list" width="100%">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Company</th>
                    <th>Branch</th>
                    <th>Description</th>
                    <th>Mobile No</th>
                    <th>Phone No</th>
                    <th>Email</th>
                    <th class="text-center">Action</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

    <br/><br/>

    <div class="row">
        <div class="xxlarge-12 xlarge-12 large-12 medium-12 small-12 columns">
            <label>Linked</label>
            <table class="display responsive" id="accountHealthFacilities-linked-table-data-list" width="100%">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Company</th>
                    <th>Branch</th>
                    <th>Description</th>
                    <th>Mobile No</th>
                    <th>Phone No</th>
                    <th>Email</th>
                    <th class="text-center">Action</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

    <script language="javascript">
        //<![CDATA[
        jQ(document).ready(function(){

            var dt_callBackAccountHealthFacilitiesAvailable = function() {
                jQ('.rxgrab-accountHealthFacilities-form-add').unbind('click').bind('click', function(){

                    if(confirm('Are you sure you want to link this health facility?')) {

                        global.addContainerLoading('#accountHealthFacilities-container');

                        var data = {healthFacilityId: jQ(this).data('id'), accountId: {{ account.id}}},
                            settings = {
                                url     : '{{ path('rx_grab_account_add_health_facility') }}',
                                data    : data,
                                error: function() {
                                    global.removeContainerLoading('#accountHealthFacilities-container');
                                },
                                success : function(json) {
                                    global.removeContainerLoading('#accountHealthFacilities-container');
                                    if(json.success) {
                                        accountHealthFacilitiesAvailableDataList.draw();
                                        accountHealthFacilitiesLinkedDataList.draw();
                                    } else
                                        alert(json.message);
                                }
                            };
                        global.ajax(settings, 'normal');

                    }

                });
            };

            var accountHealthFacilitiesAvailableDataList = jQ('#accountHealthFacilities-available-table-data-list').DataTable({
                'jQueryUI': true,
                'processing': true,
                'serverSide': true,
                'ajax': {
                    'url': '{{ path('rx_grab_account_available_health_facility_ajax_list') }}'
                },
                'columnDefs': [
                    { 'orderable': false, 'targets': [4,5,6,7] },
                    { 'searchable': false, 'targets': [4,5,6,7] }
                ],
                drawCallback: function() {
                    dt_callBackAccountHealthFacilitiesAvailable();
                },
                responsive: {
                    details: {
                        renderer: function( api,rowIdx ) {
                            return global.dt_responsive_callBack(api, rowIdx, dt_callBackAccountHealthFacilitiesAvailable);
                        }
                    }
                }
            });

            dt_callBackAccountHealthFacilitiesAvailable();

            var dt_callBackAccountHealthFacilitiesLinked = function() {
                jQ('.rxgrab-accountHealthFacilities-form-remove').unbind('click').bind('click', function(){

                    if(confirm('Are you sure you want to remove this health facility?')) {

                        global.addContainerLoading('#accountHealthFacilities-container');

                        var data = {healthFacilityId: jQ(this).data('id')},
                            settings = {
                                url     : '{{ path('rx_grab_account_remove_health_facility') }}',
                                data    : data,
                                error: function() {
                                    global.removeContainerLoading('#accountHealthFacilities-container');
                                },
                                success : function(json) {
                                    global.removeContainerLoading('#accountHealthFacilities-container');
                                    if(json.success) {
                                        accountHealthFacilitiesAvailableDataList.draw();
                                        accountHealthFacilitiesLinkedDataList.draw();
                                    } else
                                        alert(json.message);
                                }
                            };
                        global.ajax(settings, 'normal');

                    }
                });
            };

            var accountHealthFacilitiesLinkedDataList = jQ('#accountHealthFacilities-linked-table-data-list').DataTable({
                'jQueryUI': true,
                'processing': true,
                'serverSide': true,
                'ajax': {
                    'url': '{{ path('rx_grab_account_linked_health_facility_ajax_list') }}',
                    'data': function(d) {
                        d.accountId = {{ account.id }};
                    }
                },
                'columnDefs': [
                    { 'orderable': false, 'targets': [4,5,6,7] },
                    { 'searchable': false, 'targets': [4,5,6,7] }
                ],
                drawCallback: function() {
                    dt_callBackAccountHealthFacilitiesLinked();
                },
                responsive: {
                    details: {
                        renderer: function( api,rowIdx ) {
                            return global.dt_responsive_callBack(api, rowIdx, dt_callBackAccountHealthFacilitiesLinked);
                        }
                    }
                }
            });

            dt_callBackAccountHealthFacilitiesLinked();

        });
        //]]>
    </script>

{% endblock %}