<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>调整职级管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
        var validateForm;
        var count = 0;
        $(document).ready(function() {
            if($("#createDate").val()==null || $("#createDate").val()==''){
                $("#createDate").val(getNowFormatDate());
            }
            validateForm = $("#inputForm").validate({
                submitHandler: function(form){
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                rules:{
                },
                messages:{
                },
                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);
                    }
                }
            });
        });
        function doSubmit(){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
            if(validateForm.form()){
                $("#inputForm").submit();
                return true;
            }else {
                parent.layer.msg("信息未填写完整!", {icon: 5});
            }
            return false;
        }

        function deleteRow(obj,index){
            $(obj).parent().parent().remove();
            str[index] = " ";
            mon[index] = 0;

        }
        function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            month = (month < 10)?"0"+month:month;
            strDate = (strDate < 10)?"0"+strDate:strDate;
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }

        function addRow(list, idx, tpl, row){
            var idx1 = $("#sysModularFieldList tr").length;
            tpl = tpl.replace("sysModularFieldList[0].showType","sysModularFieldList["+sysModularFieldListRowIdx+"].showType");
            tpl = tpl.replace("sysModularFieldList[0].isSysDict","sysModularFieldList["+sysModularFieldListRowIdx+"].isSysDict");
            tpl = tpl.replace("sysModularFieldList[0].logic","sysModularFieldList["+sysModularFieldListRowIdx+"].logic");
            bornTemplete(list, idx, tpl, row, idx1);
        }
        function bornTemplete(list, idx, tpl, row, idx1){
            $(list).append(Mustache.render(tpl, {
                idx: idx, delBtn: true, row: row,
                order:idx1 + 1
            }));
            $(list+idx).find("select").each(function(){
                $(this).val($(this).attr("data-value"));
            });
            $(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
                var ss = $(this).attr("data-value").split(',');
                for (var i=0; i<ss.length; i++){
                    if($(this).val() == ss[i]){
                        $(this).attr("checked","checked");
                    }
                }
            });

        }
        function delRow(obj, prefix){
            var id = $(prefix+"_id");
            var delFlag = $(prefix+"_delFlag");
            if (id.val() == ""){
                delFlag.val("1");
                $(obj).parent().parent().remove();
            }else if(delFlag.val() == "0"){
                delFlag.val("1");
                $(obj).html("&divide;").attr("title", "撤销删除");
                $(obj).parent().parent().addClass("error");
                $(obj).parent().parent().addClass("hide");
            }else if(delFlag.val() == "1"){
                delFlag.val("0");
                $(obj).html("&times;").attr("title", "删除");
                $(obj).parent().parent().removeClass("error");
            }
        }

        function setColumnsInfo(data){
            var modularId=$("#id").val();
                    var idx1 = $("#sysModularFieldList tr").length;
                    var count=sysModularFieldListRowIdx-idx1;
                    for(var i=0;i<data.length;i++){
                        var flat=true;
                        for(var j=0;j<idx1;j++){
                            var trValue= $("#"+"sysModularFieldList"+count+"_2").val();
                            var del= $("#"+"sysModularFieldList"+count+"_delFlag").val();
                            count++;
                            if(data[i].field==trValue&&del!='1'){
                                flat=false;
                                break;
                            }
                        }
                        if(flat){
                            addRow('#sysModularFieldList', sysModularFieldListRowIdx, sysModularFieldListTpl);
                            sysModularFieldListRowIdx = sysModularFieldListRowIdx + 1;
                            var row = sysModularFieldListRowIdx - 1 ;
                            $("#"+"sysModularFieldList"+row+"_modularId").val(modularId);
                            $("#"+"sysModularFieldList"+row+"_1").val(data[i].notes);
                            $("#"+"sysModularFieldList"+row+"_2").val(data[i].field);
                            $("#"+"sysModularFieldList"+row+"_3").val(data[i].fieldType);
                        }
                    }
        }
        function openDialogreUser(title,url,width,height,target){
            if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
                width='auto';
                height='auto';
            }else{//如果是PC端，根据用户设置的width和height显示。

            }
            var tableName=$("#tableName").val();
            top.layer.open({
                type: 2,
                area: ['90%','90%'],
                title:title,
                skin:"two-btns",
                name:'friend',
                content: encodeURI("${ctx}/sysmodular/sysModular/gridColumn?url=${ctx}/sysmodular/sysModular/gridColumn&tableName="+tableName) ,
                btn: ['确定', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    var item = iframeWin.getSelectedItem();
                    if(item == "-1"){
                        return;
                    }
                        setColumnsInfo(item);
                    top.layer.close(index);//关闭对话框。
                },
                cancel: function(index){
                }
            });
        }
        function getChange(value,ids){
            if(value==2){
                $(ids+"_isSysDict").addClass("required");
                $(ids+"_logic").addClass("required");
            }else  {
                $(ids+"_isSysDict").removeClass("required");
                $(ids+"_logic").removeClass("required");
            }

        }
	</script>
	<style>
		/*超过5个汉字，调整label的长度，以下是配套的*/
		.layui-item .layui-form-label{
			width:90px;
		}
		.form-group .layui-item .layui-input-block,
		.query .layui-input-block {
			margin-left: 116px;
		}
	</style>
