<%@ 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>
        .form-horizontal{
            width: 60%;
        }
        .form-group{
            margin-left: 10%;
            padding-top:18px;
        }
        .lw9 .layui-item .layui-form-label{
            width: 80px;
        }
    </style>
    <script type="text/javascript">
        var validateForm;
        function doSubmit(){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
            if(validateForm.form()){
                loading('正在提交，请稍等...');
                $("#inputForm").submit();
                return true;
            }

            return false;
        }
        $(document).ready(function(){
            $("#name").focus();

            validateForm= $("#inputForm").validate({
                rules: {
                    enname: {remote: "${ctx}/sys/role/checkEnname?oldEnname=" + encodeURIComponent("${role.enname}")}
                },
                messages: {
                    enname: {remote: "英文名已存在"}
                },
                submitHandler: function(form){
                    var ids = [], nodes = tree.getCheckedNodes(true);
                    for(var i=0; i<nodes.length; i++) {
                        ids.push(nodes[i].id);
                    }
                    console.log("----ids:"+ids);
                    $("#menuIds").val(ids);
                    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);
                    }
                }
            });

            //在ready函数中预先调用一次远程校验函数，是一个无奈的回避案。(刘高峰）
            //否则打开修改对话框，不做任何更改直接submit,这时再触发远程校验，耗时较长，
            //submit函数在等待远程校验结果然后再提交，而layer对话框不会阻塞会直接关闭同时会销毁表单，因此submit没有提交就被销毁了导致提交表单失败。
            /*$("#inputForm").validate().element($("#name"));
            $("#inputForm").validate().element($("#enname"));

            var setting = {check:{enable:true,nocheckInherit:true},view:{selectedMulti:false},
                data:{simpleData:{enable:true}},callback:{beforeClick:function(id, node){
                        tree.checkNode(node, !node.checked, true, true);
                        return false;
                    }}};


            // 用户-机构
            var zNodes2=[
                    <c:forEach items="${officeList}" var="office">{id:"${office.id}", pId:"${not empty office.parent?office.parent.id:0}", name:"${office.name}"},
                </c:forEach>];
            // 初始化树结构
            var tree2 = $.fn.zTree.init($("#officeTree"), setting, zNodes2);
            // 不选择父节点
            tree2.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
            // 默认选择节点
            var ids2 = "${role.office}";
            var node = tree2.getNodeByParam("id", ids2);
            try{tree2.checkNode(node, true, false);}catch(e){}
            // 默认展开全部节点
            tree2.expandAll(true);
            // 刷新（显示/隐藏）机构
            refreshOfficeTree();
            $("#dataScope").change(function(){
                refreshOfficeTree();
            });*/

            var setting = {check:{enable:true,nocheckInherit:true},view:{selectedMulti:false},
                data:{simpleData:{enable:true}},callback:{beforeClick:function(id, node){
                        tree.checkNode(node, !node.checked, true, true);
                        return false;
                    }}};

            // 用户-菜单
            var zNodes=[
                    <c:forEach items="${menuList}" var="menu">{id:"${menu.id}", pId:"${not empty menu.parent.id?menu.parent.id:0}", name:"${not empty menu.parent.id?menu.name:'权限列表'}"},
                </c:forEach>];
            // 初始化树结构
            var tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
            // 不选择父节点
            tree.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
            // 默认选择节点
            var ids = "${role.menuIds}".split(",");
            for(var i=0; i<ids.length; i++) {
                var node = tree.getNodeByParam("id", ids[i]);
                try{tree.checkNode(node, true, false);}catch(e){}
            }
            // 默认展开全部节点
            tree.expandAll(false);
        });

        function changeRole(obj) {
            var count = 0;
            var total = obj.length;
            var trlen = $("#roleActivityList tr").length;
            for(var i=0;i<obj.length;i++){
                trlen = trlen+i;
                var str = "";
                var id = obj[i].id;
                var tr = $("#"+id);
                if(tr.length>0){
                    $("#"+id).show();
                    count++;
                }else {
                    var name = obj[i].name;
                    var type = obj[i].type;
                    var typeName = obj[i].typeName;
                    var remarks = obj[i].remarks;
                    str += '<tr id="'+id+'">' +
                        '<td>' +
                        '<input id="roleActivityList'+trlen+'_id" name="roleActivityList['+trlen+'].id" type="hidden" value="'+id+'"/>'+
                         '<input id="roleActivityList'+trlen+'_delFlag" name="roleActivityList['+trlen+'].delFlag" type="hidden" value="0"/>'+
                        name +
                        '</td>' +
                        '<td>' +
                        typeName +
                        '</td>' +
                        '<td>' +
                        remarks +
                        '</td>' +
                        '<td>' +
                        '<a href="javascript:void(0)" onclick="delRowData(this,\'#roleActivityList['+trlen+']\',\''+id+'\')"   class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</a>' +
                        '</td>' +
                        '</tr>';
                    $("#roleActivityList").append(str);
                    count++;
                }
            }
        }

        function delRowData(obj,prefix, id){
            var roleId = $("#id").val();
            $("#"+id).hide();
            var delFlag = $(prefix+"_delFlag");
            delFlag.val("1");
            /*$.ajax({
                type:"post",
                url:'${ctx}/sys/role/deleteRoleActivity',
                data:{"roleActivityIds":id,"roleId":roleId},
                dataType:"json",
                success:function(data){
                    if(data.success) {
                        parent.layer.msg("删除岗位关联的角色成功!",{icon:1});
                        $(obj).parent().parent().remove();
                    }else {
                        parent.layer.msg("删除岗位关联的角色失败!",{icon:2});
                    }
                }
            })*/
            return;
        }
        function refreshOfficeTree(){
            if($("#dataScope").val()==9){
                $("#officeTree").show();
            }else{
                $("#officeTree").hide();
            }
        }

        $(function(){
            $('#menuTree_1_switch').removeClass('root_close').addClass('root_open');
            $('#menuTree_1_ul').css('display','block');
            $('#menuTree_1_switch').click(function () {
                $('#menuTree_1_switch').toggleClass('root_open').toggleClass('root_close');
                if($('#menuTree_1_switch').is('.root_open')){
                    $('#menuTree_1_ul').css('display','block');
                }else{
                    $('#menuTree_1_ul').css('display','none');
                }
            })
        })
    </script>
