<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>用户管理</title>
	<meta name="decorator" content="default"/>
	<%--<style type="text/css">--%>
		<%--img{width: 50px;height: 50px;}--%>
	<%--</style>--%>
	<script type="text/javascript">
		var validateForm;
		function doSubmit(){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $("#inputForm").submit();
			  return true;
		  }
	
		  return false;
		}
		$(document).ready(function() {
		    //初始化加载
            var companyId = $("#companyId").val();
            queryRole(companyId)
			$("#no").focus();
            // 手机号码验证
            jQuery.validator.addMethod("isMobile", function(value, element) {
                var length = value.length;
                var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
                return (length == 11 && mobile.test(value));
            }, "请正确填写您的手机号码");
			validateForm = $("#inputForm").validate({
				rules: {
					loginName: {remote: "${ctx}/sys/user/checkLoginName?oldLoginName=" + encodeURIComponent('${user.loginName}')},//设置了远程验证，在初始化时必须预先调用一次。
                    no: {remote: "${ctx}/sys/user/checkNo?oldNo=" + encodeURIComponent($("#oldNo").val())},//设置了远程验证，在初始化时必须预先调用一次。
				},
				messages: {
					loginName: {remote: "用户登录名已存在"},
					confirmNewPassword: {equalTo: "输入与上面相同的密码"},
					no: {remote: "重复的工号"},
				},
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});

			//在ready函数中预先调用一次远程校验函数，是一个无奈的回避案。(刘高峰）
			//否则打开修改对话框，不做任何更改直接submit,这时再触发远程校验，耗时较长，
			//submit函数在等待远程校验结果然后再提交，而layer对话框不会阻塞会直接关闭同时会销毁表单，因此submit没有提交就被销毁了导致提交表单失败。
			//$("#inputForm").validate().element($("#loginName"));
		});

        /**
		 * 查询岗位
         * @param obj
         */
		function queryRole(obj){
		    $.ajax({
				type:'post',
				url:'${ctx}/sys/user/queryRole',
				data:{
				    "officeId":obj
				},
				success:function(data){
                    $("#rolest").empty();
                    var rId = $("#roleId").val();
                    $("#rolest").append("<option class='required' selected value=''>-- 请选择 --</option>");
                    $.each(data.list, function(index,item){
                        if(rId == item.id){
							$("#rolest").append("<option class='required' selected value='"+item.id+"'>"+item.name+"</option>")
						}else{
							$("#rolest").append("<option class='required' value='"+item.id+"'>"+item.name+"</option>")
						}
                    });
				}
			});
		}
	</script>
	<script type="text/javascript">
        //上传用户头像
        function this_upload_image_button(index){
            $("#this_upload_file_"+index).click();
        }
        function this_upload_show_image(index){
            var obj =$("#this_upload_file_"+index)[0].files[0];
            var fileType = obj.type;
            var url ;
            if(fileType.endsWith("png")||fileType.endsWith("bmp")||fileType.endsWith("jpg")||fileType.endsWith("jpeg")||fileType.endsWith("gif")){
                url = window.URL.createObjectURL(obj);
            }else {
                $("#this_upload_msg").css({"color":"red"});
                this_close_img(index);
                return false ;
            }
            $("#this_upload_msg" + "index").removeAttr("style");
            $("#this_upload_image_"+index).attr("src",url);

			var span = '<span id="this_upload_close_'+index+'" class="pic_close glyphicon glyphicon-remove" onclick="this_close_img('+index+');"></span>';
			$("#this_upload_image_" + index).before(span);
        }

        function this_close_img(index) {
            $("#this_upload_div_"+index).remove();
			this_add_div(index);
        }

        function this_add_div(index) {
            var this_upload_div_1 =
                '<div id="this_upload_div_'+ index + '" style="position:relative;">'+
                '<img  id="this_upload_image_' + index + '" class="upload_ico" style="cursor:pointer;"  src="${pageContext.request.contextPath}/static/common/img/pic_add.png"  onclick="this_upload_image_button('+index+')"/>'+
                '<input id="this_upload_file_'+ index + '" type="file" style="display:none"  name="this_upload_files"  onchange="this_upload_show_image('+index+ ')" /><br>'+
                '</div>';
            $("#this_upload_image_div_"+ index).html("").append(this_upload_div_1);
        }

	</script>
