
$('._showDetail_').on('click', function () {
    var that = this;
    //var t = $(that).attr('data-type') //资源类型
    var params = {};
    params.theUrl = allAPI.advert.show + '/' + $(that).data("resid")
    doGetAjax(params, function (data) {
        if (data.code == 200) {
            var info = data.data.info,type = info.type[0].tag;
            var t = $(that).attr('data-type')
            $('.headImg .list-img').attr('src', info.logo).attr('alt', info.name).css({
                "width": "100px"
            });
            //软文
            $('.infos .resId').html(info.id);
            $('.infos .resName').html(info.name);
            $('.infos .prices b').html(info.quota_min);
            $('._addQuote_ .resId').data('resid', info.id);//success_rate
            if(type == 'article'||type == 'zimeiti') {
                $('.success_rate').html(info.draft+'%');
                $('.success_time').html(info.success_rate);
                $('.website-name').html(info.name);
                $('.website-type').html(info.website[0]?info.website[0]['name']:'无');
                $('.website-collect').html(info.collect[0]?info.collect[0]['name']:'无');
                $('.website-material').html(info.material[0]?info.material[0]['name']:'无');
                $('.website-entry').html(info.from_type?info.from_type:'无');
                $('.channel-name').html(info.name);
                $('.apply-condition .tougao-date').html(info.accept_type);
                $('.apply-condition .img-count').html(info.image_count);
                $('.apply-condition .link-type').html(info.link_type);
                $('.website-fans').html(info.fans?info.fans.fans+'万':'无');
                $('.website-read').html(info.views?info.views.views+'万':'无');
                setTimeout(function(){
                    showVipHistogram(data.data.levels);
                },800)
            }else if(type == 'weibo' || type == 'weixin'){
                $('.website-fans').html(info.fans?info.fans.fans+'万':'无');
                $('.website-read').html(info.views?info.views.views+'万':'无');
                $('.quoted-price table tbody .quota-min').html('￥'+info.quota_min);// 硬广
                $('.quoted-price table tbody .quota-max').html('￥'+info.quota_max);// 软广
                if(info.material.length) {
                    $('.website-material').html(info.material[0]?info.material[0]['name']:'-');
                }
                $('.apply-condition').html(info.apply_condition);
            }else if( type == 'video'){
                $('.quoted-price table tbody .quota-min').html('￥'+info.quota_min);// 预约价格
                $('.website-fans').html(info.fans?info.fans.fans+'万':'无');
                $('.website-views').html(info.views?info.views.views+'万':'无');
                $('.website-video').html(info.video[0]?info.video[0]['name']:'-');
                if(info.material.length) {
                    $('.website-material').html(info.material[0]?info.material[0]['name']:'');
                }
                $('.apply-condition').html(info.apply_condition);
            }else if(type == 'redbook'){
                $('.website-fans').html(info.fans?info.fans.fans+'万':'无');
                $('.website-read').html(info.views?info.views.views+'万':'无');
                $('.quoted-price table tbody .quota-min').html('￥'+info.quota_min);// 笔记价格
                $('.quoted-price table tbody .quota-max').html('￥'+info.quota_max);// 视频价格
                if(info.material.length) {
                    $('.website-material').html(info.material[0]?info.material[0]['name']:'无');
                }
                $('.apply-condition').html(info.apply_condition);
            }
            $('.resOrderCount').html(info.orders);
            //info.draft+'%'
            //
            $('.anli').html('<a href="'+info.redirect_url+'" target="_blank">'+info.redirect_url.substring(0,30)+'...</a>');
            // 显示详情弹框
            $(that).parents('.wrap-container').parent().next().show().find('.close').on('click',function(){
                $(this).parents('#detailPop').hide()
            })
        } else {
            layer.msg(data.info, {
                icon: 6
            });
        }
    })
})
var addcart_flag = false
// ========添加资源到购物车========
$('._addQuote_').on('click', function () {
    var that = this;
    var params = {};
    if(addcart_flag){
        return
    }
    addcart_flag = true;
    params.theData = {
        id: $(that).data('resid'),
        'type': $('#advert-head').data('type')
    };
    params.theUrl = allAPI.cart.addCart;
    doPostAjax(params, function (data) {
        setTimeout(function(){addcart_flag = false;},1200)
        if (data.code == 200) {
            $('.carCount').html(data.data.number);
            getCartData();
            $msg(data.info,1)
        } else {
            $msg(data.info,2)
        }
    })
})

