﻿/// <reference path="GmrJsLib.js" />
/// <reference path="Plugins/XmlApi.js" />

var gmrUser = function ()
{
    var config = { apiurl: "/api/apiuser.aspx" };
    var features = { appid: "GmrUserAPI" };
    function pageAPI(method, params, funReceive)
    {
        gmrPageAPI(config.apiurl, method, params, funReceive, features);
    }
    return {
        isLogined: function (receiveFunction)
        {
            /// <summary>查询当前是否有用户登录</summary>
            /// <param name="receiveFunction">回调函数，传入参数为布尔值</param>

            gmrPageAPI(config.apiurl, "islogined", [], function (code, mes, response)
            {
                if (code == 0) { if (response.GetParamValue("status") == "1") { receiveFunction(true, response) } else { receiveFunction(false, response); } } else { alert(mes); }
            }, features);
        },
        getUserInfo: function (receiveFunction)
        {
            /// <summary>查询当前用户登录状态信息</summary>
            gmrPageAPI(config.apiurl, "getuserinfo", [], receiveFunction, features);
        },
        checkUsername: function (username, receiveFunction)
        {
            /// <summary>检测用户名</summary>
            /// <param name="username">要检测的用户名</param>
            /// <param name="receiveFunction">回调函数</param>
            gmrPageAPI(config.apiurl, "checkusername", ["username", username], receiveFunction, features);
        },
        checkEmail: function (email, receiveFunction)
        {
            /// <summary>检测用户名</summary>
            /// <param name="username">要检测的用户名</param>
            /// <param name="receiveFunction">回调函数</param>
            gmrPageAPI(config.apiurl, "checkemail", ["email", email], receiveFunction, features);
        },
        userLogin: function (username, useremail, userpassword, isrememberusername, isrememberpassword, otherinfo, receiveFunction)
        {
            /// <summary>用户登录</summary>  
            gmrPageAPI(config.apiurl, "userlogin", ["username", username, "useremail", useremail, "userpassword", userpassword, "isrememberusername", isrememberusername, "isrememberpassword", isrememberpassword.toString(), "otherinfo", otherinfo], receiveFunction, features);
        },
        userRegist: function (username, useremail, userpassword, otherinfo, receiveFunction)
        {
            /// <summary>用户注册</summary>   
            gmrPageAPI(config.apiurl, "userregist", ["username", username, "useremail", useremail, "userpassword", userpassword, "otherinfo", otherinfo], receiveFunction, features);
        },
        getPassword: function (username, useremail, vcode, otherinfo, funCall)
        {
            /// <summary>用户找回密码</summary>            
            pageAPI("getpassword", ["username", username, "useremail", useremail, "vcode", vcode], funCall);
        },
        resetPassword: function (username, email, password, guid, otherinfo, funCall)
        {
            /// <summary>重新设置密码</summary>

            pageAPI("resetpassword", ["username", username, "email", email, "password", password, "guid", guid, "otherinfo", otherinfo], funCall);
        },
        webLoginToRegister: function ()
        {
            /// <summary>从登录页面跳转到注册页面</summary>
            // var ru = gmrGetUrlParam("ReturnUrl");
            var s = window.location.search;
            window.location = "/user/regist.html" + s;
        },
        webRegisterToLogin: function ()
        {
            /// <summary>从注册页跳转到登录页面</summary>
            var s = window.location.search;
            window.location = "/user/login.html" + s;
        },
        webUserLogin: function (txtUsername, txtPassword, chkIsrememberUsername, chkIsrememberPassword, txtVcode, imgVcode, btnSrc, funEnd)
        {
            /// <summary>用户登录</summary>
            ///#region         
            var txtUsername = $(txtUsername), txtPassword = $(txtPassword), chkIsrememberUsername = chkIsrememberUsername ? $(chkIsrememberUsername) : null, chkIsrememberPassword = chkIsrememberPassword ? $(chkIsrememberPassword) : null, txtVcode = txtVcode ? $(txtVcode) : null, btnScr = $(btnScr), imgVcode = imgVcode ? $(imgVcode) : null;
            txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = true;
            var otherInfo = txtVcode ? "valcode=" + encodeURI(txtVcode.value.Trim()) : "hasvalcode=false";
            gmrUser.userLogin(txtUsername.value.Trim(), "", txtPassword.value.Trim(), chkIsrememberUsername ? chkIsrememberUsername.checked : false, chkIsrememberPassword ? chkIsrememberPassword.checked : false, otherInfo, function (code, message, response)
            {
                if (code == 0)
                {
                    var cookiename = response.GetParamValue("usercookiename"), cookievalue = response.GetParamValue("usercookievalue");
                    /*if (chkIsrememberPassword.checked && cookiename && cookiename.length > 0)
                    {
                    Gmr.Cookie.Add(cookiename, cookievalue, 20160);
                    }*/
                    if (funEnd) { funEnd(); return false; }
                    gmrCloseOpenWindow();
                    return;
                    if (gmrGetUrlParam("cmd") == "close")
                    {
                        gmrCloseOpenWindow();
                    }
                    else if (Gmr.Pub.GetUrlParam("ReturnUrl").length > 0)
                    {
                        window.location = decodeURIComponent(Gmr.Pub.GetUrlParam("ReturnUrl"));
                    }
                    else
                    {
                        window.top.location = "/user/";
                    }
                }
                else
                {
                    alert("登录错误,请重试\n" + message);
                    txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = false;
                    if (txtVcode)
                    {
                        txtVcode.disabled = false;
                    }
                    if (imgVcode)
                    {
                        imgVcode.click();
                    }
                }
            });
            ///#endregion 
        },
        webGetPassword: function (txtUsername, txtEmail, txtVcode, imgVcode, btnSrc)
        {
            /// <summary>找回密码</summary>
            var txtUsername = $(txtUsername), txtEmail = $(txtEmail), txtVcode = $(txtVcode), imgVcode = $(imgVcode), btnSrc = $(btnSrc);
            btnSrc.disabled = true;
            if (txtUsername.value.length == 0 && txtEmail.value.length == 0)
            {
                alert("用户名或用户至少输入一项");
                btnSrc.disabled = false;
            }
            else
            {
                gmrUser.getPassword(txtUsername.value.trim(), txtEmail.value.trim(), txtVcode ? txtVcode.value.trim() : "", null, function (code, mes, reponse)
                {
                    if (code == 0)
                    {
                        alert("密码邮件已经送到您的邮箱，请及时查收");
                    }
                    else
                    {
                        alert(mes);
                    }
                    btnSrc.disabled = false;
                    if (imgVcode)
                    {
                        imgVcode.click();
                    }
                });
            }

        },
        webResetPassword: function (txtPsw, btnSrc)
        {
            /// <summary>重新设置密码</summary>

            btnSrc.disabled = true;
            var email = decodeURIComponent(gmrPub.GetUrlParam("email"));
            var guid = gmrPub.GetUrlParam("guid");

            gmrUser.resetPassword("", email, $(txtPsw).value.trim(), guid, "", function (code, mes, response)
            {
                if (code == 0) { alert("新密码设置成功！"); }
                else { alert(mes); }
                btnSrc.disabled = false;
            });

        },
        webAutoReload: function ()
        {
            var url = decodeURIComponent(gmrGetUrlParam("ReturnUrl"));
            gmrUser.isLogined(function (isLogined)
            {
                if (isLogined) { window.location.href = url; }
            });


        }
    };
} ();

