//查看已发送的消息详情
function viewSendMessage(id){
	$("#myModal-viewSendmessage").modal('show');
	$.ajax({
		url:'/systemMsg/do_sentView',
		type:'get',
		data:{
			'id':id
		},
		success:function(data){
			if( data.success ){
				$("#myModal-viewSendmessage .view-title").text(data.data.title);
				var userList = '';
				for( var i=0;i<data.data.userList.length;i++ ){
					userList += '<span style="display:inline-block;">'+data.data.userList[i].name+'</span>、'
				};
				i = null;
				$("#myModal-viewSendmessage .view-userList").html(userList.substring(0,userList.length-1));
				if( data.data.sendType=='immediate' ){
					$("#myModal-viewSendmessage .view-sendType").text('即时');
				};
				if( data.data.sendType=='timing' ){
					$("#myModal-viewSendmessage .view-sendType").text('定时');
				};
				$("#myModal-viewSendmessage .view-content").text(data.data.content);
			}else{
				new $.zui.Messager('获取已发送消息详情失败:'+data.msg, {
				    type: 'danger'
				 }).show();
			}
		},
		error:function(status){
			new $.zui.Messager('获取已发送消息详情时发生错误:'+status, {
			    type: 'danger'
			 }).show();
		}
	});
};
//查看阅读情况详情
function viewSendReadDetail(id){
	$("#myModal-readingSituation").modal("show");
	$.ajax({
		url:'/systemMsg/do_readDetail',
		type:'get',
		data:{
			'id':id,
		},
		success:function(data){
			 if( data.success ){
				 $("#myModal-readingSituation .noReadPersonNo").text('未读'+data.data.unReadNum+'人');
				 $("#myModal-readingSituation .readPersonNo").text('已读'+data.data.readNum+'人');
				 var noReadPersonDetail = '';
				 if( data.data.unReadNum==0 ){
					 noReadPersonDetail = '<p style="text-align:center;color:#606266;padding:0.1rem 0;font-size:0.14rem;">全部人员已读</p>'
				 };
				 for( var i=0;i<data.data.unReadUserList.length;i++ ){
					 if( data.data.unReadUserList[i].headImgUrl ){
						 noReadPersonDetail += '<div class="readStatusPerson"><div style="background-image:url('+data.data.unReadUserList[i].headImgUrl+')"></div><p>'+data.data.unReadUserList[i].name+'</p></div>'
					 }else{
						 noReadPersonDetail += '<div class="readStatusPerson"><div></div><p>'+data.data.unReadUserList[i].name+'</p></div>'
					 }
				 };
				 i = null;
				 $("#myModal-readingSituation .noReadPersonDetail").empty().html(noReadPersonDetail);
				 var readPersonDetail = '';
				 if( data.data.readNum==0 ){
					 readPersonDetail = '<p style="text-align:center;color:#606266;padding:0.1rem 0;font-size:0.14rem;">全部人员未读</p>'
				 }
				 for( var i=0;i<data.data.readUserList.length;i++ ){
					 if( data.data.readUserList[i].headImgUrl ){
						 readPersonDetail += '<div class="readStatusPerson"><div style="background-image:url('+data.data.readUserList[i].headImgUrl+')"></div><p>'+data.data.readUserList[i].name+'</p></div>'
					 }else{
						 readPersonDetail += '<div class="readStatusPerson"><div></div><p>'+data.data.readUserList[i].name+'</p></div>'
					 }
				 };
				 i = null;
				 $("#myModal-readingSituation .readPersonDetail").empty().html(readPersonDetail)
			 }else{
				 new $.zui.Messager('获取阅读情况详情失败:'+data.msg, {
				    type: 'danger'
				 }).show();
			 }
		},
		error:function(status){
			new $.zui.Messager('获取阅读情况详情时发生错误:'+status, {
			    type: 'danger'
			 }).show();
		}
	});
};
//删除已发送消息
function deleteSendMessage(ids,batch){
	var deletedata = {};
	var deletetip = '';
	if( batch ){//批量
		deletetip = '批量删除消息'
	}else{
		deletetip = '删除消息';
	};
	bootbox.confirm({
		title : deletetip,
		message : '<div style="text-align:left;font-size:0.12rem">确认'+deletetip+'吗？</div>',
		size : 'small',
		buttons : {
			cancel : {
				label : '取消',
				className:'btn-white btn-info'
			},
			confirm : {
				label : '确定',
				className:'btn-white btn-info colorbtn'
			}
		},
		callback:function(result){
			if (result) {
				sendlistpage = $('#sendList').getGridParam('page');
				if( batch ){//批量
					for( var i=0;i<ids.length;i++ ){
						deletedata['msgIds['+i+']'] = ids[i];
					};
					i = null;
				}else{
					deletedata = {
						'msgIds[0]':ids	
					}
				};
				$.ajax({
					url:'/systemMsg/do_deleteSentMsg',
					type:'get',
					data:deletedata,
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager(deletetip+'消息成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								 $('#sendList').jqGrid('setGridParam',{ 
						            mtype:'GET',
						            url : '/systemMsg/do_sentList',
						            postData: {
						            		'pushState':sendmessagetypeid,
										'keyWord':sendmessagecontent
										},
						            page:sendlistpage 
						        }).trigger('reloadGrid');
							}else{
								new $.zui.Messager(deletetip+'消息失败', {
								    type: 'danger'
								}).show();
							}
						}else{
							new $.zui.Messager(deletetip+'消息请求失败:'+data.msg, {
							    type: 'danger'
							}).show();
						}
					},
					error:function(status){
						new $.zui.Messager(deletetip+'消息时发生错误:'+status, {
						    type: 'danger'
						}).show();
					}
				})
			}
		}
	})
}

