<div class="modal fade" id="add-edit-line-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="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('add_line'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_line'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="line_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='line_id' id='line_id'/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('line_alert'); ?>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('line_name'); ?></label>
                            <input class="line_name form-control" id="line_name" name="line_name" type="text" required  />
                        </div>
                    </div> 
                    <div class="row form-group">
                        <div class="col-md-12 ">
                            <label><?php print CommonLanguage::getMessage('machines'); ?></label>
                            <span class="red-star">*</span>
                            <input class="full-width" id="Product_ID" name="Product_ID" type="hidden" required />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;"  class="btn bg-success-dark text-white" id="submit_line"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('lineLoader'); ?>
        </div>
    </div>
</div>
<script>
    $(function(){

        $("#tableWithSearch").on("click", ".edit_line", function () {
            var line_id = $(this).attr('data-id');
            $("#line_id").val(line_id);
            $("#add-edit-line-modal .edit-label").removeClass('hidden');
            build_select2($("input[name=Product_ID]"),"xhr/MachinePartSearch?type=1",1);
            CommonViewById('MaintenanceAjax/getLineData', {line_id: line_id},
                           '#lineLoader', function (response) {
                var line_main = response.line,line_machine = response['line_machine'];
                $('#line_name').val(line_main['line_name'])
                $("input[name=Product_ID]").select2('val', [])
                $("input[name=Product_ID]").data().select2.updateSelection(line_machine);

            });
        });
        $("#new_line").on("click", function () {
            $("#line_id").val(0);
            build_select2($("input[name=Product_ID]"),"xhr/MachinePartSearch?type=1&Status_ID=1",1);
            $("#add-edit-line-modal .add-label").removeClass('hidden');
        });
        $("#submit_line").on("click", function () {
            if (!$("#line_form").valid())
                return;
            var Data = new FormData(document.getElementById("line_form"));
            $("#lineLoader").removeClass('hidden');
            $.ajax({
                url: "MaintenanceAjax/submitLine",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            hideModalAlert('add-edit-line-modal', 'line_alert');
                              RebuildDataTable($('.panel-heading .panel-title').html(), [0,1], 'MaintenanceAjax/getLineList')
                        }
                        AddAlert("#line_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#lineLoader").addClass('hidden');
                }
            });
        });  
    });
</script>