<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php echo $title; ?></div>
            <input type='hidden' name='group_type' id='group_type' value="<?php echo $group_type;?>"/>
            <?php if ($permissions->hasPermission("add_{$group_type}_group")) {
            echo Forms::FloatingAddButton('4c0870', '#group_modal', CommonLanguage::getMessage('add'), 'new_group_btn'); }?>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('group_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="group_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 text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="bold add-label h-labels hidden"><?php echo CommonLanguage::getMessage('add_group'); ?></h5>
                <h5 class="bold edit-label h-labels hidden"><?php echo CommonLanguage::getMessage('edit_group'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('group_alert'); ?></div>
                <form id="group_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='group_id' id='group_id'/>
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <label class="labelText">
                                <?php print CommonLanguage::getMessage('group_name'); ?>
                                <span class="red-star">*</span>
                            </label>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('group_name'); ?>" id="group_name" name="group_name" class="form-control " required />
                        </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_group"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('group_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/group_list',{group_type:$('#group_type').val()});
        $('#tableWithSearch').on('click', '.delete', function () {
            var group_id = $(this).parents('div.Action').attr('data-id');
            IncludeDeleteModal(common_delete_modal, {group_id: group_id}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/group_list',{group_type:$('#group_type').val()});
            }, "InventoryAjax/deleteGroup");
        });
        $(".new_group_btn").on("click", function () {
            $("#group_id").val(0);
            $('#group_modal .add-label').removeClass('hidden');
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            var group_id = $(this).parents('div.Action').attr('data-id');
            $("#group_id").val(group_id);
            $('#group_modal .edit-label').removeClass('hidden');
            $("#group_name").val($(this).parents('tr').find('td:eq(0)').html());
        });
        $("#submit_group").on("click", function () {
            if (!$("#group_form").valid())
                return;
            $("#group_loader").removeClass('hidden');
            var Data = new FormData(document.getElementById("group_form"));
            Data.append('group_type',$('#group_type').val());
            $.ajax({
                url: "InventoryAjax/submitGroup",
                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], 'InventoryAjax/group_list',{group_type:$('#group_type').val()});
                            hideModalAlert('group_modal', 'group_alert');
                        }
                        AddAlert("#group_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#group_loader").addClass('hidden');
                }
            });
        });
    });

</script>