$(document).ready(function () {
    $("#Same").on("change", function () {
        if ($(this).is(":checked")) {
            $("#ReceiverName").val($("#SenderName").val());
            $("#ReceiverCode").val($("#SenderCode").val());
            $("#ReceiverAddress").val($("#SenderAddress").val());
            $("#ReceiverPinCode").val($("#SenderPinCode").val());
            $("#ReceiverCity").val($("#SenderCity").val());
            $("#ReceiverState").val($("#SenderState").val());
            $("#ReceiverPhone").val($("#SenderPhone").val());

        }
        else {
            alert("false");
        }
    });
});



$("#Detail_0__EachCost").on("keyup", function () {
    debugger
    CalculateTotalCOD();

});

$("#Detail_0__Quantity").on("keyup", function () {
    debugger

    CalculateTotalCOD();

});

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

$('#dgvShipmentDetail tbody>tr:last #Detail_0__Quantity').on('keyup', function () {
    CalculateTotalCOD();
});
function CalculateTotalCOD() {
    var totalWeight = 0, volWeight = 0, chargeableWeight = 0;
    debugger;
    var $tblrows = $("#dgvShipmentDetail tbody tr");
    var _COD = 0;
    var eachCost=0, qty=0;
    $tblrows.each(function (index) {
        if ($.isNumeric($('input[name="Detail[' + index + '].EachCost"]').val())) {
            eachCost = $('input[name="Detail[' + index + '].EachCost"]').val();
        }
        if ($.isNumeric($('input[name="Detail[' + index + '].Quantity"]').val())) {
            qty = $('input[name="Detail[' + index + '].Quantity"]').val();
        }

        _COD = _COD + (eachCost * qty);
        eachCost = 0;
        qty = 0;
    });
    $("#CODAmount").val(_COD);


}

var arr = [];
$("input").click(function () {
    debugger
    var checked = $(this).val();
    if (checked == -1) {
        //if ($(this).is(':checked')) {
        //    // checkall();
        //}
    }
    else {
        if ($(this).is(':checked')) {
            arr.push(checked);
        } else {
            arr.splice($.inArray(checked, arr), 1);
        }
        if (arr.length > 0) {
            $("#SecondOptions").removeAttr("hidden");
            $("#MainOptions").attr("hidden", "hidden");
        }
        else {
            $("#SecondOptions").attr("hidden", "hidden");
            $("#MainOptions").removeAttr("hidden");
        }
    }
});
$("#Allcheck").click(function () {
    debugger
    if ($(this).is(':checked')) {
    //    debugger
    $('input:checkbox').prop('checked','checked').trigger('click');

    }
    else {
    //    debugger

                $('input:checkbox').removeProp('checked').trigger('click');
   }
});

function VarifyCancellation() {
    var result = confirm("Are You Sure Want to Cancel Orders");
    if (result) {
        debugger
        var data = { Ids: arr };
        $.ajax({
            url: "/Order/CancelOrder",
            method: "POST",
            data: data,
            success: function (response) {
                if (response.success) {
                    MyAlert("Cancelled", response.message, "success");
                    location.reload();
                }
                else {
                    MyAlert("Error", response.message, "error");
                }
            },
            error: function (err) {
                MyAlert(err);
            }
        });
    }
}


$("#BulkShip").on("click", function () {
    $("#ShipContent").load("/Order/ShipOrders", function () {
        $("#shipModal").modal("show");
    });

});
$("#shipModal").on("shown.bs.modal", function () {
    $.ajax({
        url: "/Order/GetPrimaryWareHouse",
        method: "GET",
        success: function (response) {
            if (response.success) {
                var pid = response.message;
                $("#RtoWareHouse").val(pid).trigger("change");
                $("#PickupWareHouse").val(pid).trigger("change");
            }
        },
        error: function (err) {

        }
    });
});



$("#ShipBtn").on("click", function () {
    var RTOWhouse = $("#RtoWareHouse").val();
    var PickupWhouse = $("#PickupWareHouse").val();
    var data = { Ids: arr, ServiceId: ServiceId, PickupWarehouse: PickupWhouse, RTOWarehouse: RTOWhouse };
    $.ajax({
        url: "/Order/ShipOrders",
        method: "POST",
        data: data,
        success: function (response) {
            if (response.success) {
                MyAlert("Success", response.message, "success");
                location.reload();
            }
            else {
                MyAlert("Error", response.message, "error");
            }
        },
        error: function (err) {
            MyAlert("Error", response.message, "error");

        }
    });
});
var ServiceId;
function GetServiceId(sid)//imp
{
    ServiceId = sid;
}
function SingleShip(id) {
    debugger
    arr.length = 0;
    arr.push(id);
    $("#ShipContent").load("/Order/ShipOrders", function () {
        $("#shipModal").modal("show");
    });
}