$('#rCar').on('click', function () {
    var $car_box = $('.carBox');
    if ($car_box.hasClass('tran03')) {
        $car_box.removeClass('tran03');
    } else {
        $car_box.addClass('tran03');
    }
})
$(document.body).on('click', function () {
    $('.carBox').removeClass('tran03');
})
// 阻止右边购物车事件冒泡
$('.rCarBox').on('click', function (event) {
    event.stopPropagation();
})

// 获取购物车数据
function getCartData(){
    var boo = $('#hard-soft').find("input[type='radio']:checked").val()==='max';
    var params = {};
    params.theData = {
        'type': $('#advert-head').data('type')
    };
    params.theUrl = allAPI.cart.listCart;
    doPostAjax(params, function (data) {
        if (data.code == 200) {
            var info = data.data;
            var alistr = '';
            Object.keys(info).forEach(function (key) {
                var item = info[key];
                alistr +=
                    '<li class="item ptb20 bb-e0e0e0 bgff pcub pcub-ac">' +
                        '<div class="pcub-f1 pcub pcub-ac">'+
                            '<div class="checkbox_ mlr10" style="width: 20px;" data-i="'+item.id+'" data-viprate="'+(item.product.sale)+'" data-p="'+(boo?item.product.quota_max:item.product.quota_min)+'" data-n="'+item.number+'">'+
                                '<input checked class="io" type="checkbox" name="type" id="adviceRadio' + item.id + '" value="' + item.id + '" hidden />' +
                                '<label onclick="sum(this)" for="adviceRadio' + item.id + '" class="advice"></label>' +
                            '</div>'+
                                //'<img class="img" src="/images/loho.png" alt="">' +
                            '<img class="img" src="' + item.product.logo + '" alt="">' +
                            '<div class="product ml10">' +
                                '<p class="pcub-te">' + item.product.name + '</p>' +
                                '<p class="c-f2752e" >￥' + (boo?item.product.quota_max:item.product.quota_min) + '</p>' +
                            '</div>' +
                            '<div class="" style="width: 20px;text-align">x ' + item.number + '</div>' +
                        '</div>'+
                        '<div style="width:30px"><span class="del" onclick="del(this)" data-id="'+item.id+'"></span></div>' +
                    '</li>';
            })
            $(".carScroll ul").html(alistr);
            sum();// 计算价格
        } else {
            $(".carScroll").html('<ul><p class="item ptb20 pcub pcub-pac">'+data.info+'</p></ul>')
            $('.b-price').find('.number').html('￥0')
        }
    })
}
// 创建下单选择的价格类型 
createCarType()
function createCarType(){
    var model = {
        'article':[],
        'zimeiti':[],
        'weixin':[
            {name:'硬广价格',domid:'wechatHard'},
            {name:'软广价格',domid:'wechatSoft'}
        ],
        'weibo':[
            {name:'硬广价格',domid:'wechatHard'},
            {name:'软广价格',domid:'wechatSoft'}
        ],
        'redbook':[
            {name:'笔记价格',domid:'wechatHard'},
            {name:'视频价格',domid:'wechatSoft'}
        ],
        'video':[
            {name:'预约价格',domid:'wechatHard'}
        ],
    }[$('#advert-head').attr('data-type')];
        var html =  '<input type="radio" class="hide hard" name="wechatHS" id="wechatHard" value="min" checked="">'+
                    '<input type="radio" class="hide soft" name="wechatHS" id="wechatSoft" value="max">';
        if(model.length){
            model.forEach(function(e){
                html +=  ('<label for="'+e.domid+'">'+e.name+'</label>');
            })
        }
        $('#hard-soft').html(html).find("input[type='radio']").change(function (){
            getCartData();
        });
    
    getCartData();
}
// 删除 购物车 商品
function del(e){
    var params = {};
    params.theData = {id:$(e).data('id')};
    params.theUrl = allAPI.cart.delCart;
    doPostAjax(params,function(data){
        if(data.code == 200) {
            getCartData()
            if(data.data.number) {
                $('.carCount').html(data.data.number);
            }else{
                $('.carCount').html(0);
            }
        }else{
            $msg(data.info,2);
        }
    })
}
// 计算选中的 价格
function sum(r){
    var ids = [],total_price = 0,all_btn = false,sale_price = 0;
    setTimeout(function(){
        $('.carScroll').find('.io').each(function(){
            var item = $(this).parent('.checkbox_')
            if($(this).prop('checked')){
                total_price += (+item.attr('data-p'))*(+item.attr('data-n'))
                sale_price += (+item.attr('data-p'))*(1-(+item.attr('data-viprate'))/100)*(+item.attr('data-n'))
                ids.push(
                    //item.attr('data-i')+'_'+item.attr('data-n')
                    item.attr('data-i')
                )
            }else{
                all_btn = true
            }
        })
        if(all_btn){
            $('.allcheck').prop('checked',null)
        }else{
            $('.allcheck').prop('checked','checked')
        }
        if($('#advert-head').attr('data-type')==='article'||$('#advert-head').attr('data-type')==='zimeiti'){
            // data-viprate
            $('.b-price').find('.putongjia').addClass('hide').siblings('.huiyuanjia').removeClass('hide');
            $('.number1').html('￥'+total_price)
            $('.number2').html('￥'+sale_price).parent().parent().siblings('.btnprice').attr('data-ids',ids.join(','))
        }else{
            $('.b-price').find('.number').html('￥'+total_price).parent().siblings('.btnprice').attr('data-ids',ids.join(','))
        }
    },100)
}
// 全选
function cAll(e){
    setTimeout(function(){
        var boo = $(e).siblings("input").prop('checked');
        if(boo){
            $('.carScroll').find('.io').each(function(){
                $(this).prop('checked','checked')
            })
        }else{
            $('.carScroll').find('.io').each(function(){
                $(this).prop('checked',null)
            })
        }
        sum()
    },100)
}
// ============下单==============
function btnOrder(e){
    if($('.carCount').html()>0){
        window.location.href = allAPI.order.create+'/'+$('#advert-head').data('type')+'?ids='+$(e).attr('data-ids')+'&type_name='+$('#hard-soft').find("input[type='radio']:checked").val()
    }else{
        $msg('请添加资源',2);
    }
}