function webUserCheckUserName(txtUsername, valControl, messageOk, messageError)
{
    /// <summary>检测用户名</summary>
    /// <param name="txtUsername"></param>
    /// <param name="valControl"></param>
    /// <param name="messageOk"></param>
    /// <param name="messageError"></param>

    var txtUsername = $(txtUsername);
    var valControl = $(valControl);
    gmrUser.checkUsername(txtUsername.value.Trim(), function(code, message, response)
    {        
        if (code == 0)
        {
            gmrValSetMessage(valControl, true, messageOk);
        }
        else
        {
            gmrValSetMessage(valControl, false, messageError);
        }
    });
}
function webUserCheckEmail(txtEmail, valControl, messageOk, messageError)
{
    /// <summary>检测用户名</summary>
    /// <param name="txtEmail"></param>
    /// <param name="valControl"></param>
    /// <param name="messageOk"></param>
    /// <param name="messageError"></param>

    var txt = $(txtEmail);
    var valControl = $(valControl);
   gmrUser.checkEmail(txt.value.Trim(), function(code, message, response)
    {
        if (code == 0)
        {
            gmrValSetMessage(valControl, true, messageOk);
        }
        else
        {
            gmrValSetMessage(valControl, false, messageError);
        }
    });
} 
function webUserRegist(txtUsername, txtEmail, txtPassword, txtVcode, radUserType, txtMobile, imgVcode, btnSrc)
{
    var txtUsername = $(txtUsername), txtEmail = $(txtEmail), txtPassword = $(txtPassword),  txtType = $(txtType), txtMobile = $(txtMobile);
    var txtVcode = txtVcode ? $(txtVcode) : null,imgVcode = imgVcode ? $(imgVcode) : null;
    txtEmail.disabled = txtMobile.disabled = txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = true;
    if (txtVcode) { txtVcode.disabled = true; }
    var otherInfo = "mobile=" + encodeURI(txtMobile.value.Trim()) + "&usertype=" + encodeURI(gmrRadioGetValue(radUserType, 0)) + "&valcode=" + (txtVcode ? encodeURI(txtVcode.value.Trim()) : "");
    gmrUser.userRegist(txtUsername.value.Trim(), txtEmail.value.Trim(), txtPassword.value.Trim(), otherInfo, function (code, message, response)
    {
        if (code == 0)
        {
            var status = response.GetParamValue("status");
            if (status == "logined")
            {
                alert("恭喜您注册成功!");
                gmrCloseOpenWindow();
                return;
                if (gmrGetUrlParam("cmd") == "close")
                {
                    gmrCloseOpenWindow();
                }
                else if (gmrGetUrlParam("ReturnUrl").length > 0)
                {
                    window.location = decodeURIComponent(Gmr.Pub.GetUrlParam("ReturnUrl"));
                }
                else
                {
                    window.location = window.top.location;
                }
            }
            else
            {
                alert("恭喜您注册成功！将转到登录页面");
                window.top.location = "/user/login.html";
            }
        }
        else
        {
            alert("注册失败\\n" + message);
            txtEmail.disabled = txtMobile.disabled = txtPassword.disabled = txtUsername.disabled = btnSrc.disabled = false;
            if (txtVcode)
            {
                txtVcode.disabled = false;
            }
            if (imgVcode)
            {
                imgVcode.click();
            }
        }
    });
}
function webUserGetInfo(obj)
{
    /// <summary>查询用户综合信息</summary>
    /// <param name="obj">信息容器对象</param>

    var divUser = $("divUserInfoBar");
    if (divUser == null) {return;}
    gmrUser.getUserInfo(function (code, mes, response)
    {
        if (code == 0)
        {
            divUser.innerHTML = response.GetParamValue("htmltext"); return;
            if (gmrPub.parseBool(response.gpv("islogin")) && $("divTopTotalLength") != null)
            {
                obj.innerHTML = response.GetParamValue("htmltext");
            }
        }
    });
}
function webShowLogin(type, funCall)
{
    /// <summary>显示用户登录框</summary>
    /// <param name="type">窗体类型</param>
    /// <param name="funCall">回调函数</param>
    var fEnd = function ()
    {
        webUserGetInfo("divTopUserInfoBar");
        if (funCall) { funCall(); }
    }
    return gmrOpenWindow("", "/html/user/login.html", "width:600px;height:400px", "", "scrolling:auto", fEnd);
}
function webShowRegist(type, funCall)
{
    /// <summary>显示用户登录框</summary>
    /// <param name="type">窗体类型</param>
    /// <param name="funCall">回调函数</param>
    var fEnd = function ()
    {
        webUserGetInfo("divTopUserInfoBar");
        if (funCall) { funCall(); }
    }
    return gmrOpenWindow("", "/html/user/regist.html", "width:600px;height:400px", "", "scrolling:auto", fEnd);
}
function webShowGetPassword(type, funCall)
{
    /// <summary>显示用户登录框</summary>
    /// <param name="type">窗体类型</param>
    /// <param name="funCall">回调函数</param>
    var fEnd = function ()
    {
        webUserGetInfo("divTopUserInfoBar");
        if (funCall) { funCall(); }
    }
    return gmrOpenWindow("", "/html/user/getpassword.html", "width:600px;height:400px", "", "scrolling:auto", fEnd);
}
