var card = {
    relationRole:{},
    clickFlag:true,
    bindInfo:{},
    bindFlag : true
};
card.showToast = function (params) {
    $.Toast("", params.msg, params.type ? params.type : 'warn', {
        stack: true,
        has_icon: false,
        has_close_btn: false,
        fullscreen: false,
        timeout: 2000,
        sticky: false,
        has_progress: false,
        rtl: false,
    });
}
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;
                    card.showToast({
                        msg:error.message
                    })
                }catch (e) {
                    card.showToast({
                        msg:'系统开小差~~'
                    })
                }
            },
            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){
            card.showToast({
                msg: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();
            $('.role-select').html('');
            location.href='/card/html?roleId='+params.roleId+'&serverId='+params.serverId;
        }else{
            card.showToast({
                msg:res.message
            })
        }
    })
}
// 初始化服务器列表
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'){
            card.showToast({
                msg:'请选择服务器'
            })
            return false;
        }else if(!roleId || roleId == '-1'){
            card.showToast({
                msg:'请选择角色'
            })
            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);
    });
    $('.close').click(function (){
        $('select').html('');
        $('.dialog').fadeOut();
    });
    $('.btn-start').click(function (){
        card.handleGetServer();
    });
    function baseImageUrl() {
        setTimeout(function () {
            html2canvas(document.querySelector("#test"), {  //关键在于new
                removeContainer: true,
                allowTaint: false,
                width: window.screen.availWidth,
                windowWidth: document.body.scrollWidth,
                x: 0,
                y: 0,
                useCORS: true,
                logging:true
            }).then(canvas => {
                var image = canvas.toDataURL('image/jpeg', 1.0);
                var img = '<img src="' + image + '">';
                $('.dialog-img-content .img-scroll').html(img);
                $('.dialog-img').show();
                $('.loading').hide();
            });
            $('.postcard-container').addClass('fixed');
        }, 2000)
    }

    $('.btn-save').click(function () {
        var height = $('.postcard-container').height();
        $('.postcard-container').height(height);
        $('.loading').show();
        $('.btn-save,.btn-share,.login-info').hide();
        $('html,body').animate({
            scrollTop: 0
        }, 0);
        baseImageUrl();
        $('.postcard-container').addClass('fixed');
    });
    $('.dialog-img,.share-img').click(function () {
        $(this).fadeOut();
        $('.btn-save,.btn-share,.login-info').show();
        $('.postcard-container').removeClass('fixed');
    });
})