<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>报销单管理</title>
	<meta name="decorator" content="default"/>
	<style>
		label.error{
			top:40px;
			left:0;
		}
		#rules-error{
			top:80px;
		}
	</style>
	<script type="text/javascript">
        var validateForm;
        var count = 0;
        $(document).ready(function() {
            if($("#createDate").val()==null || $("#createDate").val()==''){
                $("#createDate").val(getNowFormatDate());
            }

            var boxObj = $("input:checkbox[name='reminding']"); //获取所有的复选框值
            var expresslist = '${sysWarning.reminding}'; //用el表达式获取在控制层存放的复选框的值为字符串类型
            var express = expresslist.split(',');
            $.each(express, function(index, expressId){
                boxObj.each(function () {
                    if($(this).val() == expressId) {
                        $(this).iCheck('check')
                    }
                });
            });

            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);
                    }
                }
            });

            laydate.render({
                elem: '#warningDate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
                format: 'yyyy-MM-dd HH:mm',
                type : 'datetime'
            });

        });
        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 = $("#workMonthlyPlanThisList tr").length;
            //tpl = tpl.replace("workMonthlyPlanThisList[0]_workClientInfoId","workMonthlyPlanThisList["+idx1+"].workClientInfoId");

            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 isNumber(val){
            if(val === "" || val ==null){
                return false;
            }
            var regPos = /^\d+(\.\d+)?$/; //非负浮点数
            var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
            if(regPos.test(val) || regNeg.test(val)){
                return true;
            }else{
                return false;
            }

        }
        function selectRoleInfo(d){
            addRow('#sysWarningRoleList', sysWarningRoleListRowIdx, sysWarningRoleListTpl);
            sysWarningRoleListRowIdx = sysWarningRoleListRowIdx + 1;
            var row = sysWarningRoleListRowIdx - 1 ;
            $("#"+"sysWarningRoleList"+row+"_roleId").val(d.id);
            $("#"+"sysWarningRoleList"+row+"_1").val(d.name);
            $("#"+"sysWarningRoleList"+row+"_2").val(d.type);
        }
        function selectUserInfo(d){
            addRow('#sysWarningUserList', sysWarningUserListRowIdx, sysWarningUserListTpl);
            sysWarningUserListRowIdx = sysWarningUserListRowIdx + 1;
            var row = sysWarningUserListRowIdx - 1 ;
            $("#"+"sysWarningUserList"+row+"_userId").val(d.userId);
            $("#"+"sysWarningUserList"+row+"_1").val(d.name);
            $("#"+"sysWarningUserList"+row+"_2").val(d.officeName);
        }
        function openDialogreRules(title,url,width,height,target){
            if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
                width='auto';
                height='auto';
            }else{//如果是PC端，根据用户设置的width和height显示。

            }
            top.layer.open({
                type: 2,
                area: ['95%','95%'],
                title:title,
                skin:"two-btns",
                name:'friend',
                content: encodeURI("${ctx}/syswarning/sysWarning/rulesIndex") ,
                btn: ['确定', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    var item = iframeWin.getSelectedItem();
                    if(item == "-1"){
                        return;
                    }
                    console.info(item);
                    $.ajax({
                        type:'post',
                        url:'${ctx}/syswarning/sysWarning/sqlString',
                        data:{
                            "sqlString":item[0]
                        },
                        success:function(data){
                            if(data){
                               // $("#sqlString").val(item[0]);
                                $("#rules").val(item[1]);
                                $("#modularId").val(item[2]);
                                $.ajax({
                                    type:'post',
                                    url:'${ctx}/syswarning/sysWarning/modularSql',
                                    data:{
                                        "modularSql":item[2]
                                    },
                                    success:function(str){
                                            $("#sqlString").val(item[0]+=str);
                                            top.layer.close(index);//关闭对话框。
                                    }
                                })
                                top.layer.close(index);//关闭对话框。
							}else{
                                top.layer.alert('预警规则设置有误，请核查！生成SQL如下:【'+item[0]+'】',{icon:0,title:'系统提示'});
							}
                        }
                    })
                },
                cancel: function(index){
                }
            });
        }
	</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="sysWarning" action="${ctx}/syswarning/sysWarning/save" method="post" class="form-horizontal" >
			<form:hidden path="id"/>
			<form:hidden path="modularId" id="modularId"/>
			<%--<form:hidden path="sqlString" id="sqlString"/>--%>
			<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:select path="warningType" class="form-control required  simple-select">
							<form:option value="" label=""/>
							<form:options items="${fns:getDictList('warning_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</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">
						<div class="input-group">
							<form:input path="name" htmlEscape="false" class="form-control  layui-input"/>
							<span class="input-group-btn">
                                <label class="form-status"><c:choose><c:when test="${not empty sysWarning.state and sysWarning.state eq '0'}">禁用</c:when><c:otherwise>启用</c:otherwise></c:choose></label>
                             </span>
						</div>
					</div>
				</div>
				<div class="layui-item query layui-col-sm6">
					<label class="layui-form-label">预警频率：</label>
					<div class="layui-input-block">
						<form:select path="frequency"  class="form-control required query-group  simple-select">
							<form:option value="" label=""/>
							<form:options items="${fns:getDictList('frequency')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
                        <span class="group-sep">-</span>
                        <input id="warningDate"  name="warningDate" type="text" readonly="readonly" maxlength="20" class="laydate-icondate form-control layer-date layui-input laydate-icon query-group"
                               value="<fmt:formatDate value="${sysWarning.warningDate}" pattern="yyyy-MM-dd hh:mm"/>"/>
                        </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 type="checkbox" class="i-checks" name="reminding" disabled="disabled" checked id="isAssets1" value="1">&nbsp;&nbsp;&nbsp;
						<label for="isAssets1">系统通知</label>&nbsp;&nbsp;&nbsp;
						<input type="checkbox" class="i-checks" name="reminding"  id="isAssets2" value="2">&nbsp;&nbsp;&nbsp;
						<label for="isAssets2">邮件提醒</label>&nbsp;&nbsp;&nbsp;
						<input type="checkbox" class="i-checks" name="reminding"  id="isAssets3" value="3">&nbsp;&nbsp;&nbsp;
						<label for="isAssets3">短信提醒</label>&nbsp;&nbsp;&nbsp;
						<input type="checkbox" class="i-checks" name="reminding"  disabled="disabled" id="isAssets4" value="4">&nbsp;&nbsp;&nbsp;
						<label for="isAssets4">微信提醒</label>&nbsp;&nbsp;&nbsp;
						<%--<form:select path="reminding"  multiple="true" class="form-control required  simple-select">
							<form:option value="" label=""/>
							<form:options items="${fns:getDictList('reminding')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>--%>
					</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 with-icon">
						<sys:treeselect id="scope" name="scope" value="${sysWarning.scope}" labelName="scopeName" labelValue="${sysWarning.scopeName}"
										title="分公司" url="/sys/office/treeDataCompany?type=2" cssClass="form-control required layui-input" allowClear="true" notAllowSelectParent="false"/>
						<%--<sys:treeselect id="scope" name="scope" value="${sysWarning.scope}" labelName="scopeName" labelValue="${sysWarning.scopeName}"
										title="部门" url="/sys/office/treeData?type=2" cssClass="form-control required layui-input" allowClear="true" notAllowSelectParent="true" />--%>
					</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" readonly="true" type="text"  maxlength="20" class="laydate-icondate form-control layer-date  layui-input laydate-icon"
							   value="<fmt:formatDate value="${sysWarning.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="comment" htmlEscape="false" rows="4"    maxlength="255" class="form-control "/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12  with-textarea">
					<label class="layui-form-label"><a href="javascript:void(0)" onclick="openDialogreRules('设置预警规则', '','90%','90%')" style="background-color: #0bbbee" class="nav-btn" >设置规则</a></label>
					<div class="layui-input-block">
						<form:textarea path="rules" htmlEscape="false" rows="4" id="rules" readonly="true"   maxlength="255" class="form-control required"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12  with-textarea">
					<label class="layui-form-label">对应SQL：</label>
					<div class="layui-input-block">
						<form:textarea path="sqlString" id="sqlString" htmlEscape="false" rows="4"  readonly="true"  maxlength="1000" 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">
                    <sys:gridSelectRole id="" name="" value="" labelName="name" labelValue=""
                                            title="角色列表" url="${ctx}/syswarning/sysWarning/selectRole" cssClass="form-control required layui-input" isAll="true"  isSingle="true"/>

				</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">角色分类</th>
							<th width="200px">操作</th>
						</tr>
						</thead>
						<tbody id="sysWarningRoleList">
						<c:if test="${not empty sysWarning.sysWarningRoleList}">
							<c:forEach items="${sysWarning.sysWarningRoleList}" var="sysWarningRole" varStatus="index">
								<tr id="sysWarningRoleList${index.index}">
									<td class="hide">
										<input id="sysWarningRoleList${index.index}_id" name="sysWarningRoleList[${index.index}].id" type="hidden" value="${sysWarningRole.id}"/>
										<input id="sysWarningRoleList${index.index}_warningId" name="sysWarningRoleList[${index.index}].warningId" type="hidden" value="${sysWarningRole.warningId}"/>
                                        <input id="sysWarningRoleList${index.index}_roleId" name="sysWarningRoleList[${index.index}].role.id" type="hidden" value="${sysWarningRole.role.id}"/>
										<input id="sysWarningRoleList${index.index}_delFlag" name="sysWarningRoleList[${index.index}].delFlag" type="hidden" value="0"/>
									</td>
									<td>
										<input id="sysWarningRoleList${index.index}_1"  readonly="true"    type="text" value="${sysWarningRole.role.name}"   class="form-control "/>
									</td>
									<td>
										<input id="sysWarningRoleList${index.index}_2"  readonly="true"    type="text" value="${fns:getDictLabel(sysWarningRole.role.type, 'sys_role_type', '')}"   class="form-control "/>
									</td>
									<td class="text-center op-td" >
										<span class="op-btn op-btn-delete" id="sysWarningRoleList${index.index}_span" onclick="delRow(this, '#sysWarningRoleList${index.index}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>
									</td>
								</tr>
							</c:forEach>
						</c:if>
						</tbody>
					</table>
					<script type="text/template" id="sysWarningRoleListTpl">//<!--
                                    <tr id="sysWarningRoleList{{idx}}">
                                        <td class="hide">
                                            <input id="sysWarningRoleList{{idx}}_id" name="sysWarningRoleList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                            <input id="sysWarningRoleList{{idx}}_warningId" name="sysWarningRoleList[{{idx}}].warningId" type="hidden" value="{{row.warningId}}"/>
                                            <input id="sysWarningRoleList{{idx}}_roleId" name="sysWarningRoleList[{{idx}}].role.id" type="hidden" value="{{row.role.id}}"/>
                                            <input id="sysWarningRoleList{{idx}}_delFlag" name="sysWarningRoleList[{{idx}}].delFlag" type="hidden" value="0"/>
                                        </td>
                                        <td>
                                            <input id="sysWarningRoleList{{idx}}_1"   name="sysWarningRoleList[{{idx}}].role.name" type="text" value="{{row.role.name}}"  readonly="true"   class="form-control "/>
                                        </td>
                                        <td>
                                            <input id="sysWarningRoleList{{idx}}_2"  name="sysWarningRoleList[{{idx}}].role.name" type="text" value="{{row.role.name}}"   readonly="true"  class="form-control "/>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="sysWarningRoleList{{idx}}_span" onclick="delRow(this, '#sysWarningRoleList{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
					</script>
					<script type="text/javascript">
                        var sysWarningRoleListRowIdx = ${fn:length(sysWarning.sysWarningRoleList)}, sysWarningRoleListTpl = $("#sysWarningRoleListTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                        if($("#id").val()){
                            sysWarningRoleListIdx = ${fn:length(sysWarning.sysWarningRoleList)};
                        }
                        $(document).ready(function() {
                        });
					</script>
				</div>
			</div>

			<div class="form-group layui-row">
				<div class="form-group-label"><h2>预警对象-人员</h2></div>
                <div class="layui-item nav-btns">
                    <sys:gridSelectWarningUser id="userId" name="userId" value="${workRegularApply.userId}" labelName="name" labelValue="${workRegularApply.workStaffBasicInfo.name}"
                                        title="人员列表" url="${ctx}/syswarning/sysWarning/gridSelectUser" cssClass="form-control required layui-input" isAll="true"  isSingle="true"/>
                </div>
				<div class="layui-item layui-col-xs12 form-table-container" >
					<table id="contentTable1" class="table table-bordered table-condensed can-edit no-bottom-margin">
						<thead>
						<tr>
							<th width="200px">姓名</th>
							<th width="200px">部门</th>
							<th width="200px">操作</th>
						</tr>
						</thead>
						<tbody id="sysWarningUserList">
						<c:if test="${not empty sysWarning.sysWarningUserList}">
							<c:forEach items="${sysWarning.sysWarningUserList}" var="sysWarningUser" varStatus="index">
								<tr id="sysWarningUserList${index.index}">
									<td class="hide">
										<input id="sysWarningUserList${index.index}_id" name="sysWarningUserList[${index.index}].id" type="hidden" value="${sysWarningUser.id}"/>
										<input id="sysWarningUserList${index.index}_warningId" name="sysWarningUserList[${index.index}].warningId" type="hidden" value="${sysWarningUser.warningId}"/>
                                        <input id="sysWarningUserList${index.index}_userId" name="sysWarningUserList[${index.index}].user.id" type="hidden" value="${sysWarningUser.user.id}"/>
										<input id="sysWarningUserList${index.index}_delFlag" name="sysWarningUserList[${index.index}].delFlag" type="hidden" value="0"/>
									</td>
									<td>
										<input id="sysWarningUserList${index.index}_1"  readonly="true"   name="sysWarningUserList[${index.index}].user.name" type="text" value="${sysWarningUser.user.name}"   class="form-control "/>
									</td>
									<td>
										<input id="sysWarningUserList${index.index}_2"  readonly="true"   name="sysWarningUserList[${index.index}].user.office.name" type="text" value="${sysWarningUser.user.office.name}"   class="form-control "/>
									</td>
                                    <td class="text-center op-td" >
                                        <span class="op-btn op-btn-delete" id="sysWarningUserList${index.index}_span" onclick="delRow(this, '#sysWarningUserList${index.index}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>
                                    </td>
								</tr>
							</c:forEach>
						</c:if>
						</tbody>
					</table>
					<script type="text/template" id="sysWarningUserListTpl">//<!--
                                    <tr id="sysWarningUserList{{idx}}">
                                        <td class="hide">
                                            <input id="sysWarningUserList{{idx}}_id" name="sysWarningUserList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                            <input id="sysWarningUserList{{idx}}_warningId" name="sysWarningUserList[{{idx}}].warningId" type="hidden" value="{{row.warningId}}"/>
                                            <input id="sysWarningUserList{{idx}}_userId" name="sysWarningUserList[{{idx}}].user.id" type="hidden" value="{{row.user.id}}"/>
                                            <input id="sysWarningUserList{{idx}}_delFlag" name="sysWarningUserList[{{idx}}].delFlag" type="hidden" value="0"/>
                                        </td>
                                        <td>
                                            <input id="sysWarningUserList{{idx}}_1"   name="sysWarningUserList[{{idx}}].user.name" type="text" value="{{row.user.name}}"  readonly="true"   class="form-control "/>
                                        </td>
                                        <td>
                                            <input id="sysWarningUserList{{idx}}_2"  name="sysWarningUserList[{{idx}}].user.office.name" type="text" value="{{row.user.office.name}}"   readonly="true"  class="form-control "/>
                                        </td>
                                        <td class="text-center op-td" >
                                            {{#delBtn}}<span class="op-btn op-btn-delete" id="sysWarningUserList{{idx}}_span" onclick="delRow(this, '#sysWarningUserList{{idx}}')" title="删除"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</span>{{/delBtn}}
                                        </td>
                                    </tr>//-->
					</script>
					<script type="text/javascript">
                        var sysWarningUserListRowIdx = ${fn:length(sysWarning.sysWarningUserList)}, sysWarningUserListTpl = $("#sysWarningUserListTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                        if($("#id").val()){
                            sysWarningUserListIdx = ${fn:length(sysWarning.sysWarningUserList)};
                        }
                        $(document).ready(function() {
                        });
					</script>
				</div>
			</div>
			<div class="form-group layui-row page-end"></div>
		</form:form>
	</div>
</div>

</body>
</html>