mui.init({
    pullRefresh: {
        container: '#pullrefresh_chanpin_list',
        down: {},
        up: {
            contentrefresh: '正在加载...',
            callback: pullupRefresh
        }
    }
});
var search_str = "",
    pageNumber = 0,
    pagesize = 10,
    proType = "1";

function pullupRefresh() {
    pageNumber += 1;
    $.ajax(base_path + '/product/productIndex?dates=' + new Date().getTime(), {
        data: {
            "productName": search_str,
            "pageSize": pagesize,
            "pageNO": pageNumber,
            "productType": proType
        },
        dataType: 'json',
        type: 'post',
        timeout: 15000,
        xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
        success: function(dt) {
        	if (dt.isRedirect) {
				location.href = dt.redirectURL;
			} else {
            if(dt.status) {
                var chanpin_data = [];
                chanpin_data = dt.data.productList;
                mui('#pullrefresh_chanpin_list').pullRefresh().endPullupToRefresh(chanpin_data.length < pagesize);
                var table = document.body.querySelector('.my-tab-view-pull');
                search_str == "" ? search_str = "" : search_str;
                mui(chanpin_data).each(function(index) {
                    var pro_color = this.productColor;
                    var li = document.createElement('li');
                    var yuanjia = '<s class="gwc-pp">原价:¥' + accDiv(this.productPrice, 100) + '</s>';
                    if(marquee != '' && this.productDiscount == '328000') {
                        yuanjia = '<span class="mui-h6">购买即赠3年滤芯</span>'
                    }
                    var img='';
                    img=this.colorImg;
                    if($(".box-list-card .iconfont").hasClass("icon-list")) {
                        li.className = 'mui-table-view-cell mui-media';
                        li.innerHTML = '<a proid="' + this.productId + '" colorid="'+this.colorId+'" class="go_chanpin_details"><img class="mui-media-object mui-pull-left wh-100" src="' + img + '"><div class="mui-media-body gwc-tit">' + this.productName + '</div><div class="mui-ellipsis mui-h6">' + this.productColorDes + '</div><div class="shop-price">' + marquee + '<span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span>' + yuanjia + '</div></div></a>">';
                    } else {
                        li.className = 'mui-table-view-cell mui-media mui-col-xs-6';
                        li.innerHTML = '<a proid="' + this.productId + '" colorid="'+this.colorId+'" class="go_chanpin_details"><img class="mui-media-object" src="' + img + '"><div class="mui-media-body gwc-tit">' + this.productName + '</div><div class="mui-ellipsis mui-h6">' + this.productColorDes + '</div><div class="shop-price">' + marquee + '<span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span>' + yuanjia + '</div></a>';
                    }
                    table.appendChild(li)
                    mui('#pullrefresh_chanpin_list').pullRefresh().disablePullupToRefresh();
                });
                if(chanpin_data.length==0){
                    mui('#pullrefresh_chanpin_list').pullRefresh().disablePullupToRefresh();
                    var li = document.createElement('li');
                    li.className = 'mui-table-view-cell mui-media mui-col-xs-12 mui-text-center';
                    li.innerHTML='<img style="width:100px;display:block;margin:40px auto 0 auto;" src="images/null-4.png" /><h6 class="mui-text-center">暂无产品信息,逛逛别的产品吧!</h6>';
                    table.appendChild(li);
				}
            } else {
                mui('#pullrefresh_chanpin_list').pullRefresh().endPullupToRefresh(true)
            }
            }
        },
        error: function(xhr, type, errorThrown) {
            console.log(xhr)
        }
    })
};

function search_pullrefresh(str) {
    if(str != null && str != '') {
        if(!checkParameter(str)) {
            mui.alert('您输入的格式有误,请重新输入!')
        } else {
            pageNumber = 0;
            $('.my-tab-view-pull').empty();
            search_str = str;
            mui('#pullrefresh_chanpin_list').pullRefresh().refresh(true);
            mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading()
        }
    } else {
        pageNumber = 0;
        $('.my-tab-view-pull').empty();
        search_str = str;
        mui('#pullrefresh_chanpin_list').pullRefresh().refresh(true);
        mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading()
    }
};
//mui.os.ios ? listener_ipt() : keyup_fn();
//
//function keyup_fn() {
//  $('body').on('keyup', '#chanpin-search', function(e) {
//      var currKey = 0,
//          e = e || event;
//      currKey = e.keyCode || e.which || e.charCode;
//      var keyName = String.fromCharCode(currKey);
//      if(currKey == "13") {
//          var str = $.trim($('#chanpin-search').val());
//          search_pullrefresh(str)
//      }
//  })
//};
//document.getElementById('chanpin-search').addEventListener('input', function(e) {
//  var value = e.target.value
//});

//function listener_ipt() {
//  document.getElementById('chanpin-search').onblur = function() {
//      var str = $.trim($('#chanpin-search').val());
//      search_pullrefresh(str)
//  }
//};
mui.ready(function() {
    proType = getParam("proType");
    proType == null ? proType = "1" : proType;
    //springFestivalPop();
    marquee_Show();
    if(proType != 1) {
        $(".tab-with-chanpin-list .icon-shuiji").parent().removeClass('mui-active');
        switch(proType) {
            case "11":
                console.log("11")
                $(".tab-with-chanpin-list .icon-dingzhi").parent().addClass('mui-active');
                break;
            case "2":
                $(".tab-with-chanpin-list .icon-lvxin").parent().addClass('mui-active');
                break;
            default:
                $(".tab-with-chanpin-list .icon-machine").parent().addClass('mui-active');
                break
        }
    }
    mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading();
    getCartNum();
    document.querySelector('.loading-bg').style.display = 'none'
});

function checkParameter(remark) {
    if(!/^[\u4e00-\u9fa5_a-zA-Z0-9-]+$/.test(remark)) {
        return false
    }
    return true
};
$(document).on('tap', '.box-list-card', function() {
    var listcard = $(this).children().first();
    if(listcard.hasClass("icon-list")) {
        listcard.removeClass("icon-list").addClass("icon-list-card");
        $(".my-tab-view-pull").removeClass("mui-table-view-chevron").addClass("mui-grid-view");
        $(".mui-media-object").removeClass("mui-pull-left wh-100");
        $(".my-tab-view-pull .mui-table-view-cell").addClass("mui-col-xs-6")
    } else {
        listcard.removeClass("icon-list-card").addClass("icon-list");
        $(".my-tab-view-pull").removeClass("mui-grid-view").addClass("mui-table-view-chevron");
        $(".mui-media-object").addClass("mui-pull-left wh-100");
        $(".my-tab-view-pull .mui-table-view-cell").removeClass("mui-col-xs-6")
    }
});
mui.each(document.querySelectorAll('.tab-with-chanpin-list .mui-control-item'), function(index, el) {
    el.addEventListener('tap', function() {
        pageNumber = 0;
        switch(index) {
            case 0:
                proType = 1;
                break;
            case 1:
                proType = 13;
                break;
            case 2:
                proType = 14;
                break;
            default:
                proType = 2;
                break
        }
        $('.my-tab-view-pull').empty();
        mui('#pullrefresh_chanpin_list').pullRefresh().refresh(true);
        mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading()
    }, false)
});