</head>
<body>
<div class="single-form">
    <div class="container" style="display:flex;width:90%;margin-left:10%;">
        <form:form id="inputForm" modelAttribute="role" autocomplete="off" action="${ctx}/sys/role/save" method="post" class="form-horizontal" >
            <form:hidden path="id"/>
            <sys:message content="${message}"/>

            <div class="form-group layui-row first lw9">
                <div class="form-group-label"><h2>岗位信息</h2></div>
                <div class="layui-item layui-col-sm6" style="display: none">
                    <label class="layui-form-label"><span class="require-item">*</span>归属公司：</label>
                    <div class="layui-input-block with-icon">
                        <c:choose>
                            <c:when test="${not empty role.company.id}">
                                <input readonly="true" name="company.name" htmlEscape="false" class="layui-input form-control" value="${role.company.name}" />
                                <input readonly="true" type="hidden" name="company.id" htmlEscape="false" class="layui-input form-control" value="${role.company.id}" />
                            </c:when>
                            <c:otherwise>
                                <sys:treeselect id="company" name="company.id" value="${role.company.id}" labelName="company.name" labelValue="${role.company.name}"
                                                title="公司" url="/sys/office/treeData?type=1" cssClass="form-control required layui-input"/>
                            </c:otherwise>
                        </c:choose>
                    </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 path="number" htmlEscape="false" maxlength="2" class="form-control required 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 with-icon">
                        <sys:treeselect id="office" name="office.id" value="${role.office.id}" labelName="office.name" labelValue="${role.office.topCompany}"
                                        title="机构" url="/sys/office/treeData" 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="oldName" name="oldName" type="hidden" value="${role.name}">
                        <form:input path="name" htmlEscape="false" maxlength="50" class="form-control required 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 with-icon">
                        <form:select path="dataScope" class="form-control required simple-select">
                            <form:option value="" label=""/>
                            <form:options items="${fns:getDictList('sys_data_scope')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                        </form:select>
                    </div>
                </div>
                <input id="oldEnname" name="oldEnname" type="hidden" value="${role.enname}">
               <%-- <div class="layui-item layui-col-sm12">
                    <label class="layui-form-label">岗位类型：</label>
                    <div class="layui-input-block with-icon">
                        <form:select path="roleType" class="form-control required simple-select">
                            <form:option value="assignment">任务分配</form:option>
                            <form:option value="security-role">管理岗位</form:option>
                            <form:option value="user">普通岗位</form:option>
                        </form:select>
                        <span class="help-inline" title="activiti有3种预定义的组类型：security-role、assignment、user 如果使用Activiti Explorer，需要security-role才能看到manage页签，需要assignment才能claim任务">
						工作流组用户组类型（任务分配：assignment、管理岗位：security-role、普通岗位：user）</span>
                    </div>
                </div>
                <div class="layui-item layui-col-sm12">
                    <label class="layui-form-label">是否系统数据：</label>
                    <div class="layui-input-block with-icon">
                        <form:select path="sysData" class="form-control simple-select">
                            <form:options items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                        </form:select>
                        <span class="help-inline">“是”代表此数据只有超级管理员能进行修改，“否”则表示拥有岗位修改人员的权限都能进行修改</span>
                    </div>
                </div>
                <div class="layui-item layui-col-sm12">
                    <label class="layui-form-label">是否可用：</label>
                    <div class="layui-input-block with-icon">
                        <form:select path="useable" class="form-control simple-select">
                            <form:options items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                        </form:select>
                        <span class="help-inline">“是”代表此数据可用，“否”则表示此数据不可用</span>
                    </div>
                </div>--%>
                <div class="layui-item layui-col-sm12 with-textarea">
                    <label class="layui-form-label">备注：</label>
                    <div class="layui-input-block">
                        <form:textarea style="resize:none;" path="remarks" htmlEscape="false" rows="4" maxlength="200" class="form-control "/>
                    </div>
                </div>
            </div>
            <div class="form-group layui-row ">
                <div class="form-group-label"><h2>关联角色</h2></div>
                <div class="layui-item layui-col-sm12">
                    <div class="layui-input-block with-icon" style="margin-left:0">
                        <sys:treeselectroles id="roleActivityIds" name="roleActivityIds" value="${role.roleActivityIds}" labelName="roleActivityNames" labelValue="${role.roleActivityNames}"
                                             title="选择角色" url="sys/office/treeDataRoleActivitys" checked="true" cssClass="form-control required" allowClear="true" notAllowSelectParent="true"/>
                        <table id="contentTable" class="table details table-bordered table-condensed">
                            <thead>
                            <tr>
                                <th  class="sort-column name">角色名称</th>
                                <th  class="sort-column type">角色类型</th>
                                <th  class="sort-column remarks">备注信息</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="roleActivityList">
                            <c:forEach items="${role.roleActivityList}" var="roleActivity" varStatus="index">
                                <tr id="${roleActivity.id}">
                                    <td >
                                        <input id="roleActivityList${index.index}_id" name="roleActivityList[${index.index}].id" type="hidden" value="${roleActivity.id}"/>
                                        <input id="roleActivityList${index.index}_delFlag" name="roleActivityList[${index.index}].delFlag" type="hidden" value="0"/>
                                            ${roleActivity.name}
                                    </td>
                                    <td>
                                            ${fns:getDictLabel(roleActivity.type, 'sys_role_type', '')}
                                    </td>
                                    <td>
                                            ${roleActivity.remarks}
                                    </td>
                                    <td>
                                        <a href="javascript:void(0)" onclick="delRowData(this, '#roleActivityList${index.index}','${roleActivity.id}')"   class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i>&nbsp;删除</a>
                                    </td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table>
                    <%-- <sys:treeselectRole id="roleActivityIds" name="roleActivityIds" value="${role.roleActivityIds}" labelName="roleActivityNames" labelValue="${role.roleActivityNames}"
                                            title="选择角色" url="/sys/office/treeDataRoleActivity?type=3&isAll=false&role=${role.id}" cssClass="form-control layui-input" notAllowSelectParent="true" checked="true"/>
                        <label id="roleIdList-error" class="error" for="roleActivityList"></label>--%>
                    </div>
                </div>
            </div>

            <form:hidden path="menuIds"/>
        </form:form>
        <div class="form-group layui-row ">
            <div class="form-group-label"><h2>关联菜单</h2></div>
            <div id="menuTree" class="ztree" style="margin-top:3px;float:left;"></div>
        </div>
    </div>
</div>
</body>
</html>