// ============检索radio的checked值 并跳转搜索==============
function batchSearch(e){
    var arr = [];
    $('._filter_.search1').find("input[type='radio']:checked").each(function(){
        if($(this).val()){
            arr.push(
                $(this).attr('name')+'='+$(this).val()
            )
        }
    })
    e && arr.push('keywords=' + e)
    getUrlParam('keywords') && arr.push('keywords=' + getUrlParam('keywords'))
    window.location.href = window.location.pathname+'?'+arr.join('&');
}
// ==========监听_filter_ radio改变===================
$('._filter_.search1').find("input[type='radio']").change(function (){
    batchSearch()
});

function keywordsSearch(){
    var keywords =  trim($('.zst-search input').val());
    if (keywords) {
        batchSearch(keywords)
    }else{
        $msg('请输入搜索内容',2);
    }
}
// ==========获取帅选条件并展示-=================
_filter()
function _filter(){
    
    var arr = [];
    $('._filter_.search1').find("input[type='radio']:checked").each(function(){
        if($(this).val()){
            var n = $(this).parent().parent().parent().attr('data-tit'),
                t = $(this).attr('name'),
                v = trim($(this).siblings('b').html())
            arr.push(
                {
                    type:t,
                    name:n,
                    val:v
                }
            )
        }
    })
    var html = '<div class="tit"><span>已选择：</span></div><div class="con clear fz14">',ishtml = false;
    if(arr.length>0){
        arr.forEach(function(e){
            html+='<div class="sd"><i class="del" data-name='+e.type+' onclick="delTit(this)">×</i>'+e.name+'<span class="ml10">'+e.val+'</span></div>'
        })
        ishtml = true;
    }
    if(getUrlParam('keywords')){
        $('.zst-search input').val(getUrlParam('keywords'))
        html+='<div class="sd"><i class="del" data-name=keywords onclick="delTit(this)">×</i>关键字<span class="ml10">'+getUrlParam('keywords')+'</span></div>'
        ishtml = true;        
    }
    html+='</div>'
    if(ishtml){
        $('.zst-filter ._filter_ .filters.selected').html(html);
    }
    
}
function delTit(e){
    var searchStr = window.location.search.split('?')[1];
    var searchArr = searchStr&&searchStr.split('&')||[];
    var ob = {},arr=[];
    for (var i = 0; i < searchArr.length; i++) {
        var curParam = searchArr[i].split('=');
        ob[curParam[0]] = curParam[1];
    }
    ob[$(e).attr('data-name')] ='';
    
    for(var k in ob){
        if(ob[k]){
            arr.push(
                k+'='+ob[k]
            )
        }
    }
    window.location.href = window.location.pathname+'?'+arr.join('&');
}
//去左右空格;
function trim(s){
    return s.replace(/(^\s*)|(\s*$)/g, "");
}