/**
 * 管理中心
 * */

var page = 1; //页数
var pageSize = 10; //每页显示条目
var total; //数据总条数
var Type_ = 1;
var keyw;
var name = helper.request.queryString("name"); //姓名
var tel = helper.request.queryString("tel"); //受理编号

mui.init({
	swipeBack: false,
	pullRefresh: {
		container: '#pullrefresh',
		up: {
			contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: ' ', //可选，请求完毕若没有更多数据时显示的提醒内容；
			//contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: pullupRefresh
		}
	}
});

Zepto(function($) {
	//获取 工单列表
	getOrderList();
	mui('.mui-scroll-wrapper').scroll({
		deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
	});

	document.querySelector('.mui-slider').addEventListener('slide', function(event) {
		var itemIndex = event.detail.slideNumber;
		loadDatas(itemIndex, 'slide');
	});

	//查找
	$('.go_search').on('input focus', function() {
		keyw = $(this).val();
		page = 1;
		getOrderList();
	});

	//更多
	$('.turnGo').on('tap', function() {
		window.location.href = "WorkSearch.html?Tye=" + 2;
	});
	
	//个人中心
	$('#usercode').text(usercode);
	$("#signout").on('tap', function() {
		var btnArray = ['取消', '确定'];
		mui.confirm('您确定要退出吗？', ' ', btnArray, function(e) {
			if(e.index == 1) { //确定
				helper.cookies.set("usercode", "");
				f_close();//关闭当前页面
			}
		});
	});
});

function loadDatas(index, handle) {
	if(handle === "slide") {
		switch(index) {
			case 0:
					page = 1; //页数
					getOrderList();
				break;
			case 1:
					mui('#pullrefresh').pullRefresh().refresh(true); //重置上拉加载
				break;
			default:
				break;
		}
	}
}

//上拉加载
function pullupRefresh() {
	$(".order_lists .mui-pull").show();
	setTimeout(function() {
		mui('#pullrefresh').pullRefresh().endPullupToRefresh((page++ >= Math.ceil(total / 10)));//参数为true代表没有更多数据了。
		$.ajax({
			url: huayi.config.callcenter_url + 'WxLogin/GetList',
			data: {
				openid: helper.cookies.get("openid"), //微信openid
				key: keyw,
				page: page,
				pagesize: pageSize,
				name: name, ///联系人
				tel: tel, //联系电话
				//							state: state,
				//							starttime: starttime, //
				//							endtime: endtime, //
				//							workid: workid, //工单编号
				//							source: source, //工单来源
				//							infotype: infotype, //信息分类
			},
			async: true,
			dataType: 'json',
			type: 'get', //HTTP请求类型
			success: function(data) {
				mui('#pullrefresh').pullRefresh().refresh(true); //重置上拉加载
				$(".order_lists .mui-pull").hide();
				total = data.total;
				$(data.rows).each(function(i, n) {
					var States = n.State;
					var html = ''
					var Html = '';
					if(States < 2) { //0,1 待完成  2已完成
						Html = '<div class = "mui-slider-right mui-disabled" >' +
							'<a class = "mui-btn mui-btn-grey" > 处理 </a></div >' +
							'<div class = "mui-slider-handle" >'
						html = '<h5 class="wait" style="text-align:right;"><span>待完成</span></h5>'
					} else {
						html = '<h5 class="success" style="text-align:right;"><span>已完成</span></h5>'
					}

					$('<li class="mui-table-view-cell  order" data-index="' + n.ID + '">' +
						'' + Html + '' +
						'<div class = "mui-table-cell mui-col-xs-10" > ' +
						'<h4 class = "mui-ellipsis" > ' + n.Detail + ' </h4> ' +
						'<h5 > <span>联系人：' + n.Customer + '</span>' +
						'</h5>' +
						'</div >' +
						'</div> ' +
						'' + html + '' +
						'</li > ').appendTo('#OA_task_2 ');
				});
//				mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
//				if(newtotal === 0){
//					mui.toast("没有更多数据了。");
//					page = Math.ceil(total / 10) - 1;
//				}else{
//					page = Math.ceil(total / 10) - 1;
//					mui.toast("为你加载了 "+ newtotal +" 条数据。");
//				}	
				//去工单详情
				$('.order').on('tap', function() {
					var index = $(this).attr('data-index');
					window.location.href = "Schedule.html?id=" + index + '&isDeal=' + 1;
				});

			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
			}
		})

	}, 1000);
}


