mui.init({
	pullRefresh: {
		container: '#pullrefresh_chanpin_list',
		down: {
			//callback: pulldownRefresh
		},
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});
var search_str = "",pageNumber = 0,pagesize = 10;
//上拉加载
function pullupRefresh() {
	++pageNumber;
	var btnArray = ['否', '是'];
	mui.ajax(base_path + '/product/productIndex?dates=' + new Date().getTime(), {
		data: {
			"productName": search_str,
			"pageSize": pagesize,
			"pageNO": pageNumber
		},
		dataType: 'json',
		type: 'post',
		timeout: 15000,
		success: function(dt) {
			//console.log(dt);
			if(dt.status) {
				var chanpin_data = [];
				chanpin_data = dt.data.productList;
				mui('#pullrefresh_chanpin_list').pullRefresh().endPullupToRefresh(chanpin_data.length < pagesize); //参数为true代表没有更多数据了
				var table = document.body.querySelector('.my-tab-view-pull');
				search_str == "" ? search_str = "" : search_str;
				mui(chanpin_data).each(function(index) {
					/*var pro_color = switchcolor(this.productColor)*/
                    var pro_color = this.productColor;
					var li = document.createElement('li');
					li.className = 'mui-table-view-cell mui-media';
					li.innerHTML = '<a proid="' + this.productId + '" class="go_chanpin_details"><span class="mui-media-object mui-pull-left wh-100"><img src="' + this.productIntroduceImg + '"/></span><div class="mui-media-body"><div class="gwc-tit">' + this.productName + '<small class="gwc-pp">(' + pro_color + ')</small></div><div class="shop-price"><span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span><s class="gwc-pp">原价：¥' + accDiv(this.productPrice, 100) + '</s></div></div><div class="btn-box"><a class="mui-btn my-btn-orange" style="background-color:#c7c7cc;border: 1px solid #c7c7cc;color:#fff;" proid="' + this.productId + '">加入购物车</a><a class="mui-btn my-btn-danger go-clearing" style="background-color:#c7c7cc" proid="' + this.productId + '">立即购买</a></div></a>';
//					if(this.productId == 28) {
//						li.innerHTML = '<a proid="' + this.productId + '" class="go_chanpin_details"><span class="mui-media-object mui-pull-left wh-100"><img src="' + this.productIntroduceImg + '"/></span><div class="mui-media-body"><div class="gwc-tit">' + this.productName + '<small class="gwc-pp">(' + pro_color + ')</small></div><div class="shop-price"><span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span><s class="gwc-pp">原价：¥' + accDiv(this.productPrice, 100) + '</s></div></div><div class="btn-box"><a class="mui-btn my-btn-orange" style="background-color:#c7c7cc;border: 1px solid #c7c7cc;color:#fff;" proid="' + this.productId + '">加入购物车</a><a class="mui-btn my-btn-danger" style="background-color:#c7c7cc" proid="' + this.productId + '">立即购买</a></div></a>';
//					} else {
//						li.innerHTML = '<a proid="' + this.productId + '" class="go_chanpin_details"><span class="mui-media-object mui-pull-left wh-100"><img src="' + this.productIntroduceImg + '"/></span><div class="mui-media-body"><div class="gwc-tit">' + this.productName + '<small class="gwc-pp">(' + pro_color + ')</small></div><div class="shop-price"><span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span><s class="gwc-pp">原价：¥' + accDiv(this.productPrice, 100) + '</s></div></div><div class="btn-box"><a class="mui-btn my-btn-orange go_gwc_pro" proid="' + this.productId + '">加入购物车</a><a class="mui-btn my-btn-danger go-clearing" proid="' + this.productId + '">立即购买</a></div></a>';
//					}
					table.appendChild(li);
				});
			} else {
				mui.confirm(dt.message, '爱贝源-提示', btnArray, function(e) {
					if(e.index == 1) {
						//location.reload(true);
					}
				});
			}
		},
		error: function(xhr, type, errorThrown) {
			errorfn(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") {
			//console.log("按键码: " + currKey);
			//传参数并加载数据
			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() {
		//console.log(this.value);
		//传参数并加载数据
		var str = $.trim($('#chanpin-search').val());
		search_pullrefresh(str);
	}
}
mui.ready(function() {
	mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading();
	document.querySelector('.loading-bg').style.display = 'none';
});

function checkParameter(remark) {
	if(!/^[\u4e00-\u9fa5_a-zA-Z0-9-]+$/.test(remark)) {
		return false;
	}
	return true;
}