$(".nnewshoulu_code").live('click', function(e) {
	addCode()
});
$("#login").live("click", function() {
	var uname = $.trim($("#user").val());
	var pwd = $.trim($("#password").val());
	var imgcode = $.trim($("#imgcode").val());
	if(uname.length == 0 || pwd.length == 0 || imgcode.length == 0) {
		alert("请将信息填写完整!")
	} else {
		$.ajax({
			type: "post",
			url: "https://www.daohangtx.com/config/mer_manager/mer_login.php",
			data: {
				"gname": uname,
				"gpassword": pwd,
				"imgcode": imgcode
			},
			datatype: "json",
			success: function(res) {
				var a = eval("(" + res + ")");
				if(a.mid == -1) {
					alert(a.msg)
				} else {
					alert("欢迎回来：" + a.name);
					setCookie('name', a.name, 'd2');
					location.reload()
				}
				addCode();
			},
			error: function() {
				alert("提交错误！")
			}
		})
	}
});
var flpage='',newpage='',seapage='';
function appendInfo(data) {
	var arr = eval(data);
	var box = $(".shangjia_list_ul");
	var li = "";
	if(data.data.lenght<1) {
		box.html("<li style='line-height:50px;'>暂无待处理数据</li>");
		return;
	}
	$("#no_fh").html(data.notship);
	$("#u_info").html(data.usebind);
	$("#fh_count").html(data.isship);
	 newpage=$(".active").data("type");
	if(flpage!=newpage){ 
		pages(data.pages);
		flpage=newpage;
	};
	if(seapage!=query){ 
		pages(data.pages);
		seapage=query;
	};
	var now = new Date();
	var ntm=now.getTime()/1000;
	$.each(data.data, function(i, res) {
		var use_ace = [];
		
		if(res.qq != null) {
			use_ace.push(res.qq);
		} else if(res.alipay != null) {
			use_ace.push("(支付宝):"+res.alipay);
		} else if(res.weixin != null) {
			use_ace.push("(微信):"+res.weixin);
		};
		
		var fh='',sta='',usely='',ly_sta='',yhzh='',lock='',lockcss='';
		 if(res.use_leave!=null){
			usely= decodeURI(res.use_leave);
			ly_sta='tip'; } 
		if((ntm-res.prize_date)>86400){
			lock='false';
		}else{
			lock='true';
			lockcss='lock';
		}
		if(res.isship == "n"){
			fh='<a class="caozuo_fahuo" data-pid="' + res.pid + '">确认发货</a><a class="cancel '+lockcss+'" data-lock="'+lock+'"  data-pid="' + res.pid + '">取消发货</a>' ;
			sta='<b><font color="#f60">未发货</font></b>' ;
		}else if(res.isship == "qx"){
			fh = '<a class="changecolor">已取消</a>';
			sta='<b><font color="#550">已取消发货</font></b>' ;
		}else{
			fh = '<a class="changecolor">已发货</a>';
			sta='<b><font color="#888">已发货</font></b>' ;
		};
		if(usely!=''){
			yhzh='<font color="#f60">'+use_ace[0]+'</font>';
		}else{
			yhzh=use_ace[0];
		}
		 
		li += '<li><span class="newshangjia_list_xuhao">' + (i + 1) + '</span>';
		li += '<span class="newshangjia_list_user">' + (res.use_account == null ? '<font color="#999">暂未绑定</font>' : res.use_account) + '</span>';
		li += '<span class="newshangjia_list_shop">' + res.shop_name + '</span>';
		li += '<span class="newshangjia_list_qqnum '+ly_sta+'" data-usely="'+usely+'" >' + yhzh + '</span>';
		li += '<span class="newshangjia_list_stat">' + $.formatTime.UnixToDate(parseInt(res.prize_date)+28600) + '</span>';
		li += '<span class="newshangjia_list_stat">' + sta+ '</span>';
		li += '<span class="newshangjia_list_caozuo">' + fh + '</span></li>';
	});
	li += '<div style="clear:both"></div>';
	box.html(li)
}
$('.tip').live('mouseover',function(){
	if($(this).data('usely')!=''){
		layer.tips($(this).data('usely'),$(this), { tips:1});	
	}
});
$('.tip').live('mouseleave',function(){
	layer.closeAll();
});

