<script src="<?php echo THEME_PATH; ?>plugins/jquery-validation/js/jquery.validate.min.js"></script>
<div class="modal fade" id="table_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="text-left bold"><?php echo CommonLanguage::getMessage('table'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('table_alert'); ?></div>
                <form id="table_form" method="POST" onsubmit="return false">

                    <input type='hidden' name='table_flag' id='table_flag' />
                    <input type='hidden' name='table_id' id='table_id' />
                    <div class="row form-group">
                        <div class=" col-md-12  ">
                            <label class="labelText"><?php print CommonLanguage::getMessage('table_name'); ?><span class="red-star">*</span></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('table_name'); ?>" id="table_name" name="table_name" class="form-control " required />
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class=" col-md-12  ">
                            <label class="labelText"><?php print CommonLanguage::getMessage('table_alias_name'); ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('table_alias_name'); ?>" id="table_alias_name" name="table_alias_name" class="form-control " />
                        </div>
                    </div>
                    <div class="row form-group <?php echo smc_session()['default_setting']['enable_user_floors'] == 1 ? '' : 'hidden'; ?>">
                        <div class=" col-md-12  ">
                            <label class="labelText"><?php print CommonLanguage::getMessage('users'); ?></label>
                            <input id="employee_id" type="hidden" class="full-width" name="employee_id" value='' data-select-name=''>
                        </div>
                    </div>
                    <div class="row form-group">

                        <div class="col-md-1 ">
                            <div><label><?php print CommonLanguage::getMessage('radius'); ?></label></div>
                            <input type="checkbox" id="is_radius" name="is_radius" />
                        </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 btn-primary" id="submit_table"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('tableDisabler'); ?>
        </div>
    </div>
</div>
<script>
    $(function() {
        build_select2($("#employee_id"), "xhr/employeeSearch", 1);
        new Switchery($("#is_radius").get(0), {
            color: $.Pages.getColor("primary")
        });

        $(".new_table_btn").on("click", function() {
            $("#table_flag").val(0);
        });
        $("#tableWithSearch").on("click", ".edit_table", function() {
            var table_id = $(this).parents('div.Action').attr('data-id');
            $("#table_flag").val(1);
            $("#table_id").val(table_id);
            $("#table_name").val($(this).parents('tr').find('td:eq(0)').html());
            $("#table_alias_name").val($(this).parents('tr').find('td:eq(1)').html());

            var employee_ids = $(this).attr('employee-id').split(",");
            var employee_names = $(this).attr('employee-name').split(",");
            var is_radius = $(this).attr('is-radius');
            console.log('is_radius')
            console.log(is_radius)
            if(is_radius == 1){
               
                if(!$("#is_radius").is(":checked")){
                    $("#is_radius").trigger("click")
                }
            }else{
                if( $("#is_radius").is(":checked")){
                    $("#is_radius").trigger("click")
                }

            }

            var grid = [];
            for (var i = 0; i < employee_ids.length; i++) {
                var name = employee_names[i];
                var id = employee_ids[i];
                grid[i] = {}; // must initialize the sub-object, otherwise will get 'undefined' errors
                grid[i]['id'] = {}; // must initialize the sub-object, otherwise will get 'undefined' errors
                grid[i]['text'] = {}; // must initialize the sub-object, otherwise will get 'undefined' errors
                grid[i]['id'] = id;
                grid[i]['text'] = name;

            }



            $('#employee_id').select2('val', []);
            $('#employee_id').data().select2.updateSelection(grid);

        });
        $("#submit_table").on("click", function() {
            if (!$("#table_form").valid())
                return;
            $("#tableDisabler").removeClass('hidden');
            var Data = new FormData(document.getElementById("table_form"));
            Data.append('floor_id', floor_id)
            Data.append('employee_id', $('#employee_id').val());
            Data.append('is_radius', $("#is_radius").is(":checked") ? 1 : 0);

            $.ajax({
                url: "RestaurantAjax/submittable",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function(jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable(title, [0], 'RestaurantAjax/tables_list', {
                                floor_id: floor_id
                            });
                            hideModalAlert('table_modal', 'table_alert');
                        }
                        AddAlert("#table_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#tableDisabler").addClass('hidden');
                }
            });
        });
    });
</script>
