var sign = {
    relationRole:{},
    clickFlag:true
};
sign.ajax = function (data, callback,fileCallback) {
    data.data['_token'] = $('meta[name="token"]').attr("value");
    if(sign.clickFlag){
        sign.clickFlag = false;
        $.ajax({
            type: data.type ? data.type : 'GET',
            url: ccwl.http + data.url,
            dataType: "json",
            data: data.data,
            error: function (err) {
                ccwl.ajaxFlag = false;
                try{
                    var error = err.responseText;
                    ccwl.showToast({
                        msg:error.message,
                        type:'error'
                    })
                }catch (e) {
                    ccwl.showToast({
                        msg:'系统开小差~~',
                        type:'error'
                    })
                }
            },
            success: function (res) {
                if(res.error == 1019){
                    location.href="/login";
                    return false;
                }
                ccwl.ajaxFlag = false;
                callback(res);
            }
        });
        setTimeout(function (){
            sign.clickFlag = true;
        },1000)
    }
}
function renderDateStyle(){
    var index = $('.progress-date span').attr('data-type')*1;
    var width = ((index+1)/12).toFixed(2)*100+'%';
    $('.progress-date span').width(width);
    if(index+1 == 3 || index+1 == 7 || index+1 == 12){
        $('.task-date dl').eq(index).addClass('current');
    }
}
// 签到
sign.handleSign = function (obj,index){
    this.ajax({
        url:'/sign',
        type:'POST',
        data:{}
    },function (res){
        if(res.error === 0){
            renderDateStyle();
            var giftWidth = $('.gift-award dl').width();
            obj.addClass('done').removeClass('current');
            $(".scroll-container-gift").animate({scrollLeft:giftWidth*(index-1)});
            $('.dialog-confirm .toast-msg').text(res.message);
            $('.dialog-confirm .btn').addClass('btn-close');
            $('.dialog-confirm').fadeIn();
        }else if(res.error === 4003){
            $('.dialog-confirm .toast-msg').text(res.message);
            $('.dialog-confirm .btn').attr('href','/bind/ald/code');
            $('.dialog-confirm').fadeIn();
        }else{
            ccwl.showToast({
                msg:res.message,
                type:'warn'
            })
        }
    })
}

// 领取
sign.handleReceive = function (params,obj){
    this.ajax({
        url:'/send/sign/gift',
        type:'POST',
        data:params
    },function (res){
        if(res.error===0){
            $(obj).parents('dl').addClass('done gift').removeClass('current');
            ccwl.showToast({
                msg:res.message,
                type:'success'
            })
        }else{
            ccwl.showToast({
                msg:res.message,
                type:'warn'
            })
        }

    })
}

// 获取服务器和用户id
sign.handleGetServer = function (receive){
    this.ajax({
        url:'/ald_bind_info',
        type:'get',
        data:{}
    },function (res){
        if(receive === "0"){
            $('.confirm-container').show();
            $('.confirm-container .message-content p').text('您暂未获得活动资格，请关注后续活动资讯');
            $('.confirm-container .message-content a').text('确定').addClass('close-click').attr('onclick','');
            return false;
        }
        var params = {
            serverId:res.data.serverId,
            userId:res.data.userId
        }
        ccwl.handleGetRoleList(params);
    },function (res){
        $('.confirm-container p').text(res.message);
        $('.confirm-container').show();
    })
}

// 获取角色列表
sign.handleGetRoleList = function (params){
    this.ajax({
        url:'/get_role_list',
        type:'POST',
        data:params
    },function (res){
        var data = res.data,
            html = '',
            sArr = [];
        sign.relationRole = data;
        for(var key in data){
            sArr.push({id:key,name:data[key].name});
        }
        $(sArr).each(function (index,item){
            html+='<li data-value="'+item.id+'">'+item.name+'</li>';
        })
        $('.server-select').html(html);
        $('.dialog-received-gift').fadeIn();
    },function(res){

    })
}
// 绑定角色
sign.bindRole = function (params){
    this.ajax({
        url:'/bind/sign/role',
        type:'POST',
        data:params
    },function (res){
        if(res.error === 0){
            $('.dialog-received-gift,.show-roles').hide();
            $('.role-info').text(params.serverName +' '+params.roleName);
            ccwl.showToast({
                msg:res.message,
                type:'success'
            })
        }else{
            ccwl.showToast({
                msg:res.message,
                type:'warn'
            })
        }
    })
}

$(function (){
    $(document).on('click','.gift-award .status',function (){
        var obj = $(this).parents('dl');
        var index = $(this).attr('data-type');
        if(obj.hasClass('current')){
            // 签到
            if(sign.clickFlag){
                sign.handleSign(obj,index);
            }
        }
    });
    $('.show-roles').click(function (){
        sign.handleGetServer();
        $('.dialog-received-gift').fadeIn();
    });
    $('.btn-bind-role').click(function (){
        var serverId = $('.server-select li.current').attr('data-value'),
            serverName = $('.server-select li.current').text(),
            roleName = $('.role-select li.current').text(),
            roleId =  $('.role-select li.current').attr('data-value');
        if(!serverId || serverId == ''){
            ccwl.showToast({msg:'请选择服务器'});
            return false;
        }else if(!roleId || roleId == ''){
            ccwl.showToast({msg:'请选择角色'});
            return false;
        }
        var params = {
            roleId:roleId,
            serverId:serverId,
            serverName:serverName,
            roleName:roleName
        }
        if(sign.clickFlag){
            sign.bindRole(params);
        }
    });
    $(document).on('click','.task-date dl.current dt',function (){
        var type = $(this).attr('data-type');
        var params = {
            type:type
        }
        if(sign.clickFlag){
            sign.handleReceive(params,this);
        }
    });
    $(document).on('click','.btn-close',function (){
        $(this).parents('.dialog').hide();
    })
})