<%@ 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 weui-cell_select weui-cell_select-after">
                <div class="weui-cell__hd">
                    <label for="" class="weui-label">客户单位</label>
                </div>
                <div class="weui-cell__bd">
                    <select class="weui-select" id="type" errmsg="客户单位不能为空">
                        <option value="">--请选择客户单位--</option>
						<my:outOptions type="331"/>
                    </select>
                </div>
     		</div>
            
            <div class="weui-cell weui-cell_select weui-cell_select-after">
                <div class="weui-cell__hd">
                    <label for="" class="weui-label">重要程度</label>
                </div>
                <div class="weui-cell__bd">
                    <select class="weui-select" id="importance" errmsg="重要程度不能为空">
                        <option value="">--请选择类型--</option>
						<my:outOptions type="332"/>
                    </select>
                </div>
     		</div>

			<div class="weui-cell weui-cell_select weui-cell_select-after">
                <div class="weui-cell__hd">
                    <label for="" class="weui-label">执行区域</label>
                </div>
                <div class="weui-cell__bd">
                    <select class="weui-select" id="area" errmsg="执行区域不能为空">
						<option value="">--请选择区域--</option>
						<option value="深圳">深圳</option>
						<option value="上海">上海</option>
						<option value="武汉">武汉</option>
					</select>
                </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-cell weui-cell_select weui-cell_select-after">
                <div class="weui-cell__hd">
                    <label for="" class="weui-label">项目状态</label>
                </div>
                <div class="weui-cell__bd">
                    <select class="weui-select" id="status" errmsg="项目状态不能为空">
                        <!-- <option value="0">未开始</option> -->
						<option value="1">进行中</option>
						<option value="2">暂停</option>
						<option value="3">结束</option>
                    </select>
                </div>
     		</div>
			
			<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="contractNo" maxlength="30" errmsg="合同编号不能为空">
                </div>
            </div>
			
			<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="customer" maxlength="50">
                </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="fzNames" readonly="readonly" errmsg="项目负责人不能为空" value="">
	                    <input type="hidden" id="fzIds" value="">
	                </div>
	                <div class="weui-cell__ft">
	                    <button id="btn_selfzr" 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">
	            <div class="weui-cell">
	                <div class="weui-cell__bd">
	                    <input class="weui-input" placeholder="请选择授权人员" type="text" id="userNames" readonly="readonly" value="">
	                    <input type="hidden" id="userIds" value="">
	                </div>
	                <div class="weui-cell__ft">
	                    <button id="btn_seluser" 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="项目内容不能为空"></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>
	        </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">

// 选择企业微信联系人
function selectWxContact(bid){
	var inputNameId = null, inputHideId = null;
	if(bid == 'btn_selfzr'){ // 负责人
		inputNameId = 'fzNames';
		inputHideId = 'fzIds';
	}else if(bid == 'btn_seluser'){ // 抄送人员
		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);
                if(inputHideId=='fzIds')setChaoSongUser(seluserIds);
	   		}
	    }
	);
}

//选择负责人，自动设置上级人员为抄送人员
function setChaoSongUser(userIds){
	if(userIds==null || $.trim(userIds)=='')return;
	$.post('user/selectLeaderUsers.do',{userIds:userIds},function(data){
		if(data.statusCode == 200){
			var userIds = data.userIds;
			var userNames = data.userNames;
			if(userIds && userIds.length>0){
				$('#userIds').val(userIds.join(','));
				$('#userNames').val(userNames.join(','));
			}
		}
	},'json');
}

//根据ID获取数据详情
function loadById(dataid){
	$.post('project/findById.do', {id: dataid}, function(result){
		var data = JSON.parse(JSON.stringify(result).trim());
		if(data.p){
		//	$('#name').val(data.p.name);
			$('.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 = data.p[id];
					if(!isBlank(value)){
						$(this).val(formatDate(value,'yyyy-MM-ddThh:mm:ss'));
					}
				}else{
					$(this).val(data.p[id]);
				}
			});
		}
		if(!isBlank(data.fzIds)){
			$('#fzIds').val(data.fzIds);
			$('#fzNames').val(data.fzNames);
		}
		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();
		var errmsg = $(this).attr('errmsg');
		console.log('id='+id+', value='+value);
		if(!isBlank(errmsg) && isBlank(value)){
			toastMsg(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;
	
	var posturl = 'project/add.do';
	if(!isBlank($('#id').val()))posturl='project/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/list.jsp';
				}else{
					window.location.href = getSysBaseUrl()+'/wxwork/pages/project/list.do';
				}
			}, 1200);
		}else{
			toastMsg(data.message);
		}
	}, 'json');
}

$(function(){
	getJsConfig();
	
	var dataid =getQueryString('id');
	if(!isBlank(dataid)){
		$('#pagetitle').text('修改项目');
		loadById(dataid);
	}else{
		$('#pagetitle').text('新建项目');
	}
	
	// 测试填写的负责人
	var testfzr = getQueryString("fzr");
	if(!isBlank(testfzr)){
		$('#fzNames').val(testfzr);
		$('#fzIds').val(testfzr);
	}
	
	$('.lookupusers').click(function(){
		var bid = $(this).attr('id');
		selectWxContact(bid);
	});
	
	$('#btn_submit').click(function(){
		saveAction();
	});
	
});

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