apiready = function() {
	var ctrl = {
		init : {}, //初始化
		bind : {}, //绑定事件
		toast : {}, //弹出提示框
		getOperateTicket : {}, //获取巡检日志
		renderOperateTicket : {}, //渲染巡检日志
		getLogDom : {},
		currentPage: 1
	}

	ctrl.init = function() {
		ctrl.bind();
		ctrl.getOperateTicket();
	}

	//判断是不是在体验模式
	ctrl.isCus = function(){
		if($api.getStorage('cus')){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 弹出提示框
	 */
	ctrl.toast = function(msg) {
		api.toast({
			msg : msg,
			duration : 3000,
			locaiton : 'top'
		});
	}
	ctrl.bind = function() {
		var $startTime = $('#startTime-select');
		var $endTime = $('#endTime-select');
		var $lbStartTime = $('#lbStartTime');
		var $lbEndTime = $('#lbEndTime');
		$startTime.on('change', function() {
			var startTimeValue = $startTime.val();;
			var endTimeValue =  $endTime.val();
			if (startTimeValue) {
				$lbStartTime.html(startTimeValue);
			} else {
				$lbStartTime.html("开始日期");
			}
		})

		$endTime.on('change', function() {
			var startTimeValue = $startTime.val();
			var endTimeValue = $endTime.val();
			if (endTimeValue) {
				$lbEndTime.html(endTimeValue);
			} else {
				$lbEndTime.html("结束日期");
			}
		})

		$("#operate-logs").on("touchend", ".item", function() {
			var id = $(this).data('id');
			api.openWin({
				name : 'operateTicketDetail',
				url : './operateTicketDetail.html',
				pageParam : {
					id : id
				}
			});
		})

		$api.addEvt($api.dom("#query"), "touchend", function() {
			$("#operate-logs").empty();
			ctrl.currentPage = 1;
			ctrl.getOperateTicket();
		});
		//下拉刷新
		api.setRefreshHeaderInfo({
			loadingImg : 'widget://image/refresh.png',
			bgColor : '#ccc',
			textColor : '#fff',
			textDown : '下拉刷新...',
			textUp : '松开刷新...'
		}, function(ret, err) {
			$startTime.val('');
			$endTime.val('');
			$lbStartTime.html("开始日期");
			$lbEndTime.html("结束日期");
			ctrl.currentPage = 1;
			ctrl.getOperateTicket();
			api.refreshHeaderLoadDone();
		});
		//上拉加载
		api.addEventListener({
			name : 'scrolltobottom'
		}, function(ret, err) {
			ctrl.currentPage++;
			if (ctrl.currentPage > ctrl.totalPages) {
				ctrl.toast("没有更多了");
				return;
			}
			ctrl.getOperateTicket();

		});
	}
//获取操作票信息
	ctrl.getOperateTicket = function() {
		var $startTime = $('#startTime-select');
		var $endTime = $('#endTime-select');
		//显示载入动画
		api.showProgress({
			title: '载入中...',
			text: '请稍后',
			modal: false
		});
		var powerRoom = $api.getStorage('powerRoom');
		if (powerRoom) {
			var prId = powerRoom.prId;
		} else {
			return;
		}
		var dateUtil = new DateUtils();
		var pageSize = 20;
		var startDate = $startTime.val();
		if (startDate) {
				dateUtil.setDate(startDate);
				startDate = dateUtil.getMillisecond();
		}
		var endDate = $endTime.val();
		if (endDate) {
				dateUtil.setDate(endDate);
				endDate = dateUtil.getMillisecond();
		}
		// 如果开始时间大于结束时间 交换
		startDate && endDate && (startDate > endDate) && (startDate = [endDate,endDate = startDate][0]);
		var url = '/ems/rest/ticket/czp/page';

		var data = {
			"page" : ctrl.currentPage,
			"pageSize" : pageSize,
			"prId" : prId,
			"startDate" : startDate || null,
			"endDate" : endDate  || null,
			"cusId" : $api.getStorage('cusId')
		};
		$api.get(url, data, function(res, err) {
			if (err) {
				//隐藏载入动画
				api.hideProgress();
				ctrl.toast("网络请求失败");
			} else {
				if (!res.code || res.code != 200) {
					//隐藏载入动画
					api.hideProgress();
					ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
				} else {
					ctrl.renderOperateTicket(res);
				}

			}
		});

	}
	ctrl.renderOperateTicket = function(res) {
		if(ctrl.currentPage == 1){
			$("#operate-logs").empty();
		}

		ctrl.totalPages = res.body.totalPages;
		if (ctrl.totalPages == 0) {
			//隐藏载入动画
			api.hideProgress();
			api.refreshHeaderLoadDone();
			$api.css($api.dom('#operate-logs'), "display:none");
			$api.css($api.dom('#operate-log-empty-tips'), "display:block");
		} else {
			var ticketList = res.body.records;
			var length = ticketList.length;
			for (var i = 0; i < length; i++) {
				$("#operate-logs").append(ctrl.getLogDom(ticketList[i]));
			}
			//隐藏载入动画
			api.hideProgress();
			$api.css($api.dom('#operate-logs'), "display:block");
			$api.css($api.dom('#operate-log-empty-tips'), "display:none");
		}
	}
	ctrl.getLogDom = function(item) {
		var tpl = '';
		tpl += '<div class="item" data-id={{id}}>';
		tpl += '<div class="nav">';
		tpl += '<span class="pr-name">{{prName}}</span>';
		tpl += '<span class="date-time color-title">{{date}}</span></div>';
		tpl += '<div class="con">';
		tpl += '<div class="con-item color-title">';
		tpl += '<span>{{number}}</span></div></div></div>';

		var prName = $api.getStorage('cus') ? $api.guestPrName : item.prName;
		var dateUtil = new DateUtils();
		dateUtil.setDate(item.createTime);
		var createTime = dateUtil.getFormattedDate('yyyy-MM-dd hh:mm:ss');
		return tpl.replace('{{date}}', createTime).replace('{{number}}', item.ticketNum).replace('{{id}}', item.czpId).replace('{{prName}}', prName);

	}
	ctrl.init();
}
