<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>模板规则</title>
	<meta name="decorator" content="default"/>
	<%@include file="/webpage/include/treeview.jsp" %>
	<style type="text/css">
		.ztree {overflow:auto;margin:0;_margin-top:10px;padding:10px 0 0 10px;}
	</style>
	<script type="text/javascript">
       /* function refresh(){//刷新
            window.location="${ctx}/sys/role/";
        }*/
       $(document).ready(function() {

       });
	</script>
	<style>
		body{
			background-color:transparent;
			filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#26FFFFFF, endColorstr=#26FFFFFF);
			color:#ffffff;
			background-color:rgba(255,255,255,0);
			height:100%;
		}
	</style>
</head>
<body>
<div class="wrapper wrapper-content full-width" id="divId">
	<sys:message content="${message}"/>
	<div id="content" class="pr full-height full-width">
		<div id="left"  class="contentShadow fl contents">
			<div class="" style="padding: 0;min-height: 0px;">
				<select id="tableName" onchange="getChange(this.value)" style="width: 70%;margin-left: 20px;margin-top: 5px" class=" form-control  simple-select">
					<%--<option value="1" label="">人员档案</option>--%>
					<c:forEach items="${modularList}" var="modularList" varStatus="index">
						<option value="${modularList.id}"<c:if test="${index.index} eq '0'">selected="selected"</c:if> tableName="${modularList.tableName}">${modularList.name}</option>
					</c:forEach>
				</select>
			</div>
			<div class="ztreeContainer" style="max-height: calc((100% - 8px) - 64px);">
				<div id="ztree" class="ztree leftBox-content"></div>
			</div>
		</div>
		<div id="right"  class="fl contents">
			<div class="layui-row contentShadow full-height tran-bg">
					<%--<iframe id="userContent" name="userContent" src="${ctx}/sys/role/list" width="100%" height="100%" frameborder="0"></iframe>--%>
						<form:form id="" modelAttribute="sysWarning" action="#" method="post" class="form-horizontal" >
						<table class="oa-table layui-table" id="contentTable">
							<thead>
							<tr>
								<th width="200px" style="text-align: center">左括号</th>
								<th width="200px"  style="text-align: center">逻辑关系</th>
								<th width="200px"  style="text-align: center">属性信息</th>
								<th width="200px"  style="text-align: center">查询匹配方式</th>
								<th width="200px"  style="text-align: center">匹配值</th>
								<th width="200px"  style="text-align: center">右括号</th>
								<th width="100px"  style="text-align: center">操作</th>
							</tr>
							</thead>
							<tbody id="field">

							</tbody>
						</table>
						<script type="text/template" id="field1Tpl">//<!--
                                    <tr id="field1{{idx}}">
                                        <td class="hide">
                                            <input id="field1{{idx}}_field" type="hidden" "/>
                                            <input id="field1{{idx}}_showType" type="hidden" "/>
                                        </td>
                                         <td>
                                        <form:select id="field1{{idx}}_1" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('left_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_2" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('condition')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <input id="field1{{idx}}_3"  style="width: 80%"   type="text"   readonly="true"   class="form-control "/>
                                        </td>
                                        <td>
                                             <form:select id="field1{{idx}}_4" path="" cssStyle="width: 80%" class="form-control required">
											<form:options items="${fns:getDictList('logic_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <input id="field1{{idx}}_5"   style="width: 80%" type="text"    class="form-control "/>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_6" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('right_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="field1{{idx}}_span" onclick="delRow(this, '#field1{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
						</script>
						<script type="text/template" id="field1Tp2">//<!--
                                    <tr id="field1{{idx}}">
                                        <td class="hide">
                                            <input id="field1{{idx}}_field" type="hidden" "/>
                                            <input id="field1{{idx}}_showType" type="hidden" "/>
                                        </td>
                                         <td>
                                        <form:select id="field1{{idx}}_1" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('left_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_2" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('condition')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <input id="field1{{idx}}_3"  style="width: 80%"   type="text"   readonly="true"   class="form-control "/>
                                        </td>
                                        <td >
                                        <form:select id="field1{{idx}}_4" path="" cssStyle="width: 80%" class="form-control required">
											<form:options items="${fns:getDictList('logic_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                         <td id="tdfield1{{idx}}_4">
                                        <form:select id="field1{{idx}}_5" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
										</form:select>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_6" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('right_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="field1{{idx}}_span" onclick="delRow(this, '#field1{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
						</script>
							<script type="text/template" id="field1Tp3">//<!--
                                    <tr id="field1{{idx}}">
                                     <td class="hide">
                                            <input id="field1{{idx}}_field" type="hidden" "/>
                                            <input id="field1{{idx}}_showType" type="hidden" "/>
                                        </td>
                                         <td>
                                        <form:select id="field1{{idx}}_1" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('left_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_2" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('condition')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <input id="field1{{idx}}_3"  style="width: 80%"   type="text"   readonly="true"   class="form-control "/>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_4" path="" cssStyle="width: 80%" class="form-control required">
											<form:options items="${fns:getDictList('logic_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <sys:treeselect id="field1{{idx}}_5" name="" value="" labelName="" labelValue=""
																title="部门" url="/sys/office/treeDataAll?type=2"  cssClass="form-control layui-input" allowClear="true" notAllowSelectParent="true"/>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_6" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('right_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="field1{{idx}}_span" onclick="delRow(this, '#field1{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
							</script>
							<script type="text/template" id="field1Tp4">//<!--
                                    <tr id="field1{{idx}}">
                                     <td class="hide">
                                            <input id="field1{{idx}}_field" type="hidden" "/>
                                            <input id="field1{{idx}}_showType" type="hidden" "/>
                                        </td>
                                         <td>
                                        <form:select id="field1{{idx}}_1" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('left_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_2" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('condition')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <input id="field1{{idx}}_3"  style="width: 80%"   type="text"   readonly="true"   class="form-control "/>
                                        </td>
                                        <td>
                                             <form:select id="field1{{idx}}_4" path="" cssStyle="width: 80%" class="form-control required">
											<form:options items="${fns:getDictList('logic_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td>
                                            <input id="field1{{idx}}_5"   style="width: 80%" type="text"    class="form-control "/>
                                        </td>
                                        <td>
                                        <form:select id="field1{{idx}}_6" path="" cssStyle="width: 80%" class="form-control required">
											<form:option value="" label=""/>
											<form:options items="${fns:getDictList('right_bracket')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
										</form:select>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="field1{{idx}}_span" onclick="delRow(this, '#field1{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
							</script>
						<script type="text/javascript">
                            var fieldRowIdx = 0;
                            field1Tpl = $("#field1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                            field1Tp2 = $("#field1Tp2").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                            field1Tp3 = $("#field1Tp3").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                            field1Tp4 = $("#field1Tp4").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                            $(document).ready(function() {
                            });
						</script></form:form>
			</div>
		</div>
	</div>
</div>
	<script type="text/javascript">
        function addDiyDom(treeId, treeNode) {
            var spaceWidth = 15;
            var switchObj = $("#" + treeNode.tId + "_switch"),
                icoObj = $("#" + treeNode.tId + "_ico");
            switchObj.remove();
            icoObj.before(switchObj);

            if (treeNode.level > 0) {
                var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
                switchObj.before(spaceStr);
            }
        }


        var setting = {data:{simpleData:{enable:true,idKey:"id",pIdKey:"pId",rootPId:'0'}},
            callback:{onClick:function(event, treeId, treeNode){
                var id = treeNode.id == '0' ? '' :treeNode.id;
                    var name = treeNode.name ;
                   $.ajax({
                        type:'post',
                        url:'${ctx}/syswarning/sysWarning/findField',
                        data:{
                            "id":id
                        },
                        success:function(data){
                            var type=data.showType;
                            var logic=data.logic;
                            if(type=='2'){//下拉框
                                addRow('#field', fieldRowIdx, field1Tp2);
                                if(data.dictList!=null){
                                    var optionString="";
                                    var list=data.dictList;
                                    for(var i=0;i<list.length;i++){
                                        optionString +="<option value=\""+list[i].value+"\">"+list[i].label+"</option>";  //动态添加数据
									}
                                    $("#field1"+fieldRowIdx+"_5").html(optionString);
								}
								fieldRowIdx = fieldRowIdx + 1;
							}else if(type=='3'){//部门选择树
                                addRow('#field', fieldRowIdx, field1Tp3);
                                fieldRowIdx = fieldRowIdx + 1;
                            }else{//默认文本输入框
                                addRow('#field', fieldRowIdx, field1Tpl);
                                fieldRowIdx = fieldRowIdx + 1;
							}
                            var row = fieldRowIdx - 1 ;
                            $("#field1"+row+"_3").val(data.notes);
                            $("#field1"+row+"_field").val(data.field);
                            $("#field1"+row+"_showType").val(data.showType);
                        }
                    })

            }
            }
            ,view:{
                showLine: false,
                showIcon: false,
                addDiyDom: addDiyDom
            }
        };

        function refreshTree(){
            $.getJSON("${ctx}/syswarning/sysWarning/getField",function(data){
                $.fn.zTree.init($("#ztree"), setting, data).expandAll(true);
            });
        }
        refreshTree();
        function getChange(value){
            $.getJSON(
                "${ctx}/syswarning/sysWarning/getField?modularId="+value,
				function(data){
                $.fn.zTree.init($("#ztree"), setting, data).expandAll(true);
                    for(var i=0;i<fieldRowIdx;i++){
                        delRow($("#field1"+i+"_span"),"#field1"+i);
                    }
            });
        }
        function addRow(list, idx, tpl, row){
            var idx1 = $("#field tr").length;
            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");
                $(obj).parent().parent().remove();
        }

        function getSelectedItem(){
            var idx1 = $("#field tr").length;
            if(idx1 == 0 ){
                top.layer.alert('请至少添加一条数据!', {icon: 0, title:'警告'});
                return "-1";
            }
            var arr=[];
            var sqlString="";
            sqlString+=" select count(*) as count from "+($("#tableName option:selected").attr("tableName"))+" where ";
            var label="";
            for(var i=0;i<fieldRowIdx;i++){
                var tr=$("#field1"+i).html();
                if(tr!=undefined){
                    var showType= $("#field1"+i+"_showType").val();
                    sqlString+=$("#field1"+i+"_1").val()+" ";
                    sqlString+=$("#field1"+i+"_2").val()+" ";
                    label+=$("#field1"+i+"_2"+" option:selected").text()+" ";
                    sqlString+=$("#field1"+i+"_field").val()+" ";
                    label+=$("#field1"+i+"_3").val()+" ";
                    var logic=$("#field1"+i+"_4").val();//匹配逻辑
                    var notes=$("#field1"+i+"_3").val();//属性信息
                    if(logic=="left like"||logic=="right like"){
                        sqlString+=" like ";
					}else{
                        sqlString+=logic+" ";
					}
                    label+=logic+" ";
                    if(showType=="2"){
                        var val=$("#field1"+i+"_5"+" option:selected").val();
                        if(logic=="is null"||logic=="is not null"){
                        }else{
                            if(val==""||val==null){
                                top.layer.alert("【"+notes+"】"+"的匹配值不能为空",{icon:0,title:'系统提示'});
                                return -1;
                            }
                            if(logic=="like"){
                                val='%'+val+'%';
                            }else if(logic=="left like"){
                                val=val+'%';
                            }else if(logic=="right like"){
                                val='%'+val;
                            }
                            sqlString+="'"+val+"'"+" ";
                            label+=$("#field1"+i+"_5"+" option:selected").text()+" ";
                        }
                    }else if(showType=="3"){
                        var val=$("#field1"+i+"_5"+"Id").val();
                        if(logic=="is null"||logic=="is not null"){
                        }else{
                            if(val==""||val==null){
                                top.layer.alert("【"+notes+"】"+"的匹配值不能为空",{icon:0,title:'系统提示'});
                                return -1;
                            }
                            if(logic=="like"){
                                val='%'+val+'%';
                            }else if(logic=="left like"){
                                val=val+'%';
                            }else if(logic=="right like"){
                                val='%'+val;
                            }
                            sqlString+="'"+val+"'"+" ";
                            label+=$("#field1"+i+"_5"+"Name").val()+" ";
                        }
                    }else if(showType=="4"){//时间类型
                        var val=$("#field1"+i+"_5").val();//匹配值
                        if(logic=="is null"||logic=="is not null"){
						}else{
                            if(val==""||val==null){
                                top.layer.alert("【"+notes+"】"+"的匹配值不能为空",{icon:0,title:'系统提示'});
                                return -1;
                            }
                            label+=val+" ";
                            if(isNaN(val)&&!isNaN(Date.parse(val))){
                               val="DATE_FORMAT("+"'"+val+"'"+","+ "'"+"%Y-%m-%d"+"'"+")";
                            }else if(val=="今天"){
                               val="DATE_FORMAT(now(),"+ "'"+"%Y-%m-%d"+"'"+")";
							}else if(isRealNum(val)){
                                val="date_add(DATE_FORMAT(now(),"+ "'"+"%Y-%m-%d"+"'"+")"+", interval "+val+" day) ";
                            }else{
                                top.layer.alert("【"+notes+"】"+"的匹配值填写规则时间格式例“2018-01-01”或“今天”或数字",{icon:0,title:'系统提示'});
                                return -1;
							}
                            sqlString+=val+" ";
                          /*  if(logic=="like"){
                                val='%'+val+'%';
							}else if(logic=="left like"){
                                val=val+'%';
							}else if(logic=="right like"){
                                val='%'+val;
                            }*/
                           // sqlString+="'"+val+"'"+" ";
						}
                    }else{//默认普通文本输入框
                        var val=$("#field1"+i+"_5").val();//匹配值
                        if(logic=="is null"||logic=="is not null"){
                        }else{
                            if(val==""||val==null){
                                top.layer.alert("【"+notes+"】"+"的匹配值不能为空",{icon:0,title:'系统提示'});
                                return -1;
                            }
                            label+=val+" ";
                            if(logic=="like"){
                                val='%'+val+'%';
                            }else if(logic=="left like"){
                                val=val+'%';
                            }else if(logic=="right like"){
                                val='%'+val;
                            }
                            sqlString+="'"+val+"'"+" ";
                        }
                    }
                    sqlString+=$("#field1"+i+"_6").val()+" ";
				}
			}
            var modularId=$("#tableName option:selected").val();
            arr.push(sqlString);
            arr.push(label);
            arr.push(modularId);
            return arr;
        }
        function isRealNum(val){
            // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
            if(val === "" || val ==null){
                return false;
            }
            if(!isNaN(val)){
                return true;
            }else{
                return false;
            }
        }
        function IsDate(val) {
            var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
            var str = val;
            var arr = reg.exec(str);
            if (str=="") return true;
            if (!reg.test(str)&&RegExp.$2<=12&&RegExp.$3<=31){
                return false;
            }
            return true;
        }
	</script>
	<%--<script src="${ctxStatic}/common/wsize.min.js" type="text/javascript"></script>--%>
</body>
</html>