var card = {
    relationRole:{},
    clickFlag:true,
    bindInfo:{}
};
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){
    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);
        $('.dialog-server').fadeIn();
    })
}

// 获取角色列表
card.handleGetRoleList = function (params){
    this.ajax({
        url:'/get_role_list',
        type:'POST',
        data:params
    },function (res){
        var data = res.data,
            html = '',
            sArr = [];
        // data={
        //     269:{
        //             name:'越狱-月光酒馆-打的',list:[{
        //                 roleId:1,
        //                 roleName:'aaaa'
        //             }]
        //         },
        //     270:{
        //         name:'越狱aaa',list:[{
        //             roleId:1,
        //             roleName:'bbb'
        //         }]
        //     }
        //     }
        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){
            html+='<option value="'+item.id+'">'+item.name+'</option>';
        })
        $('.server-select').html(html);
    })
}
// 绑定角色
card.bindRole = function (params){
    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.clickFlag){
            card.bindRole(params);
        }
    });
    $('.server-select').change(function (){
        var serverId = $(this).val();
        initRoleList(serverId);
    });
})