function getData (index,type, name ) {
    $.ajax({
        type : "POST",
        url : host+"vote.php/Index/userList",
        data : {
            type: type,
            limit:1000,
            name: name || '',
            open_id: localStorage.openId
        },
        success : function(result) {
            $('#owner-box').children('.owner-item').eq(index).children('.loading').hide();
            var html='';

                var resultData = result.data.list;
                resultData.forEach(function( val, index ) {
                    html+='<li>'+
                        '<div class="item">'+
                        (val.rank !==0?('<p class="rank '+(val.rank<4?'rank0_3':'rank4_10')+'">第'+val.rank+'名</p>'):'')+
                        '<a href="./detail.html?uflag='+val.uflag+'">'+
                        '<p class="thum">'+
                        '<img src="'+val.avatar_pic+'">'+
                        '</p>'+
                        '<div class="info">'+
                        '<p>'+val.uname+'</p>'+
                        '<p>'+val.no+'</p>'+
                        '</div>'+
                        '</a>'+
                        '<p class="amount">'+
                        '<span class="vote-amount">'+val.vote+'</span>'+
                        '<span class="vote '+(val.voteable==1?'':'disabled')+'" data-flag="'+val.uflag+'">投票</span>'+
                        '</p>'+
                        '</div>'+
                        '</li>'
                })

            if (html=='') {
                $('#owner-box').children('.owner-item').eq(index).find('.no-data').show();
            } else {
                $('#owner-box').children('.owner-item').eq(index).find('.no-data').hide();
            }
            $('#owner-box').children('.owner-item').eq(index).children('ul').html(html);
        }
    });
}

getData(0,'cook');

$('.search').click(function (e) {
    var inputVal = $('.inp').val();
    if (liIndex === 0) {
        getData(0,'cook' ,inputVal)
    } else if (liIndex === 1) {
        getData(1,'pop' ,inputVal)
    } else if (liIndex === 2) {
        getData(2,'cloth' ,inputVal)
    }
    $('#owner-box').children('.owner-item').eq(liIndex).show().siblings('.owner-item').hide();
})

var liIndex = 0;

$('.menu li').click(function (e) {
    var index = $(this).index();
    liIndex = index;
    $(this).addClass('active').siblings().removeClass('active');
    $('#owner-box').children('.owner-item').eq(index).show().siblings('.owner-item').hide();
    $('#owner-box').children('.owner-item').eq(index).children('.loading').show();
    $('.inp').val('');
    if (index === 0) {
        getData(0,'cook')
    } else if (index === 1) {
        getData(1,'pop')
    } else if (index === 2) {
        getData(2,'cloth')
    }

});

$(document).on('click', '.check-rule', function(){
    $('.rule-wrap').show();
});

$(document).on('click', '.close-rule', function(){
    $('.rule-wrap').hide();
});

$(document).on('click','.vote',function (e) {
    if ($(this).hasClass('disabled')) {
        $('.tip').addClass('show').children('span').html('您今天已为TA投票，请明天再来给TA投吧~');
        setTimeout(function () {
            $('.tip').removeClass('show');
        }, 2000)
        return;
    }
    var flag = $(this).data('flag');
    var _this =  $(this);
    $.ajax({
        type : "POST",
        url : host+"vote.php/Index/voteTo",
        data : {
            uflag: flag,
            open_id: localStorage.openId
        },
        success : function(result) {
            if (result.status === 1) {
                var spanHtml = '<span class="and-one">+1</span>';
                _this.parent('.amount').append(spanHtml);
                _this.addClass('disabled');
                setTimeout(function () {
                    _this.siblings('.and-one').remove();
                }, 1000);
                var amount = _this.parent('.amount').find('.vote-amount');
                amount.html(parseInt(amount.html())+1);

            } else if (result.status === -1) {
                $('.tip').addClass('show').children('span').html(result.info);
                setTimeout(function () {
                    $('.tip').removeClass('show');
                    window.location.href=host+"WxAuthorize?front_url="+location.href;
                }, 2000)
            } else {
                $('.tip').addClass('show').children('span').html(result.info);
                setTimeout(function () {
                    $('.tip').removeClass('show');
                }, 2000)
            }

        }
    });
})