$(".newshangjia_list_caozuo .cancel").live('click',function(){
	var fha = $(this);
	 if(fha.data('lock')){
	 	layer.tips("中间时间未超过一天不能取消",$(this), { tips:1});	
	 	return ;
	 }
	var pid = $(this).data('pid');
	layer.prompt({
	  formType: 2,
	  value: '尊敬的用户,您的奖品没能送出,可能有以下原因:未在24小时内绑定账号',
	  title: '反馈客户不能发货原因(<font color="red">限制300字</font>)',
	  maxlength: 300,
	   btn: ['提交', '取消']
	}, function(value, index, elem){
		loading(2);
		$.getJSON("https://www.daohangtx.com/config/mer_manager/spfh.php", "qx=qx&pid=" + pid+"&content="+value, function(res) {
		layer.closeAll();
		if(res.mid = 1) {			 
			
			fha.parent().parent().find(":contains('未发货')").html("<b style='color:#550'>已取消发货</b>");
			fha.parent().html("<a class='changecolor'>已取消</a>");
			layer.msg(res.msg,{icon:6,anim:5});
			layer.close(index);
		} else {
			layer.alert(res.msg,{icon:5,anim:5});
		}
			 
		});
	});
});

$(".newshangjia_list_caozuo .caozuo_fahuo").live("click", function(e) {
	if($(this).hasClass("changecolor")) {
		return
	};
	var fha = $(this);
	
	var pid = $(this).data('pid');
	layer.prompt({
	  formType: 2,
	  value: '尊敬的用户,您的奖品定单成功已发货,请前往查看',
	  title: '发货反馈(<font color="red">限制200字</font>)',
	  maxlength: 200,
	   btn: ['确认提交', '取消']
	}, function(value, index, elem){
		loading(2);
		$.getJSON("https://www.daohangtx.com/config/mer_manager/spfh.php", "pid=" + pid+"&content="+value, function(res) {
		layer.closeAll();
		if(res.mid = 1) {
			fha.parent().parent().find(":contains('未发货')").html("<b style='color:#888'>已发货</b>");
			fha.parent().html("<a class='changecolor'>已发货</a>");
			layer.msg(res.msg,{icon:6,anim:5});
			layer.close(index);
		} else {
			layer.alert(res.msg);
		}
			 
		});
	});
	

	 
});

$(".newshangjia_caozuo a").live("click",function(){
	query='';
	if($(this).hasClass('active')){
		return;
	}
	$(".newshangjia_caozuo a").removeClass();
	$(this).addClass("active");
	var tp=$(this).data('type');
	if(tp=='out'){
		ck=['accid','name','tpcode'];
		$.get('del.php.htm'/*tpa=https://www.daohangtx.com/config/del.php*/,'cookie='+ck.toString(),function(res){location.reload();});
	}else{
		getlist(1,15,$(this).data('type'),'');
	}
})
var query="";
function pages(num) {
	 
	laypage({
		cont: 'page_box',
		pages: num,
		groups:3,
		skip: true,
		last: num,
		first: 1,
		jump: function(obj, first){
			if(!first){	
				
				if(query!=''){
						getlist(obj.curr,15,$(".newshangjia_caozuo .active").data('type'),query);
				}else{
						getlist(obj.curr,15,$(".newshangjia_caozuo .active").data('type'),'');
				}
			 }
		}
	})
}
function addCode() {
	var d = new Date();
	var time = d.getTime();
	$(".nnewshoulu_code").attr("src", "/config/sendmail/img_fontcode.php?data=" + time)
}

function pingbi() {
	$(".newbg").append('<div class="new_sjhtceng"></div>')
}

function closepingbi() {
	$(".newzuiwaiceng").eq(0).remove()
}

function iflogin() {
	if(getCookie('tpcode') != null && getCookie("accid") != null) {
		return true
	} else {
		return false
	}
}
var loading = function(type) {
	layer.load(type, {
		shade: [0.1, '#000']
	})
};


$(".newshangjia_search_p").on('click', function(e) {
	if($(".newshangjia_search_tan").is(":hidden")){
		$(".newshangjia_search_tan").show()
	} else {
		$(".newshangjia_search_tan").hide();
	}
	e.stopPropagation();
});
$(document).click(function(e) {
	$(".newshangjia_search_tan").slideUp(100)
});

$(".newshangjia_search_tan li").on('click',function(e) {
	$(".newshangjia_search_p span").html($(this).html());
	$(".newshangjia_search_p span").attr('data-cls',$(this).data('cls'));
	$(".newshangjia_search_tan").show();

});

$(".newshangjia_search_btn").live('click',function(){
	var cols=$(".newshangjia_search_p span").attr('data-cls');
	var cval=$(".newshangjia_search_text").val();
	query = '&query={"cols":"'+cols+'","cval":"'+cval+'"}';
	 getlist(1,15,'',query);
	 
});


function getlist(page,row,tp,quy){
	loading(3);
	$.getJSON("https://www.daohangtx.com/config/mer_manager/getPrize.php","page="+page+"&rows="+row+"&type="+tp+quy, function(res) { layer.closeAll(); if(res != null) { appendInfo(res); } });
}
window.onload = function() {
	if(!iflogin()) {
		pingbi();
		$(".newshangjia_login").show();
	} else {
		getlist(1,15,$(".newshangjia_caozuo .active").data('type'),'');
		$("#sj_name").html(getCookie("name"));
	}
}