
function GetMOS(m) {
    if (m == 1) {
        $("#MOS").removeAttr("hidden");
        $("#LocalAddress").attr("hidden","hidden");

    }
    else {
        $("#MOS").attr("hidden", "hidden");
        $("#LocalAddress").removeAttr("hidden");

    }
}

function GetNetAmount() {

    var unitPrice = IsNullZeroAsInt($("#UnitPrice").val());
    var qty = IsNullZeroAsInt($("#Qty").val());
    var localDeliveryCharges = IsNullZeroAsInt($("#LocalDeliveryCharges").val());
    var estimatedDeliveryCharges = IsNullZeroAsInt($('#EstimatedDeliveryCharges').val());
    var exchangeRate = IsNullZeroAsFloat($('#txtExchangeRate').val());

    var netTotal = (unitPrice * qty) + estimatedDeliveryCharges;
    var localNetTotal = netTotal * exchangeRate;
    var total = localDeliveryCharges + localNetTotal;
    $('#TotalAmount').val(total.toFixed(2));
       
}




function Submitform() {
    if ($("#TotalAmount").val() == "0") {
        MyAlert("Error", "Total Cannot be Zero", "error");
    }
    else {
        var form = $('#myform').get(0);
        $.validator.unobtrusive.parse(form);
        if ($(form).valid({ ignore: ":hidden,.tt-input" })) {

            var TotalAmount = $("#TotalAmount").val();
            var bal;
            $.ajax({
                url: "/Dashboard/GetUserBalance",
                method: "GET",
                success: function (response) {
                    if (response.success) {
                        bal = response.balance;
                        if (bal <= 0) {
                            MyAlert("Sorry!", "You have low Balance. <br>Please Topup in your Wallet", 'error');
                            return;
                        }
                        else if ((bal - TotalAmount) < 0) {
                            MyAlert("Sorry!", "You have low Balance. <br>Please Topup in your Wallet", 'error');
                            return;
                        }
                        else {
                            $("#myform").submit();
                        }
                    }
                    else {
                        alert(response.message);
                    }
                }
            });
        } 
    }
       
}

var arr = [];
$("input").click(function () {

    var checked = $(this).val();


    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");
    }


});
function VarifyCancellation() {
    $("#ModalComments").modal("show");
}
function CancelRequest() {
    var comments = $("#Comments").val();
    data = { Comments: comments, Ids: arr };
    $.ajax({
        url: "/PurchaseRequest/CancelRequest",
        method: "POST",
        data: data,
        success: function (response) {
            if (response.success) {
                MyAlert(response.message);
                location.reload();
            }
            else {
                MyAlert(response.message);

            }
        },
        error: function (err) {
            alert(err);
        }
    });
}

function ApproveRequestBulk() {
    data = { Comments: "a", Ids: arr };
    $.ajax({
        url: "/PurchaseRequest/BulkApprovePurchaseOrder/",
        method: "POST",
        data: data,
        success: function (response) {
            if (response.success) {
                MyAlert(response.message);
                location.reload();
            }
            else {
                MyAlert("Error", response.message);
            }
        },
        error: function (err) {
            MyAlert(err);
        }
    });
}


function GetDetail(Id) {
    $("#DetailBody").load("/PurchaseRequest/Detail/" + Id, function () {
        $("#DetailModal").modal("show");
    });
}


$("#EditBtn").on("click", function () {
    var id = $("#DetailId").val();
    window.open("/PurchaseRequest/Edit/" + id);
});
//CancelBtn
$("#CancelBtn").on("click", function () {
    var id = $("#DetailId").val();
    arr.push(id);
    data = { Comments: " ", Ids: arr };
    $.ajax({
        url: "/PurchaseRequest/CancelRequest",
        method: "POST",
        data: data,
        success: function (response) {
            if (response.success) {
                MyAlert("Cancelled",response.message);
                location.reload();
            }
            else {
                MyAlert(response.message);

            }
        },
        error: function (err) {
            alert(err);
        }
    });
});


//ApproveBtn


$("#ApproveBtn").on("click", function () {
    var id = $("#DetailId").val();   
    
    $.ajax({
        url: "/PurchaseRequest/ApprovePurchaseOrder/" + id,
        method: "GET",       
        success: function (response) {
            if (response.success) {
                MyAlert("Success", response.message, "success");
                location.reload();
            }
            else {
                MyAlert("Error", response.message);
            }
        },
        error: function (err) {
            MyAlert(err);
        }
    });
   
});