</head>
<body>
<div class="single-form">
	<div class="container">
		<form:form id="inputForm" modelAttribute="sysModular" action="${ctx}/sysmodular/sysModular/saveColumn"  method="post" class="form-horizontal" >
			<form:hidden path="id" id="id"/>
			<div class="form-group layui-row first">
				<div class="form-group-label"><h2>基础信息</h2></div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>模块名称：</label>
					<div class="layui-input-block">
						<form:input id="name" readonly="true" path="name" htmlEscape="false" class="form-control   layui-input"/>

					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>对应表名：</label>
					<div class="layui-input-block">
						<form:input id="tableName" readonly="true" path="tableName" htmlEscape="false" class="form-control   layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>排序：</label>
					<div class="layui-input-block">
						<form:input id="sort" readonly="true" path="sort" htmlEscape="false" class="form-control number  layui-input"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label"><span class="require-item">*</span>创建日期：</label>
					<div class="layui-input-block">
						<input id="createDate" name="createDate" type="text" readonly="true"  maxlength="20" class="laydate-icondate form-control layer-date  layui-input laydate-icon"
							   value="<fmt:formatDate value="${sysModular.createDate}" pattern="yyyy-MM-dd"/>"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12  with-textarea">
					<label class="layui-form-label">描述：</label>
					<div class="layui-input-block">
						<form:textarea path="remarks"  readonly="true" htmlEscape="false" rows="4"    maxlength="255" class="form-control "/>
					</div>
				</div>
			</div>

			<div class="form-group layui-row">
				<div class="form-group-label"><h2>字段关系</h2></div>
				<div class="layui-item nav-btns ">
					<a href="javascript:void(0)" onclick="openDialogreUser('选择表字段', '${ctx}/sysmodular/sysModular/gridColumn','90%','90%')" class="nav-btn nav-btn-add" ><i class="fa fa-plus"></i>&nbsp;添加字段关系</a>
				</div>
				<div class="layui-item layui-col-xs12 form-table-container" >
					<table id="contentTable" class="table table-bordered table-condensed can-edit no-bottom-margin">
						<thead>
						<tr>
							<th width="200px">字段名称</th>
							<th width="200px">column</th>
							<th width="150px">字段类型</th>
							<th width="200px">排序</th>
							<th width="200px">显示类型</th>
							<th width="200px">字典类型</th>
							<th width="200px">对应类型</th>
							<th width="200px">操作</th>
						</tr>
						</thead>
						<tbody id="sysModularFieldList">
						<c:if test="${not empty sysModular.sysModularFieldList}">
							<c:forEach items="${sysModular.sysModularFieldList}" var="sysModularField" varStatus="index">
								<tr id="sysModularFieldList${index.index}">
									<td class="hide">
										<input id="sysModularFieldList${index.index}_id" name="sysModularFieldList[${index.index}].id" type="hidden" value="${sysModularField.id}"/>
										<input id="sysModularFieldList${index.index}_modularId" name="sysModularFieldList[${index.index}].modularId" type="hidden" value="${sysModularField.modularId}"/>
										<input id="sysModularFieldList${index.index}_delFlag" name="sysModularFieldList[${index.index}].delFlag" type="hidden" value="0"/>
									</td>
									<td>
										<input id="sysModularFieldList${index.index}_1"  name="sysModularFieldList[${index.index}].notes"   type="text" value="${sysModularField.notes}"   class="form-control required"/>
									</td>
									<td>
										<input id="sysModularFieldList${index.index}_2"  name="sysModularFieldList[${index.index}].field"   type="text" value="${sysModularField.field}"   class="form-control "/>
									</td>
									<td>
										<input id="sysModularFieldList${index.index}_3"  name="sysModularFieldList[${index.index}].fieldType"    type="text" value="${sysModularField.fieldType}"   class="form-control "/>
									</td>
									<td>
										<input id="sysModularFieldList${index.index}_4"  name="sysModularFieldList[${index.index}].sort"    type="text" value="${sysModularField.sort}"   class="form-control required number"/>
									</td>
									<td>
										<form:select id="sysModularFieldList${index.index}_showType" onchange="getChange(this.value,'#sysModularFieldList${index.index}')" path="sysModularFieldList[${index.index}].showType" class="form-control required">
											<form:option value="" label="请选择"/>
											<form:options items="${fns:getDictList('show_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
									</td>
									<td>
										<form:select id="sysModularFieldList${index.index}_isSysDict" path="sysModularFieldList[${index.index}].isSysDict" class="form-control ">
											<form:option value="" label="请选择"/>
											<form:options items="${fns:getDictList('isSysDict')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>

									</td>
									<td>
										<form:select id="sysModularFieldList${index.index}_logic" path="sysModularFieldList[${index.index}].logic" class="form-control ">
											<form:option value="" label="请选择"/>
											<form:options items="${dictList}" />
										</form:select>

									</td>
									<td class="text-center op-td" >
										<span class="op-btn op-btn-delete" id="sysModularFieldList${index.index}_span" onclick="delRow(this, '#sysModularFieldList${index.index}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>
									</td>
								</tr>
							</c:forEach>
						</c:if>
						</tbody>
					</table>
					<script type="text/template" id="sysModularFieldListTpl">//<!--
                                    <tr id="sysModularFieldList{{idx}}">
                                        <td class="hide">
                                            <input id="sysModularFieldList{{idx}}_id" name="sysModularFieldList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                            <input id="sysModularFieldList{{idx}}_modularId" name="sysModularFieldList[{{idx}}].modularId" type="hidden" value="{{row.modularId}}"/>
                                            <input id="sysModularFieldList{{idx}}_delFlag" name="sysModularFieldList[{{idx}}].delFlag" type="hidden" value="0"/>
                                        </td>
                                        <td>
                                            <input id="sysModularFieldList{{idx}}_1"   name="sysModularFieldList[{{idx}}].notes" type="text" value="{{row.notes}}"   class="form-control required"/>
                                        </td>
                                        <td>
                                            <input id="sysModularFieldList{{idx}}_2"  name="sysModularFieldList[{{idx}}].field" type="text" value="{{row.field}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <input id="sysModularFieldList{{idx}}_3"  name="sysModularFieldList[{{idx}}].fieldType" type="text" value="{{row.fieldType}}"   readonly="true"  class="form-control "/>
                                        </td>
                                         <td>
                                            <input id="sysModularFieldList{{idx}}_4"  name="sysModularFieldList[{{idx}}].sort" type="text" value="{{row.sort}}"    class="form-control required number"/>
                                        </td>
                                        <td>
                                         <form:select id="sysModularFieldList{{idx}}_showType" onchange="getChange(this.value,'#sysModularFieldList{{idx}}')" path="sysModularFieldList[0].showType" class="form-control required">
                                                    <form:option value="" label="请选择"/>
                                                    <form:options items="${fns:getDictList('show_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                                          </form:select>

										</td>
                                        <td>
                                        <form:select id="sysModularFieldList{{idx}}_isSysDict" path="sysModularFieldList[0].isSysDict" class="form-control ">
                                                    <form:option value="" label="请选择"/>
                                                    <form:options items="${fns:getDictList('isSysDict')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                                          </form:select>

										</td>
                                         <td>
                                          <form:select id="sysModularFieldList{{idx}}_logic" path="sysModularFieldList[0].logic" class="form-control ">
											<form:option value="" label="请选择"/>
											<form:options items="${dictList}"/>
										</form:select>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="sysModularFieldList{{idx}}_span" onclick="delRow(this, '#sysModularFieldList{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
					</script>
					<script type="text/javascript">
                        var sysModularFieldListRowIdx = ${fn:length(sysModular.sysModularFieldList)}, sysModularFieldListTpl = $("#sysModularFieldListTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
					</script>
				</div>
			</div>
			<div class="form-group layui-row page-end"></div>
		</form:form>

	</div>
</div>
</body>
</html>