<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('scale') ?></div>
            <?php
            echo Forms::ModalLoader('DeleteModalIsBusy')
            . Forms::FloatingAddButton('4c0870', '#EditScale', CommonLanguage::getMessage('add'));
            ?> 
        </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('Name'); ?></th>
                        <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('ip'); ?></th>
                        <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('status'); ?></th>
                        <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('pricing_class'); ?></th>
                        <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('type'); ?></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="EditScale" 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" onsubmit="return false">
                    <div class="row form-group">
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('Name') ?> <span class="red-star">*</span></label>
                            <input type="text"  id="scale_name" name="scale_name" class="form-control " required>
                        </div>
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('ip') ?> <span class="red-star">*</span></label>
                            <input type="text"  id="scale_ip" name="scale_ip" class="form-control " required>
                        </div>
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('pricing_class') ?> <span class="red-star">*</span></label>
                            <input required id="Class_ID" name="Class_ID" class="full-width Class_ID" type="hidden"/>
                        </div>
                        <div class="col-md-6">
                            <label><?php echo CommonLanguage::getMessage('type') ?> <span class="red-star">*</span></label>
                            <select id="scale_type" class="full-width " data-init-plugin="select2" >
                                <option value=""><?php echo CommonLanguage::getMessage('select') ?></option>
                                <?php foreach ($scale_types as $indx => $type) { ?>
                                    <option value="<?php echo $indx ?>"><?php echo $type ?></option>
                                <?php } ?>

                            </select>
                        </div>
                        <div class="col-md-12">
                            <label><?php echo CommonLanguage::getMessage('scale_properties') ?> </label>
                            <textarea class="form-control " id="scale_properties"  name="scale_properties" style="overflow: hidden; word-wrap: break-word; resize:vertical "></textarea>

                        </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 () {
        build_select2($(".Class_ID"), "xhr/PricingClassSearch")
        RebuildDataTable(title, [0, 1], 'InventoryAjax/scaleList', 1);
        $('#tableWithSearch').on('click', '.Action .Delete', function () {
            $("#DeleteFromList").modal("show")
            ToDelete = $(this).parents("tr").find(".Action").attr('data-id');
        })
        $('#tableWithSearch').on('click', '.Action .reset', function () {
            ToReset = $(this).parents("tr").find(".Action").attr('data-id');
            ipToReset = $(this).parents("tr").find("span.my-ip").attr("data-status");
        })
        $("#ConfirmDelete").on("click", function () {
            $("#DeleteModalIsBusy").removeClass('hidden')
            var dataToDelete = {
                scale_id: ToDelete
            };
            $.ajax({
                type: 'POST',
                url: 'InventoryAjax/deleteScale',
                data: dataToDelete,
                complete: function (data, vv) {
                    if (vv == "success") {
                        if (data.responseText == 'OK') {
                            $("#Delete_modalAlert").addClass("alert alert-success").show().html(Submitted_Successfully);
                            RebuildDataTable(title, [0, 1], 'InventoryAjax/scaleList', 1);
                            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')
                }});
        })
        $("#ConfirmReset").on("click", function () {
            $("#ResetModalIsBusy").removeClass('hidden')
            var dataToDelete = {
                scale_id: ToReset,
                scale_ip: ipToReset
            };
            $.ajax({
                type: 'POST',
                url: 'InventoryAjax/resetScale',
                data: dataToDelete,
                complete: function (data, vv) {
                    if (vv == "success") {
                        if (data.responseText == 'OK') {
                            $("#Reset_modalAlert").addClass("alert alert-success").show().html(Submitted_Successfully);
                            RebuildDataTable(title, [0, 1], 'InventoryAjax/scaleList', 1);
                            setTimeout(function () {
                                $("#ResetScaleModal").modal("hide")
                                $("#Reset_modalAlert").hide()
                            }, 1000)
                        } else {
                            $("#Reset_modalAlert").addClass("alert alert-danger").show().html(data.responseText);
                        }
                    } else {
                        $("#Reset_modalAlert").addClass("alert alert-danger").show().html(data.responseText);
                    }
                    $("#ResetModalIsBusy").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_scale_details',
                data: {id: TRtoEdit},
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        var data = v1.responseJSON;
                        $('#scale_name').val(data['scale_name'])
                        $('#scale_ip').val(data['scale_ip'])
                        $("#Class_ID").val(data['class_id'])
                        $("#Class_ID").attr("data-select-name", data['Class_Name'])
                        $("#scale_type").val(data['scale_type']).select2()
                        $("#scale_properties").val(data['scale_properties'])
                        build_select2($(".Class_ID"), "xhr/PricingClassSearch")
                        $("#EditScale").modal("show")
                    }
                }
            })
//            scale_name = $(this).parents("tr").find('td:eq(0)').html();
//            scale_ip = $(this).parents("tr").find('td:eq(1)').html();
//            scale_class_id = $(this).parents("tr").find(".scale-class").attr("data-class");
//            scale_class_name = $(this).parents("tr").find(".scale-class").attr("data-class-name");
//            scale_type = $(this).parents("tr").find(".scale-class").attr("data-scale_type");
//            TRtoEdit = $(this).parents('tr').find('.Action').attr('data-id');
//            IfEdit = true;

        })
        $("#SubmitEntry").on("click", function () {
            if ($("#scale_name").val() == '' || $("#scale_ip").val() == '' || $("#scale_type").val() == '') {
                $("#EditScaleAlert").attr("class", "alert alert-danger").show().html(Required_Fields);
                return false;
            }
            $("#AjaxIsBusy").removeClass('hidden')
            var dataToEdit = {
                scale_name: $("#scale_name").val(),
                scale_ip: $("#scale_ip").val(),
                class_id: $("#Class_ID").val(),
                scale_type: $("#scale_type").val(),
                scale_properties: $("#scale_properties").val()
            }
            if (IfEdit) {
                dataToEdit.scale_id = TRtoEdit;
            }
            $.ajax({
                type: 'POST',
                url: IfEdit ? 'InventoryAjax/updateScale' : 'InventoryAjax/addScale',
                data: dataToEdit,
                complete: function (data, vv) {
                    if (vv == "success") {
                        if (data.responseText !== 'error') {
                            if (data.responseText == 'OK') {
                                $("#EditScaleAlert").attr("class", "alert alert-success").show().html(Submitted_Successfully);
                                RebuildDataTable(title, [0, 1], 'InventoryAjax/scaleList', 1);
                                setTimeout(function () {
                                    $("#EditScale").modal("hide")
                                    $("#EditScaleAlert").hide();
                                }, 1000)
                                $("#AjaxIsBusy").addClass('hidden')
                            }
                        } else {
                            $("#EditScaleAlert").attr("class", "alert alert-danger").show().html(Failed_Submit);
                        }
                    }
                }});
        })
        $('#tableWithSearch').on('click', '.Action .sync', function () {
            var sid = $(this).parents(".Action").attr("data-id");
            $("#scaleListLoader").removeClass('hidden');
            $.ajax({
                type: 'POST',
                url: 'InventoryAjax/syncScaleProducts',
                data: {sid: sid},
                complete: function (data, vv) {
                    if (vv == "success") {
                        $("#ScaleListAlert").attr("class", "alert alert-success").show().html(Submitted_Successfully);
                    } else {
                        $("#ScaleListAlert").attr("class", "alert alert-danger").show().html(Failed_Submit);
                    }
                    $("#scaleListLoader").addClass('hidden');
                }
            })
        })
    })
</script>