$(function(){
	$("#sendMessageContent").val(sendmessagecontent);
	$("#sendMessageTypeId i").text(sendmessagetypeword);
	function loadSendData(){
		$('#sendList').setGridParam({
			mtype:'GET',
			url : '/systemMsg/do_sentList',
			datatype : 'json',
			postData: {
				'pushState':sendmessagetypeid,
				'keyWord':sendmessagecontent
				},
			page : 1
		});
		$('#sendList').trigger('reloadGrid');
	};
	
	function showPushState(cellValue,options,rowObject){
		if( rowObject.pushState ){
			return '已发送'
		}else{
			return '<span style="color:red">待发送</span>'
		}
	};
	
	function operateReading(cellValue,options,rowObject){
		return '<div class="readingSituationWord" onclick="viewSendReadDetail('+rowObject.id+')">'+rowObject.readingSituation+'</div>'
	};
	
	function operateSendMessage(cellValue,options,rowObject){
		var btnstr = '';
		btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewSendMessage('+rowObject.id+')"></i></p>';
		btnstr += '<p><span>删除</span><i class="operateIconfont operateIcon-shanchu" onclick="deleteSendMessage('+rowObject.id+')"></i></p>';
		return '<div class="operateIcon noHover">'+btnstr+'</div>';
	}
	
	sxUtil.jqGrid('#sendList',{
		colNames:['消息ID','序号','消息标题','状态','发送时间','阅读情况','事件','操作'],
		colModel:[
			{name: 'id', label: '消息ID', hidedlg:true, hidden:true,sortable:false,title:false},
			{name: 'orderNumber', label: '序号',sortable:false,width:77,align:'center',title:false},
			{name: 'title', label: '消息标题',sortable:false,width:150,align:'left',title:false},
            {name: 'pushState', label: '状态',sortable:false,width:100,align:'center',title:false,formatter:showPushState},
            {name: 'pushTime', label: '发送时间',sortable:false,align:'center',width:180,title:false},
            {name: 'readingSituation', label: '阅读情况',sortable:false,align:'center',width:220,title:false,formatter:operateReading},
            {name: 'content', label: '事件',sortable:false,align:'left',width:250,title:false},
            {name: 'remark', label: '操作',sortable:false,align:'center',width:150,title:false,formatter:operateSendMessage}
		], 
		height:'auto',
		mtype:'GET',
		url : '/systemMsg/do_sentList',
		pager : "#sendListPage",
		multiselect: true,
		rownumbers:false,
		postData : {
			'pushState':sendmessagetypeid,
			'keyWord':sendmessagecontent
		},
		page:sendlistpage,
		loadError:function(){
			new $.zui.Messager('消息列表加载失败', {
			    type: 'warning',
			    time:1500
			}).show();
		},
		beforeSelectRow: function (rowid, e) { //设置点击行是不选中复选框 
		    var $myGrid = $(this),  
		        i = $.jgrid.getCellIndex($(e.target).closest('td')[0]),  
		        cm = $myGrid.jqGrid('getGridParam', 'colModel');  
		    return (cm[i].name === 'cb');  
		} 
	},true);
	
	//切换表格
	$(".projectListTab li").click(function(){
		$(".projectListTab .active").removeClass("active");
		$(this).addClass("active");
		
		if( !$(this).hasClass('viewSentMsgList') ){// 已发送消息
			sendlistpage = $('#sendList').getGridParam('page');
			$(".yk_rightcontent").load("/systemMsg/messageManageList")
		};
	});
	
	//刷新
	$('#refresh').click(function(){
		$("#sendMessageContent").val('');
		$("#sendMessageTypeId i").text('请选择消息类型');
		$("#sendMessageTypeId").data('messageId','');
		$('#sendList').jqGrid('setGridParam',{ 
            mtype:'GET',
            url : '/systemMsg/do_sentList',
            postData: {
            		'pushState':'',
				'keyWord':''
				},
            page:1 
        }).trigger('reloadGrid'); //重新载入
	});
	//消息类型选择	
	$(".sendMessageType").click(function(){
		$("#sendMessageTypeId i").text($(this).text());
		$("#sendMessageTypeId").data("messageId",$(this).attr('value'));
		sendmessagetypeid = $("#sendMessageTypeId").data('messageId');
		sendmessagetypeword = $("#sendMessageTypeId i").text();
		loadSendData();
	})
	//“查询按钮点击”	
	$("#query").click(function(){
		sendmessagecontent = $("#sendMessageContent").val();
		loadSendData();
	})
	//新增按钮点击
	$("#hasBeenSendAdd").click(function(){
		sendlistpage = $('#sendList').getGridParam('page');
		$(".yk_rightcontent").load("/systemMsg/add")
	});
	//批量删除
	$("#batchDeleting").click(function(){
		var deleteIds = $('#sendList').jqGrid('getGridParam','selarrrow');
		if( !deleteIds.length ){
			new $.zui.Messager('请选择要删除的消息', {
			    type: 'danger',
			    time:1500
			}).show();
		}else{
			deleteSendMessage(deleteIds,true)
		}
	})
	var backmessagetypeid,backmessagetypeword,backmessagecontent;
	//返回
	$(".newBackBtn").click(function(){
		$(".yk_rightcontent").load("/systemMsg/messageManageList")
	})
})