/**
 * 用户相关js
 *
 * @author CaiAoLin
 * @date 2017/6/13
 * @version
 */
$(document).ready(function() {

    $("#info-form").submit(function() {
        if (!valid()) {
            return false;
        }
    });

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

    //获取cld接口手机号码和数据
    $('.getcategory').on('click', function () {
        let category = $(this).attr('data-category');
        let isupgrade = $(this).attr('data-upgrade');
        if (isupgrade === 'true') {
            $('#upgrade-title').text($(this).attr('data-title') + ' 售后服务');
        } else {
            $('#upgrade-title').text('联系销售代表激活');
        }
        CommonHeader.getCategoryList(category);
    });

    // 关闭和开启账号登录
    $('#isSmsLogin').click(function () {
        let status = $(this).is(':checked') ? 1 : 0;
        $.ajax({
            type: 'post',
            url: '/user/change/isSmsLogin',
            data: {status : status},
            success: function (response) {
                if (response.error !== 0) {
                    alert(response.msg);
                }
            }
        })
    });

    // 关闭和开启异常登录提醒
    $('#isLoginValid').click(function () {
        let status = $(this).is(':checked') ? 1 : 0;
        $.ajax({
            type: 'post',
            url: '/user/change/isLoginValid',
            data: {status : status},
            success: function (response) {
                if (response.error !== 0) {
                    alert(response.msg);
                }
            }
        })
    })
});

/**
 * 验证数据
 *
 * @return {boolean}
 */
function valid() {
    let result = true;
    let name = $("#name").val();
    if (name === undefined || name === '') {
        showError('您得填上姓名!', $("#name"));
        return false;
    }

    let company = $("#company").val();
    if (company === undefined || company === '') {
        showError('您得填上公司名称!', $("#company"));
        return false;
    }

    let province = $("#province").val();
    if (province === undefined || province === '') {
        showError('您得选择企业所在地区!', $("#province"));
        return false;
    }

    return result;
}

/**
 * 提示错误
 *
 * @param {string} msg
 * @param {object} element
 * @return {void}
 */
function showError(msg, element) {
    element.addClass('orm-control-danger');
    if (element !== null) {
        element.parent().addClass('has-danger');
    }
    let html = '<div class="form-control-feedback" style="color: red">' + msg + '</div>';
    if (element.siblings('.form-control-feedback').length > 0) {
        element.siblings('.form-control-feedback').text(msg);
    } else {
        element.after(html);
    }
}

/**
 * 清除错误提示
 *
 * @return {void}
 */
function cleanError() {
    $("input").removeClass('orm-control-danger');
    $("input").parent().removeClass('has-danger');
    $(".form-control-feedback").remove();
}
