<%@ page import="com.jeeplus.modules.sys.utils.UserUtils" %>
<%@ page import="com.jeeplus.modules.sys.entity.Role" %>
<%@ page import="java.util.List" %>
<%@ page import="com.jeeplus.modules.sys.entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<%
	boolean admin = UserUtils.getUser().isAdmin();
%>
<html>
<head>
	<title>字典管理</title>
	<meta name="decorator" content="default"/>
	<%@include file="/webpage/include/treetable.jsp" %>
	<script type="text/javascript">
        $(document).ready(function() {
            // $("#treeTable").treeTable({expandLevel : 2},{ expandable: true });
        });
	</script>
	<script type="text/javascript">
		var validateForm;
		function doSubmit(){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $("#inputForm").submit();
			  return true;
		  }
	
		  return false;
		}
		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				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);
					}
				}
			});
            getDictDetail()
		});

        function openDialogDict(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: [width, height],
                title: title,
                maxmin: true, //开启最大化最小化按钮
                content: url ,
                btn: ['提交', '关闭'],
                yes: function(index, layero){
                    var body = top.layer.getChildFrame('body', index);
                    var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    var inputForm = body.find('#inputForm');
                    var top_iframe;
                    if(target){
                        top_iframe = target;//如果指定了iframe，则在改frame中跳转
                    }else{
                        top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe
                    }
                    inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
					var $document = iframeWin.contentWindow.document;
                    var index1 = parent.layer.load(0, {shade: [0.1, 'tranparent']});
					formSubmit($document,"inputForm",index,index1);
                },
                cancel: function(index){
                }
            });

        }
        function formSubmit($document,inputForm,index,index1){
            var validateForm = $($document.getElementById(inputForm)).validate({
                submitHandler: function(form){
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $($document.getElementById("#messageBox")).text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
            if(validateForm.form()){
                $($document.getElementById(inputForm)).ajaxSubmit({
                    success:function(data) {
                        getDictDetail()
                        top.layer.close(index);
                        parent.layer.msg("操作成功",{icon:1})
						parent.layer.close(index1)
                    },error:function(){
                        parent.layer.msg("操作失败",{icon:2})
                        parent.layer.close(index1)
					}
                });
            }else {
                parent.layer.msg("信息未填写完整!", {icon: 5});
            }
        }
        /**
		 * 获取明细数据
         */
		function getDictDetail(){
            var company=$("#company").val();
            var index = parent.layer.load(0, {shade: [0.1, 'tranparent']});
		    $.ajax({
				type:'post',
				url:'${ctx}/sys/mainDictDetail/getDictDetailDefalt',
				data:{
					"dictId":"${mainDict.id}",
					"company":company
				},
				success:function(data){

                    $("#treeTable tbody").empty();
                    var list=data.list;
                    console.info(list);
                    var rows="";
                    for(var i=0;i<list.length;i++){
                        rows+="<tr id="+list[i].id+" pid="+list[i].parentId +">"
                        rows+="<td>"+list[i].detailName +"</td>"
                        rows+="<td style=\"text-align: center\">"+list[i].detailKey +"</td>"
                        rows+="<td style=\"text-align: center\">"+list[i].remarks +"</td>"
                        rows+= "<td style=\"text-align: center\">"+list[i].sort +"</td>"
                        rows+= "<td style=\"text-align: center\">"
                        if(list[i].delFlags==0){
                            rows+="正常";
                        }else{
                            rows+="已移除";
                        }
                        rows+=	"</td>"
                    }
                    $("#treeTable").append(rows);
                    $('#treeTable').treeTable({expandLevel : 2},{ expandable: true });
                    parent.layer.close(index);
				},
                error:function(){
                    parent.layer.close(index)
                }
			})
		}
        /**
         * 删除数据
         */
        function updateDelFlag(id,delflag,endFlag){
			if(endFlag == 0){
			    parent.layer.msg("该节点有子节点，不能移除",{icon:2});
			    return;
			}
            var index = parent.layer.load(0, {shade: [0.1, 'tranparent']});
            $.ajax({
                type:'post',
                url:'${ctx}/sys/mainDictDetail/updateDelFlag',
                data:{
                    "dictDetailId":id,
					"delflag":delflag
                },
                success:function(data){
					if(data.flag){
                        getDictDetail();
                        parent.layer.msg("操作成功",{icon:1});
					}else{
                        parent.layer.msg("操作失败",{icon:2});
					}
					parent.layer.close(index)
                },
				error:function(){
                    parent.layer.close(index)
				}
            })
        }
	</script>
</head>
<body>
<div class="single-form">
	<div class="container view-form">
		<form:form id="inputForm" modelAttribute="mainDict" action="${ctx}/sys/mainDict/save" 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">
					<label class="layui-form-label">类型名称：</label>
					<div class="layui-input-block">
						<input readonly="true" htmlEscape="false" class="form-control layui-input" value="${mainDict.typeName}"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">类型：</label>
					<div class="layui-input-block">
						<input readonly="true" htmlEscape="false" class="form-control layui-input" value="${mainDict.typeName}"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">类型排序：</label>
					<div class="layui-input-block">
						<input readonly="true" htmlEscape="false" class="form-control layui-input" value="${mainDict.typeSort}"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">定义方式：</label>
					<div class="layui-input-block">
						<input readonly="true" htmlEscape="false" class="form-control layui-input" value="${fns:getDictLabel(mainDict.dictType, 'main_type', '')}"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm6">
					<label class="layui-form-label">所属公司：</label>
					<div class="layui-input-block">
						<input readonly="true" id="company" htmlEscape="false" class="hidden layui-input" value="${mainDict.office.id}"/>
						<input readonly="true" htmlEscape="false" class="form-control layui-input" value="${mainDict.office.name}"/>
					</div>
				</div>
				<div class="layui-item layui-col-sm12 with-textarea">
					<label class="layui-form-label">类型描述：</label>
					<div class="layui-input-block">
						<textarea readonly="true" htmlEscape="false" rows="4" class="form-control ">${mainDict.remarks}</textarea>
					</div>
				</div>
			</div>
		</form:form>
			<div class="form-group layui-row">
				<div class="form-group-label"><h2>详细信息</h2></div>
				<c:if test="'<%=admin%>' == 'true'">
					<div class="layui-item nav-btns">
						<button class="nav-btn nav-btn-add"  onclick="openDialogDict('字典明细新增','${ctx}/sys/mainDictDetail/form?dictId=${mainDict.id}','90%', '90%');" title="添加"><i class="fa fa-plus"></i> 添加</button>
					</div>
				</c:if>
				<div class="layui-item layui-col-xs12 form-table-container" >
					<table id="treeTable" class="table table-bordered table-hover list-table">
						<thead>
						<tr>
							<th  width="40%">名称</th>
							<th  >键值</th>
							<th >描述</th>
							<th >排序</th>
							<th width="10%">删除标记</th>
						</tr>
						</thead>

					</table>
				</div>
			</div>
			<div class="form-group layui-row page-end"></div>
	</div>
</div>
</body>
</html>