<?php 
//echo '<pre>';
//print_r($_COOKIE);
//die('okk');
?>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
            <?php if ($permissions->hasPermission('add_region') ) { ?>
            <div class="col-xs-12 p-0 m-b-10">  
                <div class="floatingContainer ">
                    <a href="" id="add_region" data-target="#region_modal" data-toggle="modal" >
                        <div class="actionButton bg-info-light" >
                            <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('add_region') ?></span></p>
                        </div>
                    </a>
                </div>
            </div>
            <?php } ?>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered setting-table" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('region_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="region_modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('add_region'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_region'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="region_form" method="POST" onsubmit="return false">
                    <input type="hidden" id="region_id" name="region_id"/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('region_alert'); ?>
                    </div>
                    <div class="row  m-t-10">
                        <div class="col-md-12 form-group">
                            <label><?php print CommonLanguage::getMessage('region_name'); ?></label>
                            <input class="form-control empty" id="region_name" name="region_name" type="text" required autocomplete="off" />
                        </div>
                    </div> 
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn bg-info-light text-white" id="submit_region"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('region_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function(){
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'SettingsAjax/getRegions');
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {region_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'SettingsAjax/getRegions');
            }, "SettingsAjax/deleteRegion");
        });
        $("#add_region").on("click", function () {
            $('#region_id').val(0)
            $("#region_modal .add-label").removeClass('hidden');
        });

        $("#tableWithSearch").on("click",'.edit', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            $('#region_id').val(entry_value)
            $('#region_name').val($('td:eq(0)',$(this).parents('tr')).html())
            $("#region_modal .edit-label").removeClass('hidden');
        });
        $("#submit_region").on("click", function () {
            if (!$("#region_form").valid())
                return;
            var Data = new FormData(document.getElementById("region_form"));
            $("#region_loader").removeClass('hidden');
            $.ajax({
                url: "SettingsAjax/submitRegion",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'SettingsAjax/getRegions');
                            hideModalAlert('region_modal', 'region_alert');
                        }
                        AddAlert("#region_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#region_loader").addClass('hidden');
                }
            });
        });
    });
</script>