/**
 * 登录相关js
 *
 * @author CaiAoLin
 * @date 2017/6/8
 * @version
 */
$(document).ready(function () {
    let referer = scUrlUtil.GetQueryString('referer');

    $("#login").click(function () {
        if (!valid()) {
            return false;
        }
        let account = $("#inputEmail").val();
        let pw = $("#inputPassword").val();

        $.ajax({
            url: '/login',
            type: 'post',
            data: {"account": account, "pw": pw},
            success: function (response) {
                if (response.error === 0) {
                    if (response.login_ask === 0) {
                        location.href = '/pm';
                    } else {
                        response.compilation_list = response.compilation_list === undefined || response.compilation_list === '' ?
                            null : JSON.parse(response.compilation_list);
                        if (response.compilation_list === null || response.compilation_list.length <= 0) {
                            location.href = '/pm';
                            return false;
                        }
                        setVersion(response.compilation_list);
                        $('#ver').modal('show');
                    }
                } else {
                    let msg = response.msg !== undefined ? response.msg : '未知错误';
                    showError(msg, $("input"));
                }
            },
            error: function (result) {
                showError('内部程序错误', null);
            }
        });
    });

    $("input").blur(function () {
        cleanError();
    });

    $(".form-control").on('input', function () {
        $('#hint').html('&nbsp;');
    });

});

/**
 * 验证数据
 *
 * @return {boolean}
 */
function valid() {
    let result = true;
    let account = $("#inputEmail").val();
    if (account === undefined || account === '') {
        showError('用户名不能为空!', $("#inputEmail"));
        return false;
    }

    let password = $("#inputPassword").val();
    if (password === undefined || password === '') {
        showError('密码不能为空!', $("#inputPassword"));
        return false;
    }

    return result;
}

/**
 * 提示错误
 *
 * @param {string} msg
 * @param {object} element
 * @return {void}
 */
function showError(msg, element) {
    if (element !== null) {
        element.parent().addClass('has-danger');
    }
    $("#message").text(msg);
    $("#error-tips").show("fast");
}

/**
 * 清除错误提示
 *
 * @return {void}
 */
function cleanError() {
    $("input").parent().removeClass('has-danger');
    $("#message").text('');
    $("#error-tips").hide("fast");
}

/**
 * 设置版本信息
 *
 * @param {Object} versionData
 * @return {void}
 */
function setVersion(versionData) {
    let html = '';
    for (let version of versionData) {
        let tmpHtml = '<div class="col-sm-6">' +
            '<div class="card card-block">' +
            '<h3 class="card-title">'+ version.name +'</h3>' +
            '<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>' +
            '<a class="btn btn-primary" href="/boot/'+ version._id.toString() +'">开始使用</a>' +
            '</div>' +
            '</div>';
        html += tmpHtml;
    }
    $("#version-area").html(html);
}