<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", -10);
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"	+ request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@include file="/WEB-INF/views/base.jsp"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>sendSms</title>
<%@include file="/WEB-INF/views/common_base_css.jsp"%>
</head>
<body>
	<!-- 菜单导航 开始 -->
	<input type="hidden" id="basePath" value="<%=basePath%>" />
	<div id="wrapper">
		<jsp:include page="../menu.jsp"></jsp:include>
		<!-- 菜单导航 结束 -->

		<div id="page-wrapper" class="gray-bg">
			<!-- 顶部 菜单开始  -->
			<jsp:include page="../head.jsp"></jsp:include>
			<jsp:include page="../pageNav.jsp"></jsp:include>

			<!-- 页面 主题内容  开始-->
			<div class="wrapper wrapper-content animated fadeInRight">
				<div class="row">
				<!-- 用户 分配角色 弹层 -->
		        	<div class="modal inmodal fade" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
	              		<div class="modal-dialog">
	                     	<div class="modal-content animated ">
	                        	<div class="modal-header">
	                                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
	                                <h4 class="modal-title">发送短信</h4>
	                           	</div>
	                           	<div class="modal-body">
	                           		<div class="input-group">
	                           			<label  style="text-align:center; width: 100px;vertical-align: middle;display: table-cell;">发送内容 </label>
											<textarea id="content" name="content" rows="3"
													class="form-control" required="" style="width: 200%;" >同事您好，感谢您对此次测试的配合。</textarea>
				                    </div>
	                           	</div>
	
	                            <!-- 角色菜单  tab 结束-->
	                            <div class="modal-footer">
	                                <button type="button" class="btn btn-white" data-dismiss="modal">取消</button>
									<button type="button" data-dismiss="modal" id="sendOK"
										class="btn btn-primary">发送</button>
								</div>
	                         </div>
	                     </div>
	                 </div>
		        	<!-- 用户 分配角色 弹层 -->
		        	
					<!-- 查询条件 开始 -->
					<input type="hidden" id="pageNo" name="pageNo" value="1">

					<div class="ibox-content m-b-sm border-bottom">
						<div class="row">
							<div class="col-sm-3">
								<div class="form-group">
									<label class="control-label" for="classify">部门</label> 
									<select class="form-control" id="classify" name="classify" >
										<option value="">请选择</option>
										<c:forEach items="${constList}" var="s">
											<option value="${s.dictvalue }">${s.dictname }</option>
										</c:forEach>
									</select> 
				                </div>
							</div>
							<div class="col-sm-3">
								<div class="form-group">
									<label class="control-label" for="name">姓名</label> <input
										type="text" id="name" name="name" class="form-control">
								</div>
							</div>
							
							<div class="col-sm-3">
								<div class="form-group">
									<label class="control-label" for="phoen">手机号码</label> <input
										type="text" id="phoen" name="phoen" class="form-control">
								</div>
							</div>
							
							<div class="col-sm-3">
								<div class="form-group">
									<label class="control-label" for="search">查询</label>
									<button onclick="query()"
										class="btn btn-w-m btn-success form-control">
										<i class="fa fa-search"></i>&nbsp;&nbsp;查询
									</button>
								</div>
							</div>
						</div>
					</div>

					<!-- 查询条件 结束 -->

					<!-- 用户列表开始 -->
					<div class="ibox float-e-margins">
					<div class="ibox-title">
								<a href="${pageContext.request.contextPath}/sms/smsUserImport" class="btn btn-outline btn-primary">&nbsp;导入</a>
								<a href="${pageContext.request.contextPath}/sms/addSmsUserGet" class="btn btn-outline btn-primary">&nbsp;添加</a>
								<button id="update_smsUser" type="button" class="btn btn-outline btn-primary">&nbsp;编辑</button>
								<button id="delete_smsUser" type="button" class="btn btn-outline btn-primary">&nbsp;删除</button>
							
					</div>

						<div class="ibox-content" id="UserListPartial">
							<div class="">
								<button id="sendMsg" type="button" class="btn btn-outline btn-default">&nbsp;发送短信</button>
							</div>
							
							<div id="editable">
								<!-- table开始 -->

								<!-- table结束 -->
							</div>
						</div>
					</div>
					<!-- 用户列表结束 -->
				</div>
			</div>
		</div>

	</div>
	<!-- Mainly scripts -->
	<%@include file="/WEB-INF/views/common_base_script.jsp"%>
	<script>
	//弹出框
		$(function () {
       		var modal = $('#myModal').modal({  keyboard: false, show : false});
			initData();
        	$("#sendMsg").click(function (){
         	 	var smsUserPhoens = "";
				var flag = false;
		 		$("input[name='smsuserId']").each(function(){
					if($(this).is(":checked")){
						smsUserPhoens += $(this).val() + ",";
						flag = true;
					}
				});
				if(!flag){
					CommonUtils.alertMsg(0, "发送内容", "请至少选择一个用户发送短信！");
				} else {
					if(smsUserPhoens.length > 1){
						smsUserPhoens = smsUserPhoens.substring(0, smsUserPhoens.length-1);
					}
					$('#myModal').attr("smsUserPhoens",smsUserPhoens);
					modal.modal('show');
				}
       	 	});
       	 	
       	 	//全选，全不选
			$("body").unbind('click').delegate('#selectAll', 'click' , function(){
				if($(this).is(":checked")){
					$("input[name='smsuserId']").each(function(){
						$(this).prop("checked", true);
					});
				} else {
					$("input[name='smsuserId']").each(function(){
						$(this).prop("checked", false);
					});
				}
			});
	       	
	     $("#sendOK").click(function(){
			var smsUserPhoens=$('#myModal').attr("smsUserPhoens");
			var content = $('#content').val();
			$('#content').val("");
			if(content == ""){
				CommonUtils.alertMsg(0, "发送短信", "请输入发送内容");
				return;
			}
			$.ajax({
				type : 'POST',
				async : false,
				url : basePath + 'sms/sendMessage.json',
				data : {
					'smsUserPhoens' : smsUserPhoens,
					'content':content
				},
				dataType : 'json',
				success : function(data){
					if(data.status == "success"){
						CommonUtils.toastMsg(0,data.message, 2);
						initData();
					}else if(data.status =="failure"){
						CommonUtils.toastMsg(0,data.message, 2);
						initData();
					}else{
						CommonUtils.alertMsg(0, "发送短信", data.message);
					}
				}
			})
			
		});	
		
      });
	    
       var basePath = "<%=basePath %>";
         function initData(){
          var pageNo = $("#pageNo").val();
          var classify = $("#classify").val();
          var name = $("#name").val();
          var phoen = $("#phoen").val();
			//第一次加载数据
			$.ajax({
				type : 'POST',
				async : false,
				url : basePath + '/sms/initDataList',
				data : {
					'pageNo' : pageNo,
					'classify' : classify,
					'name' : name,
					'phoen' : phoen
				},
				dataType : 'html',
				success : function(data){
					$("#editable").html(data);
				}
			})
         }
     	
     	function query(){
     		$("#pageNo").val("1");
     		initData();
     	}
     	
     	
		//删除用户
		$("#delete_smsUser").click(function(){
			var smsuserIds = "";
			var flag = false;
	 		$("input[name='smsuserId']").each(function(){
				if($(this).is(":checked")){
					smsuserIds += $(this).attr("suId") + ",";
					flag = true;
				}
			});
			if(!flag){
				CommonUtils.alertMsg(0, "删除用户", "请至少选择一条记录");
			} else {
				if(smsuserIds.length > 1){
					smsuserIds = smsuserIds.substring(0, smsuserIds.length-1);
				}
				var basePath = $("#basePath").val();
				$.ajax({
					type : 'POST',
					async : false,
					url : basePath + 'sms/deleteSmsUser.json',
					data : {
						'smsuserIds' : smsuserIds
					},
					dataType : 'json',
					success : function(data){
						if(data.status == "success"){
							CommonUtils.toastMsg(0,data.message, 2);
							initData();
						}else{
							CommonUtils.alertMsg(0, "删除用户", data.message);
						}
					}
					
				})
			}
		});
		
		//修改信息
		$("#update_smsUser").click(function(){
			var smsuserIds = "";
			var flag = false;
			var count = 0;
			$("input[name='smsuserId']").each(function(){
				if($(this).is(":checked")){
					smsuserIds += $(this).attr("suId") + ",";
					count++;
					flag = true;
				}
			});
			if(!flag){
				CommonUtils.alertMsg(0, "修改用户信息", "请至少选择一条用户信息");
			}else if(count > 1){ 
				CommonUtils.alertMsg(0, "修改用户信息", "不能同时选择多条用户信息");
			}else {
				if(smsuserIds.length > 1){
					smsuserIds = smsuserIds.substring(0, smsuserIds.length-1);
				}
				var basePath = $("#basePath").val();
				window.open(basePath + 'sms/updateSmsUser?smsuserId='+smsuserIds, '_self');
			}
		});
	</script>
</body>
</html>