<%@ 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/treetable.jsp" %>
	<script type="text/javascript">
		$(document).ready(function() {
            $("th").css('text-align','center');
			var tpl = $("#treeTableTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
			var data = ${fns:toJson(list)}, rootId = "${not empty office.id ? office.id : '0'}";
            //console.log(data+"-----"+rootId);
            addRow("#treeTableList", tpl, data, rootId, true);
            $("#treeTable").treeTable({expandLevel : 2});

            $("a").on("click",addLinkVisied);
            stateBtn("#treeTableList")

        });
		function stateBtn(list) {
            //隐藏对应状态按钮
            var tableLists = $(list).context.getElementById('treeTableList').getElementsByTagName('tr');
            var arr = [];
            for (var i = 0, length = tableLists.length; i < length; i++) {
                arr.push(tableLists[i]);
                var organizStateText = $(arr[i]).children('td').eq(3).text();
                if(organizStateText=='启用'){
                    $(arr[i]).children('td').eq(4).find('a').eq(1).hide();
                }else if(organizStateText=='禁用'){
                    $(arr[i]).children('td').eq(4).find('a').eq(2).hide();
				}
            }

        }

        function addRow(list, tpl, data, pid, root){
            /*console.log(list+"---List--");
            console.log(data+"---data--");
            console.log(pid+"---pid--");
            console.log(root+"---root--");*/
            for (var i=0; i<data.length; i++){
				var row = data[i];
                if ((${fns:jsGetVal('row.parentId')}) == pid){
                    //console.log(pid+"***********");
					$(list).append(Mustache.render(tpl, {
						dict: {
							type: getDictLabel(${fns:toJson(fns:getDictList('sys_office_type'))}, row.type),
                            useable: getDictLabel(${fns:toJson(fns:getDictList('office_useable'))}, row.useable)
						}, pid: (root?0:pid), row: row
					}));
                    //console.log(row.id+"+++++++++++");
					addRow(list, tpl, data, row.id);
				}
			}
		}
        function openDialogres(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,
                skin: 'three-btns',
                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中展示
                    if(iframeWin.contentWindow.doSubmit(1) ){
                        //top.layer.close(index);//关闭对话框。
                        setTimeout(function(){top.layer.close(index)}, 100);//延时0.1秒，对应360 7.1版本bug
                    }
                },
                no: function(index){
                }
            });
        }
		function refresh(){//刷新或者排序，页码不清零
    		
			window.location="${ctx}/sys/office/list";
    	}
	</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">
	<sys:message content="${message}"/>
	<div class="layui-row">
		<div class="full-width fl">
			<div class="contentShadow layui-row" id="queryDiv">
				<form:form id="searchForm" modelAttribute="office" action="${ctx}/sys/office/" method="post" class="form-inline">
					<div class="commonQuery">
						<div class="layui-item query athird">
							<label class="layui-form-label">机构名称：</label>
							<div class="layui-input-block with-icon">
								<form:input path="name" htmlEscape="false" class=" form-control layui-input"></form:input>
							</div>
						</div>
						<div class="layui-item query athird">
							<label class="layui-form-label">机构全称：</label>
							<div class="layui-input-block">
								<form:input path="topCompany" htmlEscape="false" class=" form-control layui-input"></form:input>
							</div>
						</div>
						<div class="layui-item athird">
							<div class="input-group">
								<button id="searchReset" style="margin-right: 10px;" class="fixed-btn searchReset fr" onclick="resetSearch()">重置</button>
								<button id="searchQuery" class="fixed-btn searchQuery fr" onclick="search()">查询</button>
							</div>
						</div>
						<div style="    clear:both;"></div>
					</div>
				</form:form>
			</div>
		</div>
		<div class="contentShadow shadowT full-width fl" style="margin-top: 0px;">
			<div class="layui-form contentDetails">
				<div class="nav-btns">
					<%--<shiro:hasPermission name="sys:office:add">
						<table:addRow url="${ctx}/sys/office/form?parent.id=${office.id}" title="机构" target=""></table:addRow><!-- 增加按钮 -->
					</shiro:hasPermission>--%>
					<button class="nav-btn nav-btn-refresh" data-toggle="tooltip" data-placement="left" onclick="sortOrRefresh()" title="刷新"><i class="glyphicon glyphicon-repeat"></i>&nbsp;刷新</button>
					<div style="clear: both;"></div>
				</div>

				<table id="treeTable" class="table table-striped table-bordered table-hover table-condensed dataTables-example dataTable">
					<thead><tr><th>机构名称</th><th>机构全称</th><th>机构类型</th><th>机构状态</th><shiro:hasPermission name="sys:office:edit"><th width="330">操作</th></shiro:hasPermission></tr></thead>
					<tbody id="treeTableList"></tbody>
				</table>
				<script type="text/template" id="treeTableTpl">
					<tr id="{{row.id}}" pId="{{pid}}">
						<td><a  class="attention-info" href="javascript:void(0)" onclick="openDialogView('查看机构', '${ctx}/sys/office/form?id={{row.id}}&&view=view','95%','95%')">{{row.name}}</a></td>
						<td>{{row.topCompany}}</td>
						<td>{{dict.type}}</td>
						<td>{{dict.useable}}</td>
						<%--<td>禁用</td>--%>
						<td class="op-td text-center">
							<div class="op-btn-box">
							<%--<shiro:hasPermission name="sys:office:view">
								<a href="javascript:void(0)" onclick="openDialogView('查看机构', '${ctx}/sys/office/form?id={{row.id}}','80%','80%')" class="btn btn-info btn-xs" ><i class="fa fa-search-plus"></i> 查看</a>
							</shiro:hasPermission>--%>
								<shiro:hasPermission name="sys:office:edit">
								<a href="javascript:void(0)" onclick="openDialogres('修改机构', '${ctx}/sys/office/form?id={{row.id}}','95%','95%', '')" class="op-btn op-btn-edit" ><i class="fa fa-edit"></i> 修改</a>
								<a href="${ctx}/sys/office/deleteUseable?id={{row.id}}&useable=1" onclick="return confirmx('要启用该机构及所有子机构项吗？', this.href)" class="op-btn op-btn-edit"><i class="fa fa-plus"></i> 启用</a>
							</shiro:hasPermission>
							<shiro:hasPermission name="sys:office:del">
								<a href="${ctx}/sys/office/deleteUseable?id={{row.id}}&useable=2" onclick="return confirmx('要禁用该机构及所有子机构项吗？', this.href)" class="op-btn op-btn-delete"><i class="fa fa-trash"></i> 禁用</a>
								<a href="${ctx}/sys/office/delete?id={{row.id}}" onclick="return confirmx('要删除该机构及所有子机构项吗？', this.href)" class="op-btn op-btn-delete"><i class="fa fa-trash"></i> 删除</a>
							</shiro:hasPermission>
							<shiro:hasPermission name="sys:office:add">
								<a href="javascript:void(0)" onclick="addSonNode('{{row.id}}');" class="op-btn  op-btn-add"><i class="fa fa-plus"></i> 添加下级机构</a>
							</shiro:hasPermission>
							</div>
						</td>
					</tr>
				</script>
			</div>
		</div>
	</div>
</div>
<script>

    resizeListWindow3();
    $(window).resize(function(){
        resizeListWindow3();
    });

    function addSonNode(rowId) {
        $.ajax({
            url:"${ctx}/sys/office/validate",
            data:{
                "id":rowId
            },
            success:function (result) {
                // if(result.success&&!result.body.inUse){
                    openDialogres('添加下级机构', '${ctx}/sys/office/form?parent.id='+rowId,'95%','95%', '');
                    return;
                // }
                // top.layer.msg("该机构下已经存在用户或者合同信息，不能添加下级机构",{icon:0});
            }
        })

    }
</script>
</body>
</html>
