<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_display')) {
    echo Forms::FloatingAddButton('4c0870', '#display_modal', CommonLanguage::getMessage('add'), 'add_display'); } ?>
            <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('display_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('display_blocks') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="display_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="add-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('add_display'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('edit_display'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="display_form" method="POST" onsubmit="return false">
                    <div class="row">
                        <?php echo Forms::AlertDiv('display_alert'); ?>
                        <input type='hidden' name='display_id' id='display_id'/>
                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('display_name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('display_name'); ?>" id="display_name" name="display_name" class="form-control " autocomplete="off" required>
                        </div>
                    </div> 
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('display_blocks'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('display_blocks'); ?>" id="display_blocks" name="display_blocks" class="form-control " autocomplete="off" required>
                        </div>
                    </div> 
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('Printers'); ?></label>
                            <input type="hidden" id="printer_ids" name="printer_ids" class="full-width" autocomplete="off" >
                        </div>
                    </div>
                    <div class="row form-group m-t-10">
                         <div class="col-md-6">
                            <input type="checkbox" id="hide_done_order" name="hidedoneorder">
                            <label><?php print CommonLanguage::getMessage('hide_done_order'); ?></label>
                        </div>
                        <div class="col-md-6">
                            <input type="checkbox" id="hide_done_details" name="hidedonedetails">
                            <label><?php print CommonLanguage::getMessage('hide_done_details'); ?></label>
                        </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_display"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('display_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#printer_ids"), "xhr/CommonSearch?col_id=printer_id&col_name=printer_name&table=tbl_printers",1);
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/display_list');
        $(".add_display").on("click", function () {
            $('#display_modal .add-label').removeClass('hidden')
            $("#display_id").val(0);
            $("#hide_done_order,#hide_done_details").prop('checked',false);
        });
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {display_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/display_list');
            }, "InventoryAjax/deleteDisplay");
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            $('#display_modal .edit-label').removeClass('hidden')
            var display_id = $(this).parents('div.Action').attr('data-id');
            $("#display_id").val(display_id);
            CommonViewById('InventoryAjax/getDisplay', {display_id:display_id},
                           '#display_loader', function (response) {
                var main = response['main'],details= response['details'];
                $("#display_name").val(main.display_name);
                $("#display_blocks").val(main.display_blocks);
                $("#hide_done_order").prop('checked',main.hide_done_order == 1 ? true : false);
                $("#hide_done_details").prop('checked',main.hide_done_details == 1 ? true : false);
                if(details.length > 0){
                    $('#printer_ids').select2('val', []);
                    $('#printer_ids').data().select2.updateSelection(details);
                }
            });

        });
        $("#submit_display").on("click", function () {
            if (!$("#display_form").valid())
                return;
            $("#display_loader").removeClass('hidden');
            var Data = new FormData(document.getElementById("display_form"));
            Data.append('hide_done_order',$("#hide_done_order").is(":checked") ? 1 : 0)
            Data.append('hide_done_details',$("#hide_done_details").is(":checked") ? 1 : 0)
            $.ajax({
                url: "InventoryAjax/submitDisplay",
                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/display_list',1);
                            hideModalAlert('display_modal', 'display_alert');
                        }
                        AddAlert("#display_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#display_loader").addClass('hidden');
                }
            });
        });
    });
</script>