
//#region 個人資訊表單驗證
$(function () {
    $("#personal-info-form").validate({
        rules: {
            name_modal: {
                required: true,
                maxlength: 50
            },
            email_modal: {
                email: true,
            },
        },
    });

    $("#btn-personal-info").click(function (e) {
        var url = "/User/GetPersonalInfo"

        $.post(url, null, function (rel) {
            if (rel.code != "0000") {
                toast_error(rel.msg);
            }

            $("#name_modal").val(rel.data.name);
            $("#account_modal").val(rel.data.account);
            $("#email_modal").val(rel.data.email);
            $("#phone_modal").val(rel.data.phone);

            $('#personal-info-modal').modal();
        }, 'json');
    });
})
//#endregion

//#region 修改個人資料
function SavePersonalInfo() {
    if ($("#personal-info-form").valid()) {
        var url = "/User/SavePersonalInfo";

        var send_data = {
            Name: $("#name_modal").val(),
            Account: $("#account_modal").val(),
            Email: $("#email_modal").val(),
            Phone: $("#phone_modal").val()
        }

        $.post(url, send_data, function (rel) {
            if (rel.code == "9999") {
                toast_error(rel.msg);
                return;
            }
            else if (rel.code == "9998") {
                toast_error(rel.msg);

                setTimeout(function () {
                    location.href = "/Login/Logout";
                }, 5000)
            }

            toast_ok(rel.msg);
            $('#personal-info-modal').modal('hide');
        }, 'json');
    }
}
//#endregion

//#region 變更密碼表單驗證
$(function () {
    jQuery.validator.addMethod("pwcheck", function (value, element) {
        var xxx = /^[A-Za-z0-9\d=!\-@._*]*$/.test(value);
        return xxx;// consists of only these
        //&& /[a-z]/.test(value) // has a lowercase letter
        //&& /\d/.test(value) // has a digit
    });

    $("#change-password-form").validate({
        rules: {
            old_password_modal: {
                required: true,
                //rangelength: [6, 12]
            },
            new_password_modal: {
                required: true,
                rangelength: [6, 12],
                pwcheck: true
            },
            again_password_modal: {
                required: true,
                rangelength: [6, 12],
                pwcheck: true,
                equalTo: "#new_password_id_modal",
            }
        },
        messages: {
            new_password_modal: {
                pwcheck: "密碼格式錯誤"
            },
            again_password_modal: {
                pwcheck: "密碼格式錯誤",
                equalTo: "兩次密碼輸入不同"
            }
        }
    });

    $("#btn-change-password").click(function (e) {
        $("#change-password-form").trigger("reset");
        $('#change-password-modal').modal();
    });
})
//#endregion

//#region 變更密碼
function ChangePassword() {
    if ($("#change-password-form").valid()) {
        var url = "/User/ChangePassword";

        var send_data = {
            OldPassword: $("#old_password_id_modal").val(),
            NewPassword: $("#new_password_id_modal").val(),
            AgainPassword: $("#again_password_id_modal").val()
        }

        $.post(url, send_data, function (rel) {
            if (rel.code == "9999") {
                toast_error(rel.msg);
                return;
            }
            else if (rel.code == "9998") {
                toast_error(rel.msg);

                setTimeout(function () {
                    location.href = "/Login/Logout";
                }, 5000)
                return;
            } else if (rel.code == "0001") {
                toast_error(rel.msg);
                return;
            }

            toast_ok(rel.msg);
            $('#change-password-modal').modal('hide');
        }, 'json');
    }
}
//#endregion
