var card = {
    relationRole:{},
    clickFlag:true,
    bindInfo:{},
    bindFlag : true
};
card.ajax = function (data, callback) {
    data.data['_token'] = $('meta[name="token"]').attr("value");
        $.ajax({
            type: data.type ? data.type : 'GET',
            url: location.origin + data.url,
            dataType: "json",
            data: data.data,
            error: function (err) {
                try{
                    var error = err.responseText;
                    alert(error.message)
                }catch (e) {
                    alert('系统开小差~~')
                }
            },
            success: function (res) {
                if(res.error == 1019){
                    location.href="/login";
                    return false;
                }else if(res.error == 4003 ){
                    location.href = "/bind/ald/code"
                    return false;
                }
                callback(res);
            }
        });
        setTimeout(function (){
            card.clickFlag = true;
        },1000)
}
// 获取服务器和用户id
card.handleGetServer = function (receive){
    $('.loading').show();
    this.ajax({
        url:'/ald_bind_info',
        type:'get',
        data:{}
    },function (res){
        card.bindInfo = res.data;
        var params = {
            serverId:res.data.serverId,
            userId:res.data.userId
        }
        card.handleGetRoleList(params);
    })
}

// 获取角色列表
card.handleGetRoleList = function (params){
    this.ajax({
        url:'/get_role_list',
        type:'POST',
        data:params
    },function (res){
        if(res.error == 80001){
            alert(res.message);
            return false;
        }
        var data = res.data,
            html = '',
            sArr = [];
        card.relationRole = data;
        for(var key in data){
            sArr.push({id:key,name:data[key].name});
        }
        html='<option value="-1">请选择</option>'
        $(sArr).each(function (index,item){
            var name = '';
            if(item.name.indexOf('非硬核-')>-1){
                name = item.name.replace('非硬核-','')
            }else if(item.name.indexOf('硬核-')>-1){
                name = item.name.replace('硬核-','')
            }else if(item.name.indexOf('越狱-')>-1){
                name = item.name.replace('越狱-','')
            }else{
                name = item.name
            }
            html+='<option value="'+item.id+'">'+name+'</option>';
        })
        $('.server-select').html(html);
        $('.loading').hide();
        $('.dialog-server').fadeIn();
    })
}
// 绑定角色
card.bindRole = function (params){
    card.bindFlag = false;
    this.ajax({
        url:'/bind/ald/role',
        type:'POST',
        data:params
    },function (res){
        if(res.error === 0){
            $('.dialog').hide();
            location.href='/card/html?roleId='+params.roleId+'&serverId='+params.serverId;
        }else{
            alert(res.msg)
        }
    })
}
// 初始化服务器列表
var initRoleList = function (serverId){
    var roleList = card.relationRole[serverId].list;
    var sHtml='<option value="-1">请选择</option>'
    $(roleList).each(function (index,item){
        sHtml+='<option value="'+item.roleId+'">'+item.roleName+'</option>'
    });
    $('.role-select').html(sHtml)
}
$(function (){
    $('.btn-card-bind-role').click(function (){
        var serverId = $('.server-select option:selected').val(),
            serverName = $('.server-select option:selected').text(),
            roleId = $('.role-select option:selected').val();
            roleName = $('.role-select option:selected').text();
        if(!serverId || serverId == '-1'){
            alert('请选择服务器')
            return false;
        }else if(!roleId || roleId == '-1'){
            alert('请选择角色')
            return false;
        }
        var params = {
            roleId:roleId,
            serverId:serverId,
            serverName:serverName,
            roleName:roleName
        }
        if(card.bindFlag){
            setTimeout(function(){
                card.bindFlag = true;
            },3000)
            card.bindRole(params);
        }
    });
    $('.server-select').change(function (){
        var serverId = $(this).val();
        if(serverId == -1){
            $('.role-select').html('');
            return false;
        }
        initRoleList(serverId);
    });
})