
<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('Description_Points') ?></div>
            <div class="row">

                <?php if ($permissions->hasPermission('add_description_for_points')) {
                echo Forms::ModalLoader('DeleteModalIsBusy')
                    . Forms::FloatingAddButton('4c0870', '#Edit_desc_points', CommonLanguage::getMessage('add'));
                } ?>
            </div>
        </div>
        <div class="panel-body">
            <div id="ScaleListAlert"></div>
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch">
                        <thead>
                            <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('from_point'); ?></th>
                            <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('to_point'); ?></th>
                            <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('description'); ?></th>
                            <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Action'); ?></th>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <?php echo Forms::ModalLoader('scaleListLoader'); ?>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-md" data-backdrop="static" id="DeleteFromList" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" 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 bold"><?php echo CommonLanguage::getMessage('Confirm_Delete'); ?></h5>
                <div id="Delete_modalAlert"><span></span></div>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <p><?php print CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button type="button" class="btn btn-danger" id="ConfirmDelete" style="width:auto"><?php print CommonLanguage::getMessage('delete'); ?></button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade stick-up " id="Edit_desc_points" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog " style="width:60%;margin:0 auto">
        <div class="modal-content">
            <div class="modal-header clearfix 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 bold"><?php echo CommonLanguage::getMessage('add'); ?></h5>
                <div id="EditScaleAlert"></div>
            </div>
            <div class="modal-body">
                <form id="EditScalePostForm" method="POST" action="javascript:void(0);">
                    <input type="hidden" id="id" name="id" />

                    <div class="row form-group">
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('from_point') ?> <span class="red-star">*</span></label>
                            <input required id="from_point" name="from_point" class="form-control from_point" type="text" />
                        </div>
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('to_point') ?> <span class="red-star">*</span></label>
                            <input required id="to_point" name="to_point" class="form-control to_point" type="text" />
                        </div>
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('description') ?> <span class="red-star">*</span></label>
                            <input required type="text" id="description" name="description" class="form-control" >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" id="CloseEdit" data-dismiss="modal"> <?php print CommonLanguage::getMessage('Close'); ?> </button>
                <button class="btn btn-primary" id="SubmitEntry"> <?php print CommonLanguage::getMessage('Save'); ?> </button>
            </div>
            <?php echo Forms::ModalLoader('AjaxIsBusy') ?>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-md" id="ResetScaleModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" 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 bold"><?php echo CommonLanguage::getMessage('confirm_reset'); ?></h5>
                <div id="Reset_modalAlert"><span></span></div>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 ">
                        <p><?php print CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button type="button" class="btn btn-danger" id="ConfirmReset" style="width:auto"><?php print CommonLanguage::getMessage('reset'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('ResetModalIsBusy') ?>
        </div>
    </div>
</div>
<script>
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
        Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
        Data_Exists = <?php echo json_encode(CommonLanguage::getMessage('Data_Exists')); ?>,
        AddLang = <?php echo json_encode(CommonLanguage::getMessage('add')); ?>,
        EditLang = <?php echo json_encode(CommonLanguage::getMessage('Edit')); ?>,
        title = <?php echo json_encode(CommonLanguage::getMessage('general_setting')); ?>,
        Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
    var ToDelete, TRtoEdit, ToReset, ipToReset, IfEdit = false;
    $(function() {
        ChangeTable();

        $('#tableWithSearch').on('click', '.Action .Delete', function() {
            $("#DeleteFromList").modal("show")
            ToDelete = $(this).parents("tr").find(".Action").attr('data-id');
        })
        $("#ConfirmDelete").on("click", function() {
            $("#DeleteModalIsBusy").removeClass('hidden')
            var dataToDelete = {
                id: ToDelete
            };
            $.ajax({
                type: 'POST',
                url: 'InventoryAjax/delete_description_points',
                data: dataToDelete,
                complete: function(data, vv) {
                    if (vv == "success") {
                        if (data.responseText == 'OK') {
                            $("#Delete_modalAlert").addClass("alert alert-success").show().html(Submitted_Successfully);
                            ChangeTable()
                            setTimeout(function() {
                                $("#DeleteFromList").modal("hide")
                                $("#Delete_modalAlert").hide()
                            }, 1000)
                        } else {
                            $("#Delete_modalAlert").addClass("alert alert-danger").show().html(Failed_Submit);
                        }
                    } else {
                        $("#Delete_modalAlert").addClass("alert alert-danger").show().html(Failed_Submit);
                    }
                    $("#DeleteModalIsBusy").addClass('hidden')
                }
            });
        })
        $('.actionButton ').on("click", function() {
            IfEdit = false;
        })
        $('#tableWithSearch').on('click', '.Action .Edit', function() {
            IfEdit = true;
            TRtoEdit = $(this).parents('tr').find('.Action').attr('data-id');
            $.ajax({
                type: 'GET',
                url: 'InventoryAjax/get_point_description',
                data: {
                    id: TRtoEdit
                },
                dataType: 'json',
                complete: function(v1, v2) {
                    if (v2 == "success") {
                        var data = v1.responseJSON;

                        $("#id").val(data['id'])
                        $("#from_point").val(data['first_point'])
                        $('#to_point').val(data['last_point'])
                        $('#description').val(data['description'])
                        $("#Edit_desc_points").modal("show")
                    }
                }
            })

        })
        $("#SubmitEntry").on("click", function() {
            if ($("#from_point").val() == '' || $("#to_point").val() == '' || $("#description").val() == '') {
                $("#EditScaleAlert").attr("class", "alert alert-danger").show().html(Required_Fields);
                return false;
            }
            $("#AjaxIsBusy").removeClass('hidden')

            var Data = new FormData(document.getElementById("EditScalePostForm"));
            if (IfEdit) {
                Data.append('id', TRtoEdit);
            }

            $.ajax({
                url: IfEdit ? 'InventoryAjax/update_description_points' : 'InventoryAjax/add_descriptoin_points',
                method: 'POST',
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function(data_ajax, vv) {
                    console.log('data_ajax')
                    console.log(data_ajax)
                    var data = data_ajax.responseJSON;
                    if (data[1] == "success") {
                        $("#EditScaleAlert").attr("class", "alert alert-success").show().html(data[0]);
                        ChangeTable()
                        setTimeout(function() {
                            $("#Edit_desc_points").modal("hide")
                            $("#EditScaleAlert").hide();
                        }, 1000)
                        $("#AjaxIsBusy").addClass('hidden')

                    } else {
                        $("#EditScaleAlert").attr("class", "alert alert-danger").show().html(data[0]);
                    }


                }
            });
        })
    })

    function ChangeTable() {

        RebuildDataTable(title, [0, 1], 'InventoryAjax/description_point_list');

    }

</script>