
$(document).ready(function () {

    $('#ShipmentDetail_0__Length').on('keyup', function () {

        var row = 0;
        var length = $('input[name="ShipmentDetail[' + row + '].Length"]').val();
        var width = $('input[name="ShipmentDetail[' + row + '].Width"]').val();
        var height = $('input[name="ShipmentDetail[' + row + '].Height"]').val();
        var volweight = CalculateVolWeight(length, width, height);
        $('#TotalVolWeight').val(volweight);        
    });


    $('#ShipmentDetail_0__Width').on('keyup', function () {

        var row = 0;
        var length = $('input[name="ShipmentDetail[' + row + '].Length"]').val();
        var width = $('input[name="ShipmentDetail[' + row + '].Width"]').val();
        var height = $('input[name="ShipmentDetail[' + row + '].Height"]').val();
        var volweight = CalculateVolWeight(length, width, height);
        $('#TotalVolWeight').val(volweight);        

    });
    $('#ShipmentDetail_0__Height').on('keyup', function () {

        var row = 0;
        var length = $('input[name="ShipmentDetail[' + row + '].Length"]').val();
        var width = $('input[name="ShipmentDetail[' + row + '].Width"]').val();
        var height = $('input[name="ShipmentDetail[' + row + '].Height"]').val();
        var volweight = CalculateVolWeight(length, width, height);
        $('#TotalVolWeight').val(volweight);        
    });


    $('#dgvShipmentDetail tbody>tr:last #ShipmentDetail_0__Length').on('keyup', function () {

        var td = $(this).parent();
        var row = td.parent().index();
        var length = $('input[name="ShipmentDetail[' + row + '].Length"]').val();
        var width = $('input[name="ShipmentDetail[' + row + '].Width"]').val();
        var height = $('input[name="ShipmentDetail[' + row + '].Height"]').val();
        var volweight = CalculateWeight(length, width, height);
        $('input[name="ShipmentDetail[' + row + '].VolWeight"]').val(volweight);
        CalculateTotal();
    });


    $('#dgvShipmentDetail tbody>tr:last #ShipmentDetail_0__Width').on('keyup', function () {

        var td = $(this).parent();
        var row = td.parent().index();
        var length = $('input[name="ShipmentDetail[' + row + '].Length"]').val();
        var width = $('input[name="ShipmentDetail[' + row + '].Width"]').val();
        var height = $('input[name="ShipmentDetail[' + row + '].Height"]').val();
        var volweight = CalculateWeight(length, width, height);
        $('input[name="ShipmentDetail[' + row + '].VolWeight"]').val(volweight);
        CalculateTotal();

    });
    $('#dgvShipmentDetail tbody>tr:last #ShipmentDetail_0__Height').on('keyup', function () {

        var td = $(this).parent();
        var row = td.parent().index();
        var length = $('input[name="ShipmentDetail[' + row + '].Length"]').val();
        var width = $('input[name="ShipmentDetail[' + row + '].Width"]').val();
        var height = $('input[name="ShipmentDetail[' + row + '].Height"]').val();
        var volweight = CalculateWeight(length, width, height);
        $('input[name="ShipmentDetail[' + row + '].VolWeight"]').val(volweight);
        CalculateTotal();
    });

    $('#dgvShipmentDetail tbody>tr:last #ShipmentDetail_0__Weight').on('keyup', function () {
        CalculateTotal();
    });



    function CalculateVolWeight(length, width, height) {
        volWeight = 0;
        if (length != '' && width != '' && height != '') {
            volWeight = (parseInt(length) * parseInt(width) * parseInt(height)) / 5000;            
        }
        return volWeight.toFixed(1);
    }

    function CalculateWeight(length, width, height) {
        volWeight = 0;
        if (length != '' && width != '' && height != '') {
            volWeight = (parseInt(length) * parseInt(width) * parseInt(height)) / 5000;
            CalculateTotal();
        }
        return volWeight.toFixed(1);
    }

    function CalculateTotal() {
        var totalWeight = 0, volWeight = 0, chargeableWeight = 0;
        debugger;
        var $tblrows = $("#dgvShipmentDetail tbody tr");
        $tblrows.each(function (index) {
            var _totalweight = 0, _totalVolweight = 0;
            if ($.isNumeric($('input[name="ShipmentDetail[' + index + '].Weight"]').val())) {
                _totalweight = $('input[name="ShipmentDetail[' + index + '].Weight"]').val();
            }
            if ($.isNumeric($('input[name="ShipmentDetail[' + index + '].VolWeight"]').val())) {
                _totalVolweight = $('input[name="ShipmentDetail[' + index + '].VolWeight"]').val();

            }
            totalWeight += parseFloat(_totalweight);
            volWeight += parseFloat(_totalVolweight);
        });

        $('#TotalWeight').val(totalWeight);
        $('#TotalVolWeight').val(volWeight.toFixed(1));
        if (totalWeight > volWeight) {
            $('#TotalChargeableWeight').val(totalWeight);
        }
        else {
            $('#TotalChargeableWeight').val(volWeight);
        }

    }



    $('#contentFile').on('change', function (e) {
        var files = e.target.files;
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.docx|.pdf|.xls|.xlsx|.txt|.ppt)$/;
        
        if (files.length > 0) {
            if (window.FormData !== undefined) {
                var data = new FormData();
                for (var x = 0; x < files.length; x++) {
                    if (regex.test(files[x].name.toLowerCase())) {
                        data.append("file" + x, files[x]);
                    }
                }

                $.ajax({
                    type: "POST",
                    url: '/FileUpload/UploadDocument',
                    contentType: false,
                    processData: false,
                    data: data,
                    beforeSend: function () {
                        $('#alert-loader').show();
                    },
                    success: function (result) {
                        $('#alert-loader').hide();
                        $('.attachment').empty();
                        $('.attachment').append('<li><a href="/FileUpload/GetUploadedObject?fileId=' + result.id+'">' + result.name + '</a><a href="javascript:void(0);" data-id="' + result.id + '" class="deleteItem">X</a></li>');
                        $('#DeliveryDetail_PODUrl').val(result.id);
                        $('#DeliveryDetail_FileName').val(result.name);

                    },
                    error: function (xhr, status, p3, p4) {
                        $('#alert-loader').hide();
                        MyAlert('Warning!', "Error", 'danger', 'icon-ios-close-outline');
                        $('#DeliveryDetail_PODUrl').val('');
                        $('#DeliveryDetail_FileName').val('');

                    }
                });
            } else {
                alert("This browser doesn't support HTML5 file uploads!");
            }
        }
    });


    

    $('.attachment').on('click', 'li a.deleteItem', function (e) {
        e.preventDefault();
        var $ctrl = $(this);
        if (confirm('Do you really want to delete this file?')) {
            $.ajax({
                url: '/FileUpload/DeleteObject',
                type: 'POST',
                data: { id: $(this).data('id') }
            }).done(function (data) {
                if (data.success) {
                    $ctrl.closest('li').remove();
                    MyAlert('Good!', 'Deleted...!', 'success', 'icon-ios-close-outline')
                    $('#DeliveryDetail_PODUrl').val('');                
                }                
            }).fail(function () {
                alert("There is something wrong. Please try again.");
            })

        }
    });

    $('#imageFile').on('change', function (e) {
        var files = e.target.files;
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg|.jpeg)$/;
        var shipmentId = $('#ShipmentId').val();
        debugger;
        if (files.length > 0) {
            if (window.FormData !== undefined) {
                var data = new FormData();
                for (var x = 0; x < files.length; x++) {
                    if (regex.test(files[x].name.toLowerCase())) {
                        data.append("file" + x, files[x]);
                    }
                }

                $.ajax({
                    type: "POST",
                    url: '/Shipments/UploadImage?shipmentId=' + shipmentId,
                    contentType: false,
                    processData: false,
                    data: data,
                    beforeSend: function () {
                        $('#alert-loader').show();
                    },
                    success: function (result) {
                       $('#alert-loader').hide();
                        $('.imageattachment').append('<li><a href="/FileUpload/GetUploadedObject?fileId=' + result.id + '">' + result.name + '</a><a href="javascript:void(0);" data-id="' + result.id + '" class="deleteItem">X</a></li>');

                    },
                    error: function (xhr, status, p3, p4) {
                        $('#alert-loader').hide();
                        MyAlert('Warning!', status, 'danger', 'icon-ios-close-outline');



                    }
                });
            } else {
                alert("This browser doesn't support HTML5 file uploads!");
            }
        }
    });




    $('.imageattachment').on('click', 'li a.deleteItem', function (e) {
        e.preventDefault();
        var $ctrl = $(this);
        if (confirm('Do you really want to delete this file?')) {
            $.ajax({
                url: '/Shipments/DeleteImage',
                type: 'POST',
                data: { id: $(this).data('id') }
            }).done(function (data) {
                if (data.success) {
                    $ctrl.closest('li').remove();
                    MyAlert('Good!', 'Deleted...!', 'success', 'icon-ios-close-outline')
                    
                }
            }).fail(function () {
                alert("There is something wrong. Please try again.");
            })

        }
    });    

});
