$(document).ready(function() {
    checkSessionStat();
});
function checkSessionStat(){
    var status = sessionStorage.getItem("alert_status");
    var message = sessionStorage.getItem("alert_message");
    if(status == "success"){
        successAlert(message);
        sessionStorage.removeItem("alert_status");
        sessionStorage.removeItem("alert_message");
    } else if(status == "failed"){
        errorAlert(message);
        sessionStorage.removeItem("alert_status");
        sessionStorage.removeItem("alert_message");
    }
}

//START Detail Function
function checkStat(param){
    var result = "";

    if(param == "open"){
        result = "Open";
    } else if(param == "progress") {
        result = "In Progress";
    } else if(param == "prepared") {
        result = "Prepared";
    } else if(param == "complete") {
        result = "Complete";
    } else if(param == "cancel") {
        result = "Cancel";
    }

    return result;
}
function validateFormNotes(){
    var notes = $("#input-note").val();

    if(notes != ""){
        win_loader('open');
        $("#error-note").html("");
        $("#error-note").hide();
    } else {
        $("#error-note").show();
        $("#error-note").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-note").focus();
        return false;
    }
    return true;
};
function validateFormPONotes(){
    var notes = $("#input-ponote").val();

    if(notes != ""){
        win_loader('open');
        $("#error-ponote").html("");
        $("#error-ponote").hide();
    } else {
        $("#error-ponote").show();
        $("#error-ponote").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-ponote").focus();
        return false;
    }
    return true;
};
function validateInvoice(){
    var invoice = $("#input-invoice").val();

    if(invoice != ""){
        win_loader('open');
        $("#error-invoice").html("");
        $("#error-invoice").hide();
    } else {
        $("#error-invoice").show();
        $("#error-invoice").html("<i class='fa fa-warning'></i> This field is required.");
        $("#input-invoice").focus();
        return false;
    }
    return true;
};
//function confirmation change stat
function confirmStatusChange(stat){
    var url_page = $("#input-pageurl").val();
    var po_id = $("#po-id").val();
    var redir_url = url_page+"?action=changeStat&poid="+po_id+"&stat="+stat;
    swal({
        title: "Are you sure want to set this PO to "+checkStat(stat)+" ?",
        text: "Status purchase order will change to "+checkStat(stat)+" ",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#00c0ef",
        confirmButtonText: "Process ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.location.href = redir_url;
        } else {
            //nothing
        }
    });
}
function confirmCancel(){
    var url_page = $("#input-pageurl").val();
    var po_id = $("#po-id").val();
    var stat = "cancel";
    var redir_url = url_page+"?action=changeStat&poid="+po_id+"&stat="+stat;
    swal({
        title: "Are you sure to cancel this order?",
        text: "You will not be able to set open this order again!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Process ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.location.href = redir_url;
        } else {
            //nothing
        }
    });
}
function confirmPaid(param){
    var url_page = $("#input-pageurl").val();
    var po_id = $("#po-id").val();
    var stat = "cancel";
    var redir_url = url_page+"?action=changePaid&poid="+po_id+"&stat="+param;
    if(param == 'no'){
        swal({
            title: "Are you sure to set not paid?",
            text: "",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Process ! ",
            cancelButtonText: "Cancel !",
            closeOnConfirm: true,
            closeOnCancel: true
        },
        function (isConfirm) {
            if (isConfirm) {
                window.location.href = redir_url;
            } else {
                //nothing
            }
        });
    } else {
        swal({
        title: "Are you sure want to set as Paid this order ?",
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#00c0ef",
        confirmButtonText: "Process ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.location.href = redir_url;
        } else {
            //nothing
        }
    });
    }
}
function confirmDelete(){
    var url_page = $("#input-pageurl").val();
    var po_id = $("#po-id").val();
    var redir_url = url_page+"?action=delete&poid="+po_id;
    swal({
        title: "Are you sure to DELETE this order?",
        text: "You will not be able to open this order again!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Process ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.location.href = redir_url;
        } else {
            //nothing
        }
    });
}
//END Detail Function