<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_job_order_block')) {
    echo Forms::FloatingAddButton('4c0870', '#job_order_block', CommonLanguage::getMessage('add'), 'add_job_order_block');} ?>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('block_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('sort') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="job_order_block" 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_job_order_block'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_job_order_block'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="job_order_block_form" method="POST" onsubmit="return false">
                    <div class="row">
                        <?php echo Forms::AlertDiv('job_order_block_alert'); ?>
                        <input type='hidden' name='block_id' id='block_id'/>
                    </div>
                    <div class="row form-group m-t-10">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('block_name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('block_name'); ?>" id="block_name" name="block_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('sort'); ?>
                            <span class="red-star">*</span></label>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('sort'); ?>" id="sort_nb" name="sort_nb" 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('job_order_options'); ?></label>
                            <span class="red-star">*</span>
                            <input type="hidden"  id="option_ids" name="option_ids" class="full-width " autocomplete="off" 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_job_order_block"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('job_order_block_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#option_ids"), "xhr/commonSearch?col_id=option_id&col_name=option_title&table=tbl_job_order_option",1);
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/job_order_block_list');
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {block_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'InventoryAjax/job_order_block_list');
            }, "InventoryAjax/deleteJobOrderBlock");
        });
        $(".add_job_order_block").on("click", function () {
            $('#job_order_block .add-label').removeClass('hidden');
            $("#block_id").val(0);
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            $('#job_order_block .edit-label').removeClass('hidden');
            var block_id = $(this).parents('div.Action').attr('data-id');
             $("#block_id").val(block_id);
            CommonViewById('InventoryAjax/getJobOrderBlockData', {block_id: block_id},
                           '#job_order_block_loader', function (response) {
                var main = response['main'],options = response['options'];
                $('#block_name').val(main.block_name);
                $('#sort_nb').val(main.sort_nb);
                $('#option_ids').select2('val', []);
                $('#option_ids').data().select2.updateSelection(options);
            });
        });
        $("#submit_job_order_block").on("click", function () {
            if (!$("#job_order_block_form").valid())
                return;
            $("#job_order_block_loader").removeClass('hidden');
            var Data = new FormData(document.getElementById("job_order_block_form"));
            $.ajax({
                url: "InventoryAjax/submitJobOrderBlock",
                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/job_order_block_list');
                            hideModalAlert('job_order_block', 'job_order_block_alert');
                        }
                        AddAlert("#job_order_block_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#job_order_block_loader").addClass('hidden');
                }
            });
        });
    });
</script>