<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/include.inc.jsp" %>
<%@include file="/WEB-INF/jsp/commons/jstl_message_tld.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title><fmt:message key="app.name" bundle="${commonBundle}"/></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
	<link href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui-for-work.min.css" rel="stylesheet" type="text/css"/>
	<link href="resource/css/weixin-work/public.css" rel="stylesheet" type="text/css"/>
</head>
<body ontouchstart>
	<div class="page__bd">
		<div class="weui-cells__title" id="pagetitle" style="text-align:center;font-weight: bold;"></div>
        <div class="weui-cells weui-cells_form">
        	<input type="hidden" id="id" name="id" value="" />
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">任务名称</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" placeholder="请输入任务名称" type="text" id="name" maxlength="100" errmsg="任务名称不能为空">
                </div>
            </div>
            
   			<div class="weui-cell">
                <div class="weui-cell__hd"><label for="" class="weui-label">开始时间</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" value="" placeholder="" type="datetime-local" id="startTime" errmsg="开始时间不能为空">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label for="" class="weui-label">结束时间</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" value="" placeholder="" type="datetime-local" id="endTime" errmsg="结束时间不能为空">
                </div>
            </div>
            
			<div class="weui-cells__title">执行人</div>
	        <div class="weui-cells">
	            <div class="weui-cell">
	                <div class="weui-cell__bd">
	                    <input class="weui-input" placeholder="请选择任务执行人" type="text" id="userNames" readonly="readonly" errmsg="任务执行人不能为空" value="">
	                    <input type="hidden" id="userIds" value="">
	                </div>
	                <div class="weui-cell__ft">
	                    <button id="btn_selzxr" class="weui-vcode-btn lookupusers" style="padding-right: 0px; height: 100%; line-height: 100%;">选择用户</button>
	                </div>
	            </div>
			</div>
			
			<div class="weui-cells__title">内容</div>
	        <div class="weui-cells weui-cells_form">
	            <div class="weui-cell">
	                <div class="weui-cell__bd">
	                    <textarea class="weui-textarea" placeholder="请输入任务内容" rows="3" id="content" errmsg="任务内容不能为空" maxlength="500"></textarea>
	                    <div class="weui-textarea-counter"><span>0</span>/200</div>
	                </div>
	            </div>
	        </div>
			
	        <div class="weui-btn-area">
	            <a class="weui-btn weui-btn_primary" href="javascript:" id="btn_submit">确 定</a>
	       		<a class="weui-btn weui-btn_default" href="javascript:history.back();">返 回</a>
	   		</div>
	        
    	</div><!-- .weui-cells_form -->
	</div><!-- .page__bd -->

<script type="text/javascript" src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript" src="resource/js/weixin-work/public.js"></script>
<script type="text/javascript">
var pid = null, id = null;

// 选择企业微信联系人
function selectWxContact(bid){
	var inputNameId = null, inputHideId = null;
	if(bid == 'btn_selzxr'){ // 任务执行人
		inputNameId = 'userNames';
		inputHideId = 'userIds';
	}else{
		return;
	}
	
	var selusernames = '', seluserIds = $('#'+inputHideId).val();
	wx.invoke("selectEnterpriseContact", {
        "fromDepartmentId": 0,			// 必填，-1表示打开的通讯录从自己所在部门开始展示, 0表示从最上层开始
        "mode": "multi",				// 必填，选择模式，single表示单选，multi表示多选
        "type": ["user"],				// 必填，选择限制类型，指定department、user中的一个或者多个
        "selectedDepartmentIds": [],	// 非必填，已选部门ID列表。用于多次选人时可重入
        "selectedUserIds": seluserIds.split(",")	// 非必填，已选用户ID列表。用于多次选人时可重入
	    },function(res){
	 		if (res.err_msg == "selectEnterpriseContact:ok"){
                if(typeof res.result == 'string'){
           			res.result = JSON.parse(res.result) //由于目前各个终端尚未完全兼容，需要开发者额外判断result类型以保证在各个终端的兼容性
                }
                seluserIds='';
                var selectedUserList = res.result.userList; // 已选的成员列表
                for (var i = 0; i < selectedUserList.length; i++){
            		var user = selectedUserList[i];
                    seluserIds += (seluserIds==null||seluserIds==''?'':',')+user.id;
                    selusernames += (selusernames==null||selusernames==''?'':',')+user.name;
           		}
                $('#'+inputHideId).val(seluserIds);
                $('#'+inputNameId).val(selusernames);
	   		}
	    }
	);
}

//根据ID获取数据详情
function loadById(){
	$.post('projectTask/findTaskById.do', {id: id}, function(result){
		var data = JSON.parse(JSON.stringify(result).trim());
		if(data.t){
			var taskObj=data.t;
			$('.weui-cells_form input, .weui-cells_form select, .weui-cells_form textarea').each(function(){
				var id = $(this).attr('id');
				if(id == 'startTime' || id == 'endTime'){
					var value = taskObj[id];
					if(!isBlank(value)){
						$(this).val(formatDate(value,'yyyy-MM-ddThh:mm:ss'));
					}
				}else{
					$(this).val(taskObj[id]);
				}
			});
		}
		if(!isBlank(data.userIds)){
			$('#userIds').val(data.userIds);
			$('#userNames').val(data.userNames);
		}
	}, 'json');
}

// 新建项目保存
function saveAction(){
	var params = {};
	var pass = true;
	$('.weui-cells_form input, .weui-cells_form select, .weui-cells_form textarea').each(function(){
		var id = $(this).attr('id');
		var value = $(this).val();
		if(id == 'id'){
			params[id]=value;
			return true;
		}
		if(value == null || $.trim(value)==''){
			toastMsg($(this).attr('errmsg'));
			pass = false;
			return false;
		}
		if(id == 'startTime' || id == 'endTime'){
			value = value.replace("T", " ").replace("/", "-");
			if(value.length > 19){ // IOS格式：2018-07-23 15:22:40.340
				value = value.substring(0, 19);
			}
		}
		params[id]=value;
	});
	if(!pass)return;
	
	params['projectId']=pid;
	var posturl = 'projectTask/add.do';
	if(!isBlank($('#id').val()))posturl='projectTask/update.do';
	$.post(posturl, params, function(result){
		var data = JSON.parse(JSON.stringify(result).trim());
		if(data.statusCode == 200){
			toastMsg('保存成功！', 1);
			setTimeout(function() {
				if(debugMode){
					window.location.href = getSysBaseUrl()+'/pages/weixin-work/project/task/list.jsp?pid='+pid;
				}else{
					window.location.href = getSysBaseUrl()+'/wxwork/pages/project/task/list.do?pid='+pid;
				}
			}, 1200);
		}else{
			toastMsg(data.message);
		}
	}, 'json');
}

$(function(){
	getJsConfig();
	
	pid = getQueryString('pid');
	id = getQueryString('id');
	
	if(!isBlank(id)){
		$('#pagetitle').text('修改任务');
		loadById();
	}else{
		$('#pagetitle').text('新建任务');
	}
	
	// 测试填写的负责人
	var testuser = getQueryString("zxr");
	if(!isBlank(testuser)){
		$('#userNames').val(testuser);
		$('#userIds').val(testuser);
	}
	
	$('.lookupusers').click(function(){
		var bid = $(this).attr('id');
		selectWxContact(bid);
	});
	
	$('#btn_submit').click(function(){
		saveAction();
	});
	
});

</script>
</body>
</html>
