$(document).ready(function () {
    var myPageModule = userService.myPageModule();
    myPageModule.myPageRequest(function (user, userPhone) {
        $("#userId").text(user.loginId);
        $("#userId").val(user.loginId);
        $("#name").val(user.name);
        $(":input:radio[name=genderRadios]:input[value=" + user.gender + "]").attr("checked", true);
        $("#birth").val(clfdUtils.appendHyphenToBirth(user.birth));
        $("#phone").val(clfdUtils.appendHyphenToPhone(userPhone.phone));
    });

    $("#pwUpdateBtn").click(function () {
        if ($("#passwordDiv").css("display") == "none") {
            $("#passwordDiv").css("display", "");
        } else {
            $("#passwordDiv").css("display", "none");
        }
    })

    $("#changePhoneBtn").click(function () {
        if ($("#phoneDiv").css("display") == "none") {
            $("#phoneDiv").css("display", "");
        } else {
            $("#phoneDiv").css("display", "none");
        }
    })

    $("#cancelBtn").click(function () {
        location.href = "/";
    })

    $("#changeBtn").click(function () {
        var changedUser = {
            loginId: $("#userId").text(),
            name: $("#name").val(),
            gender: $(":input:radio[name=genderRadios]:checked").val(),
            birth: clfdUtils.removeHyphen($("#birth").val())
        }

        if (!myPageModule.isChangedForUpdate(changedUser)) {
            alert("변경된 내용이 없습니다.");
            return;
        }

        if (myPageModule.myPageChangeCheckAll(changedUser)) {
            myPageModule.myPageUpdateRequest(changedUser, function () {
                location.reload();
            });
        }
    })
    $("#newPassword").keyup(function () {
        userService.comparePasswordEquality($("#newPassword").val(), $("#newPasswordConfirm").val());
    })
    $("#newPasswordConfirm").keyup(function () {
        userService.comparePasswordEquality($("#newPassword").val(), $("#newPasswordConfirm").val());
    })
    $("#pwdChangeBtn").click(function () {
        var pwd = {
            password: $("#password").val(),
            newPassword: $("#newPassword").val(),
            newPasswordConfirm: $("#newPasswordConfirm").val(),
        }
        if (userService.changePasswordCheck(pwd)) {
            userService.changePasswordRequest(pwd, function () {
                $("#passwordDiv").css("display", "none");
            });
        }
    })

    $('#authRequestBtn').click(function () {
        var phone = clfdUtils.removeHyphen($("#phone").val());
        var newPhone = clfdUtils.removeHyphen($("#newPhone").val());
        var checkPhone = userService.checkPhone(newPhone);
        if (newPhone == phone) {
            alert("휴대폰 번호가 변경되지 않았습니다.");
            return;
        }
        if (checkPhone) {
            userService.ajaxSmsSend(newPhone, function () {
            });
        }
    });

    $('#authCheckBtn').click(function () {
        var scb = function () {
            location.reload();
        };
        var ecb = function () {
        };
        var newPhone = clfdUtils.removeHyphen($("#newPhone").val());
        myPageModule.ajaxSmsUpdatePhone($('#phoneAuthKey').val(), newPhone, scb, ecb);
    });
})
