<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('Printers'); ?></div>
            <?php if (smc_session()['default_setting']['sync_type'] != 2) {
            echo Forms::FloatingAddButton('4c0870', '#add_edit_printer_modal', CommonLanguage::getMessage('add'), 'add_printer'); } ?>
            <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('printer_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('printer_title') ?></th>
                            <th><?php print CommonLanguage::getMessage('printer_key') ?></th>
                            <th><?php print CommonLanguage::getMessage('branch') ?></th>
                            <th><?php print CommonLanguage::getMessage('is_default') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="add_edit_printer_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_printer'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('edit_printer'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="printer_form" method="POST" onsubmit="return false">
                    <div class="row">
                        <?php echo Forms::AlertDiv('printer_alert'); ?>
                        <input type='hidden' name='printer_id' id='printer_id'/>
                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('printer_name'); ?>&nbsp;<i class="fa fa-info-circle" title="<?php print CommonLanguage::getMessage('printer_name_system'); ?>"></i></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('printer_name'); ?>" id="printer_name" name="printer_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('printer_title'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('printer_title'); ?>" id="printer_title" name="printer_title" 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('printer_key'); ?></label>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('printer_key'); ?>" id="printer_key" name="printer_key" class="form-control " autocomplete="off">
                        </div>
                    </div> 
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('branch'); ?></label>
                            <input type="hidden" id="current_branch_id" name="branch_id" class="full-width" autocomplete="off" >
                        </div>
                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <div class="checkbox check-primary ">
                                <input type="checkbox" id="is_printer_default" name="is_printer_default">
                                <label for="is_printer_default"><?php print CommonLanguage::getMessage('is_default'); ?></label>
                            </div>
                        </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_printer"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('printerLoader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#current_branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches");
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/printer_list',1);
        $(".add_printer").on("click", function () {
            $('#is_printer_default').prop('checked',false)
            $('#add_edit_printer_modal .add-label').removeClass('hidden')
            $("#printer_id").val(0);
        });
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {printer_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/printer_list');
            }, "InventoryAjax/deletePrinter");
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            $('#add_edit_printer_modal .edit-label').removeClass('hidden')
            var printer_id = $(this).parents('div.Action').attr('data-id');
            $("#printer_id").val(printer_id);
            CommonViewById('common_ajax/ViewByEntryId', {table: 'printer_list',entry_id:'printer_id',entry_value:printer_id},
                           '#printerLoader', function (response) {
                var current_printer = response[0];
                $("input[name=is_printer_default]").prop('checked',current_printer.is_default == "1" ? true : false); 
                $("#printer_name").val(current_printer.printer_name);
                $("#printer_title").val(current_printer.printer_title);
                $("#printer_key").val(current_printer.printer_key);
                $("#current_branch_id").val(current_printer.branch_id).attr('data-select-name',current_printer.branch_name);
                build_select2($("#current_branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches");
            });

        });
        $("#submit_printer").on("click", function () {
            if (!$("#printer_form").valid())
                return;
            $("#printerLoader").removeClass('hidden');
            var Data = new FormData(document.getElementById("printer_form"));
            Data.append('is_default',$('#is_printer_default').is(':checked') ? 1 : 0)
            $.ajax({
                url: "InventoryAjax/submitPrinter",
                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/printer_list',1);
                            hideModalAlert('add_edit_printer_modal', 'printer_alert');
                        }
                        AddAlert("#printer_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#printerLoader").addClass('hidden');
                }
            });
        });
    });
</script>