<%@ page language="java" contentType="text/html;charset=UTF-8" trimDirectiveWhitespaces="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fastboot" prefix="fb"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<fb:ui src="base/main">

	<fb:property name="title"><spring:message code="taglib.addnew"/></fb:property>

	<fb:script type="text/javascript">
		<script>
			layui.use(['tool', 'form', 'sug'],function() {
				  		// 操作对象
					    var $ = layui.$,tool = layui.tool,form = layui.form,sug = layui.sug;
				  		// 初始化姓名输入提示框
						sug.render({
							id: "company", //设置容器唯一id
							height: "400",
							cols: [
								[{
									field: 'id',
									title: 'id'
								},{
									field: 'company',
									title: '<spring:message code="dir.company"/>'
								}, {
									field: 'contacts',
									title: '<spring:message code="dir.contacts"/>'
								}]
							],
							params: [{
							    name: 'company',
							    field: 'company'
							}, {
							    name: 'contacts',
							    field: 'contacts'
							}, {
							    name: 'organId',
							    field: 'id'
							}],
							type: 'sugTable', 
							url: "${base}/organ/page?keyword=",
							limit: 10
						});
						$("#company").blur(function(){
							let organ = $.trim($("#company").val());
							if (organ == "") {
								$("#organId").val("");
							}
						})
						// 初始赋值
					    form.val('inputForm', ${json});
					    // 监听提交
					    form.on('submit(go)',function(data) {
					    	let prefix = '${defaultUsername}'
							let username = $("#username").val()
							if(username.length < prefix.length){
								layer.msg("<spring:message code='user.tips2' /> ${defaultUsername}")
								return false;
							}
							if(username.substr(0,"${defaultUsername}".length) != "${defaultUsername}"){
								layer.msg("<spring:message code='user.tips2' />  ${defaultUsername}")
								return false;
							}

							if(username == "${defaultUsername}"){
								layer.msg("<spring:message code='please_input' />   <spring:message code='username' />")
								return false;
							}

					        console.log($("#inputForm").serializeArray());
					        tool.submit('${base}/user/submit', $("#inputForm").serializeArray());
					        return false;
					    });

						//监听指定开关
						form.on('switch(showOrHide)', function(data){
							if(this.checked){
								$("#pwd").val($("#code").val())
							}else{
								$("#pwd").val("")
							}
						});
			});
		</script>

	</fb:script>

	<fb:layout>

		<div class="layui-card">

			<form id="inputForm" class="layui-form layui-form-pane" action="" lay-filter="inputForm">

				<input type="hidden" name="id" /> <input type="hidden" name="password" class="layui-input" />

				<div class="layui-form-item">
					<label class="layui-form-label"><spring:message code="organ"/></label>
					<div class="layui-input-block">
						<input type="text" id="company" name="company" value="${user.organName}" class="layui-input">
						<input type="hidden" id="organId" name="organId" value="${user.organId}" class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label"><spring:message code="account"/></label>
					<div class="layui-input-block">
						<input type="text" id="username" name="username"  class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label"><spring:message code="password"/></label>
					<div class="layui-input-block">
						<input type="text" id="pwd"  name="pwd" class="layui-input" autocomplete="off">
						<input type="text" id="pwd_" style="display: none"   class="layui-input" autocomplete="off">
						<input type="hidden" id="code" name="code"/>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label"><spring:message code="showOrHide"/></label>
					<div class="layui-input-block">
						<input type="checkbox"  lay-skin="switch" lay-filter="showOrHide" lay-text="ON|OFF">
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label"><spring:message code="nickname"/></label>
					<div class="layui-input-block">
						<input type="text" name="nickname" lay-verify="required" lay-verType="tips" autocomplete="off"  class="layui-input">
					</div>
				</div>
				<div class="layui-form-item" pane="">
					<label class="layui-form-label"><spring:message code="role"/> </label>
					<div class="layui-input-block">
						<c:forEach items="${roles}" var="role">
							<input type="radio" name="ids" value="${role.id}" title="${role.name}"
									<c:forEach items="${userRoles}" var="userRoles">
										${role.id == userRoles.pk.roles?'checked':''}
									</c:forEach> lay-skin="primary" lay-filter="go" />
						</c:forEach>
					</div>
				</div>

				<button id="submit" lay-submit lay-filter="go" hidden="true" />
				<button id="reset" type="reset" hidden="true" />
			</form>
		</div>
	</fb:layout>
</fb:ui>