</head>
<body>
<div class="single-form">
	<div class="container">
	<form:form id="inputForm" modelAttribute="user" action="${ctx}/sys/user/save" enctype="multipart/form-data" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<div class="form-group layui-row first">
			<div class="form-group-label"><h2>用户信息</h2></div>
			<div class="layui-item layui-col-sm6 lw7" style="padding-right: 0;">
				<div class="layui-item layui-col-sm12">
					<label class="layui-form-label"><span class="require-item">*</span>登录名：</label>
					<div class="layui-input-block">
						<input id="oldLoginName" name="oldLoginName" type="hidden" value="${user.loginName}">
						<form:input path="loginName" htmlEscape="false" minlength="11" maxlength="11" class="form-control layui-input required number text-muted isMobile userName"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12">
					<label class="layui-form-label"><span class="require-item">*</span>手机：</label>
					<div class="layui-input-block">
						<form:input path="mobile" htmlEscape="false" maxlength="11" minlength="11" class="form-control layui-input text-muted number required isMobile"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12">
					<label class="layui-form-label">邮箱：</label>
					<div class="layui-input-block">
						<form:input path="email" htmlEscape="false" maxlength="100" class="form-control layui-input email"/>
					</div>
				</div>
			</div>
			<div class="layui-item layui-col-sm6 lw7">
				<div style="margin-right: 120px;">
					<div class="layui-item layui-col-sm12">
						<label class="layui-form-label"><span class="require-item">*</span>姓名：</label>
						<div class="layui-input-block">
							<form:input path="name" htmlEscape="false" maxlength="50" class="form-control layui-input required"/>
						</div>
					</div>

					<div class="layui-item layui-col-sm12">
						<label class="layui-form-label"><span class="require-item">*</span>归属公司：</label>
						<div class="layui-input-block">
							<input htmlEscape="false" readonly="true" class="form-control layui-input" value="${user.company.name}"/>
						</div>
					</div>
					<div class="layui-item layui-col-sm12">
						<label class="layui-form-label"><span class="require-item">*</span>归属部门：</label>
						<div class="layui-input-block with-icon">
							<sys:treeselect id="office" name="office.id" value="${user.office.id}" labelName="office.name" labelValue="${user.office.name}"
											title="部门" url="/sys/office/treeData?type=2" cssClass="form-control layui-input required" notAllowSelectParent="false"/>
						</div>
					</div>
				</div>
				<div class="profile_box_wrapper">
					<div class="profile_box">
						<div id="this_upload_image_div_1">
							<div id="this_upload_div_1" style="position:relative;">
								<c:choose>
									<c:when test="${not empty user.photo}">
										<span id="this_upload_close_1" class="pic_close glyphicon glyphicon-remove" onclick="this_close_img(1)"></span>
										<img alt="_blank" id="this_upload_image_1" class="upload_ico" style="cursor:pointer;"  src="${user.photo}?x-oss-process=image/resize,w_150,limit_0"  onclick="this_upload_image_button(1)"/>
									</c:when>
									<c:otherwise>
										<img alt="_blank" id="this_upload_image_1" class="upload_ico" style="cursor:pointer;"  src="${pageContext.request.contextPath}/static/common/img/pic_add.png"  onclick="this_upload_image_button(1)"/>
									</c:otherwise>
								</c:choose>
								<input id="this_upload_file_1" type="file" style="display:none"  name="this_upload_files"  onchange="this_upload_show_image(1)"  class="form-control required"/>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-item layui-col-sm6 lw7">
				<label class="layui-form-label"><c:if test="${empty user.id}"><span class="require-item">*</span></c:if>密码：</label>
				<div class="layui-input-block">
					<input id="newPassword" name="newPassword" type="password" value="" maxlength="12" minlength="6" class="form-control layui-input ${empty user.id?'required':''}"/>
					<c:if test="${not empty user.id}"><span class="help-inline">若不修改密码，请留空。</span></c:if>
				</div>
			</div>
			<div class="layui-item layui-col-sm6 lw7">
				<label class="layui-form-label"><c:if test="${empty user.id}"><font color="red">*</font></c:if>确认密码：</label>
				<div class="layui-input-block">
					<input id="confirmNewPassword" name="confirmNewPassword" type="password"  class="layui-input form-control ${empty user.id?'required':''}" value="" maxlength="12" minlength="6" equalTo="#newPassword"/>
				</div>
			</div>
			<div class="layui-item layui-col-sm12 lw7">
				<label class="layui-form-label">备注：</label>
				<div class="layui-input-block">
					<form:textarea path="remarks" htmlEscape="false" rows="2" maxlength="200" class="form-control"/>
				</div>
			</div>
			<c:if test="${not empty user.id}">
				<div class="layui-item layui-col-sm6 lw7">
					<label class="layui-form-label">创建时间：</label>
					<div class="layui-input-block">
						<input htmlEscape="false" disabled="true" class="form-control layui-input" value="<fmt:formatDate value="${user.createDate}" type="both" dateStyle="full"/>"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6 lw7">
					<label class="layui-form-label">最后登陆：</label>
					<div class="layui-input-block">
						<input htmlEscape="false" disabled="true" class="form-control layui-input" value="IP: ${user.loginIp}&nbsp;&nbsp;&nbsp;&nbsp;时间：<fmt:formatDate value="${user.loginDate}" type="both" dateStyle="full"/>"/>
					</div>
				</div>
			</c:if>
		</div>
		</form:form>
	</div>
</div>
</body>
</html>