$(document).ready(function () {

    loadvalue();

    $("#search_btn").click(function () {
        if ($("#rule").val() == "") {
            alert('請輸入機率值');
            return;
        }

        var formData = {
            value: $("#rule").val()
        }

        $.ajax({
            url: "/BackEndApi/updateRule",
            type: "POST",
            data: formData,
            success: function (data, textStatus, jqXHR) {
                if (data.ret == "yes") {
                    $("#rule").val(data.rule.rules_value).trigger('change');
                    alert('儲存成功!');
                } else {
                    alert(data.message);
                    if (data.err_code == "9999") {
                        location.href = "login.aspx?isLogout=true";
                    }
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('網路或伺服器發生錯誤，請稍後重試!');
            }
        });
    });

    function loadvalue() {
        $.ajax({
            url: "/BackEndApi/getRule",
            type: "POST",
            data: null,
            success: function (data, textStatus, jqXHR) {
                if (data.ret == "yes") {
                    $("#rule").val(data.rule.rules_value).trigger('change');
                } else {
                    alert(data.message);
                    if (data.err_code == "9999") {
                        location.href = "login.aspx?isLogout=true";
                    }
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('網路或伺服器發生錯誤，請稍後重試!');
            }
        });
    }
});