//获取工单列表
function getOrderList() {
	$(".order").remove();
	$(".order_lists .mui-pull").hide();
	$.ajax({
		url: huayi.config.callcenter_url + 'WxLogin/GetList',
		data: {
			openid: helper.cookies.get("openid"), //微信openid	,
			key: keyw,
			page: page,
			pagesize: pageSize,
			name: name, ///联系人
			tel: tel, //联系电话
			//						state: state,
			//						starttime: starttime, //
			//						endtime: endtime, //
			//						workid: workid, //工单编号
			//						source: source, //工单来源
			//						infotype: infotype, //信息分类
		},
		async: true,
		dataType: 'json',
		type: 'get', //HTTP请求类型
		success: function(data) {
			if(document.getElementById('hy_loading')) {
				document.getElementById('hy_loading').style.opacity = 0;
				document.getElementById('hy_loading').remove();
			}
			total = data.total;
			$(data.rows).each(function(i, n) {
				var States = n.State;
				var html = ''
				var Html = '';
				if(States < 2) { //0,1 待完成  2已完成
					Html = '<div class = "mui-slider-right mui-disabled" >' +
						'<a class = "mui-btn mui-btn-grey"> 处理 </a></div >' +
						'<div class = "mui-slider-handle">'
					html = '<h5 class="wait" style="text-align:right;"><span>待完成</span></h5>'
				} else {
					html = '<h5 class="success" style="text-align:right;"><span>已完成</span></h5>'
				}

				$('<li class="mui-table-view-cell  order" data-index="' + n.ID + '">' +
					'' + Html + '' +
					'<div class = "mui-table-cell mui-col-xs-10" >' +
					'<h4 class = "mui-ellipsis" >' + n.Detail + ' </h4> ' +
					'<h5 > <span>联系人：' + n.Customer + '</span>' +
					'</h5>' +
					'</div>' +
					'</div> ' +
					'' + html + '' +
					'</li >').appendTo('#OA_task_2');
			})

			//工单跳转 工单详情
			$('.order').on('tap', function() {
				var index = $(this).attr('data-index');
				window.location.href = "Schedule.html?id=" + index + '&isDeal=' + 1;
			});

		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
		}
	})
}

//左滑处理
(function($) {
	var btnArray = ['确认', '取消'];
	//第二个demo，向左拖拽后显示操作图标，释放后自动触发的业务逻辑
	$('#OA_task_2').on('slideleft', '.mui-table-view-cell', function(event) {
		var elem = this;
		var Workindex = elem.getAttribute("data-index");
		mui.confirm('确认要处理此工单吗？', ' ', btnArray, function(e) {
			if(e.index == 0) {
				window.location.href = "Workdeal.html?id=" + Workindex;
			} else {
				setTimeout(function() {
					$.swipeoutClose(elem);
				}, 0);
			}
		});
	});
})(mui);

//关闭页面
function f_close() {
	if(typeof(WeixinJSBridge) != "undefined") {
		//WeixinJSBridge.call('closeWindow');  
		WeixinJSBridge.invoke('closeWindow', {}, function(res) {
			//alert(res.err_msg);
		});
	} else {
		if(navigator.userAgent.indexOf("MSIE") > 0) {
			if(navigator.userAgent.indexOf("MSIE 6.0") > 0) {
				window.opener = null;
				window.close();
			} else {
				window.open('', '_top');
				window.top.close();
			}
		} else if(navigator.userAgent.indexOf("Firefox") > 0) {
			window.location.href = 'about:blank ';
		} else {
			window.location.href="about:blank";
			window.close();
			/*window.opener = null;
			window.open('', '_self', '');
			window.close();*/
		}
	}
}


	