@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<div class="layuimini-container ">
    <div class="layuimini-main">
        <div class="layui-form layuimini-form" lay-filter="adminform">
            <div class="layui-tab layui-tab-brief" lay-filter="tab" style="margin: 0px">
                <ul class="layui-tab-title">
                    <li class="layui-this">基本信息</li>
                    <li>执行人</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-form-item">
                            <label class="layui-form-label">节点标识</label>
                            <div class="layui-input-block">
                                <input type="text" name="NodeCode" id="NodeCode" readonly lay-verify="required"
                                       placeholder="流程编号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">节点名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="NodeName" id="NodeName" required lay-verify="required"
                                       placeholder="节点名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">驳回类型</label>
                            <div class="layui-input-block">
                                <select id="NodeRejectType" lay-filter="NodeRejectType" name="NodeRejectType" required lay-verify="required">
                                    <option value="0">上一步</option>
                                    <option value="1">第一步</option>
                                    @*<option value="2">指定步骤</option>
                                    <option value="3">不处理</option>*@
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">会签方式</label>
                            <div class="layui-input-block">
                                <select id="NodeConfluenceType" lay-filter="NodeConfluenceType" name="NodeConfluenceType">
                                    <option value="all">全部分支都同意</option>
                                    <option value="one">至少一个分支同意</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">三方系统回调URL</label>
                            <div class="layui-input-block">
                                <input type="text" name="ThirdPartyUrl" id="ThirdPartyUrl"
                                       placeholder="http://www.xxxxxx.com/api/workflow/execute_callback" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">允许修改表单字段</label>
                            <div class="layui-input-block">
                                <div id="CanWriteFormItemIds" name="CanWriteFormItemIds">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="margin-left: 0px;">
                                <select id="NodeDesignate" lay-filter="NodeDesignate" name="NodeDesignate">
                                    <option value="ALL_USER">所有用户</option>
                                    <option value="SPECIAL_USER">指定用户</option>
                                    <option value="SPECIAL_ROLE">指定角色</option>
                                    <option value="USER_MANAGER">上一级的直属上级</option>
                                    <option value="DEPARTMENT_MANAGER">部门负责人</option>
                                    <option value="MORE_USER_MANAGER">连续多级直属上级</option>
                                    <option value="RUNTIME_SPECIAL_ROLE">运行时指定角色</option>
                                    <option value="RUNTIME_SPECIAL_USER">运行时指定用户</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item" id="department">
                            <div class="layui-input-block" style="margin-left: 0px;">
                                <input id="currentDepart" name="currentDepart" type="checkbox" value="true" title="当前部门">
                            </div>
                        </div>
                        <div class="layui-form-item" id="stoprole">
                            <label>审批终点角色</label>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-hide" id="specialrole">
                                <div class="layui-input-block" style="margin-left: 0px;">
                                    <table class="layui-hide" id="RoleId" lay-filter="RoleTableFilter"></table>
                                </div>
                            </div>
                            <div class="layui-hide" id="departmentmanager">
                                <div class="layui-input-block" style="margin-left: 0px;">
                                    <table class="layui-hide" id="OrganizeId" lay-filter="OrganizeTableFilter"></table>
                                </div>
                            </div>
                            <div class="layui-hide" id="specialuser">
                                <div class="layui-input-block" style="margin-left: 0px;">
                                    <table class="layui-hide" id="UserId" lay-filter="UserTableFilter"></table>
                                </div>
                            </div>
                            <div class="layui-hide" id="alluser">
                                <div class="layui-input-block" style="margin-left: 0px;">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var FlowDesignObject, valueList;
    function initForm(data) {
        FlowDesignObject = data.FlowDesignObject;
        valueList = data.valueList;
    }
    layui.use(['form', 'commonTable', 'element', 'table', 'jquery', 'common', 'optimizeSelectOption', 'xmSelect'], function () {
        var commonTable = layui.commonTable,
            $ = layui.jquery,
            table = layui.table,
            common = layui.common,
            element = layui.element,
            xmSelect = layui.xmSelect,
            form = layui.form;
        var Method = $.request("Method");
        //从flowschemes.js进入的节点信息
        var node = FlowDesignObject;
        var tmp = {
            NodeName: node.name,
            NodeCode: node.id,//默认的code
            NodeRejectType: "1",
            NodeDesignate: "ALL_USER",
            NodeConfluenceType: "all",
            ThirdPartyUrl: "",
            CanWriteFormItemIds:[]
        };
        $('#specialrole').addClass("layui-hide");
        $('#specialuser').addClass("layui-hide");
        $('#alluser').removeClass("layui-hide");
        $('#department').addClass("layui-hide");
        $('#departmentmanager').addClass("layui-hide");
        $('#stoprole').addClass("layui-hide");
        
        var userids = '';
        var roleids = '';
        var orgids = '';
        //初始化节点设置信息
        if (node.setInfo != null) {
            tmp = Object.assign({}, tmp, node.setInfo);
            if (tmp.NodeDesignate === "SPECIAL_USER" && tmp.NodeDesignateData.users.length > 0) {
                form.val("adminform", {
                    "NodeDesignate": 'SPECIAL_USER' //对应radio的value
                });
                userids = tmp.NodeDesignateData.users.join(',');
                $('#stoprole').addClass("layui-hide");
                $('#department').addClass("layui-hide");
                $('#departmentmanager').addClass("layui-hide");
                $('#specialrole').addClass("layui-hide");
                $('#specialuser').removeClass("layui-hide");
                $('#alluser').addClass("layui-hide");
            } else if (tmp.NodeDesignate === "SPECIAL_ROLE" && tmp.NodeDesignateData.roles.length > 0) {
                form.val("adminform", {
                    "NodeDesignate": 'SPECIAL_ROLE' //对应radio的value
                });
                roleids = tmp.NodeDesignateData.roles.join(',');
                $('#stoprole').addClass("layui-hide");
                $('#department').removeClass("layui-hide");
                $('#departmentmanager').addClass("layui-hide");
                $('#specialrole').removeClass("layui-hide");
                $('#specialuser').addClass("layui-hide");
                $('#alluser').addClass("layui-hide");
                form.val("adminform", {
                    "currentDepart": tmp.NodeDesignateData.currentDepart
                });
            } else if (tmp.NodeDesignate === "DEPARTMENT_MANAGER") {
                form.val("adminform", {
                    "NodeDesignate": 'DEPARTMENT_MANAGER' //对应radio的value
                });
                if (tmp.NodeDesignateData.orgs.length > 0) {
                    orgids = tmp.NodeDesignateData.orgs.join(',');
                }
                $('#stoprole').addClass("layui-hide");
                $('#department').removeClass("layui-hide");
                $('#departmentmanager').removeClass("layui-hide");
                $('#specialrole').addClass("layui-hide");
                $('#specialuser').addClass("layui-hide");
                $('#alluser').addClass("layui-hide");
                form.val("adminform", {
                    "currentDepart": tmp.NodeDesignateData.currentDepart
                });
            } else if (tmp.NodeDesignate === "MORE_USER_MANAGER" && tmp.NodeDesignateData.roles.length > 0) {
                form.val("adminform", {
                    "NodeDesignate": 'MORE_USER_MANAGER' //对应radio的value
                });
                roleids = tmp.NodeDesignateData.roles.join(',');
                $('#stoprole').removeClass("layui-hide");
                $('#department').addClass("layui-hide");
                $('#departmentmanager').addClass("layui-hide");
                $('#specialrole').removeClass("layui-hide");
                $('#specialuser').addClass("layui-hide");
                $('#alluser').addClass("layui-hide");
            } else {
                $('#stoprole').addClass("layui-hide");
                $('#department').addClass("layui-hide");
                $('#departmentmanager').addClass("layui-hide");
                $('#specialrole').addClass("layui-hide");
                $('#specialuser').addClass("layui-hide");
                $('#alluser').removeClass("layui-hide");
            }

        }
        var xmData=[];
        for (var i = 0; i < valueList.length; i++)
        {
            xmData.push({
             value:i,
                name: valueList[i]
            });
        }
        var demo1 = xmSelect.render({
            el: '#CanWriteFormItemIds',
            data: xmData,
            model: {
                type: 'relative', //默认 absolute
            },
            filterable: true,
            toolbar: { show: true }
        });
        if (!!tmp.CanWriteFormItemIds) {
            demo1.setValue(tmp.CanWriteFormItemIds);
        }
        commonTable.rendertable({
            elem: '#UserId',
            id: 'UserId',
            url: '/SystemOrganize/User/GetListJson?ids=' + (!!userids ? userids : ""),
            page: false,
            height: 'full-120',
            toolbar: false,//工具栏

            cols: [[
                { type: 'checkbox' },

                { field: 'F_Account', title: '账户', width: 120, sort: true },
                { field: 'F_RealName', title: '姓名', width: 120, sort: true },
                {
                    field: 'F_CompanyName', title: '公司名称', width: 120, sort: true
                },
                {
                    field: 'F_OrganizeName', title: '部门名称', width: 120, sort: true
                },
                {
                    field: 'F_EnabledMark', title: '状态', width: 100, sort: true,
                    templet: function (d) {
                        if (d.F_EnabledMark == true) {
                            return "<span class=\"layui-bg-green\">正常</span>";
                        } else {
                            return "<span class=\"layui-bg-red\">禁用</span>";
                        }
                    }
                }
            ]]
        });
        commonTable.rendertable({
            elem: '#RoleId',
            id: 'RoleId',
            url: '/SystemOrganize/Role/GetSelectJson?ids=' + (!!roleids ? roleids : ""),
            page: false,
            height: 'full-160',
            toolbar: false,//工具栏

            cols: [[
                { type: 'checkbox' },

                { field: 'F_FullName', title: '角色名称', width: 120, sort: true },
                {
                    field: 'F_Type', title: '角色类型', width: 120, sort: true,
                    templet: function (d) {
                        return top.clients.dataItems["RoleType"][d.F_Type] == undefined ? "" : top.clients.dataItems["RoleType"][d.F_Type];
                    }
                },
                {
                    field: 'F_CompanyName', title: '归属公司', width: 120, sort: true
                },
                {
                    field: 'F_EnabledMark', title: '状态', width: 100, sort: true,
                    templet: function (d) {
                        if (d.F_EnabledMark == true) {
                            return "<span class=\"layui-bg-green\">正常</span>";
                        } else {
                            return "<span class=\"layui-bg-red\">禁用</span>";
                        }
                    }
                }
            ]]
        });
        var options = {
            id: 'OrganizeId',
            elem: '#OrganizeId',
            url: '/SystemOrganize/Organize/GetTreeGridJson',
            where: { ids: !!orgids ? orgids : "" },
            treeIdName: 'F_Id',  // id字段名称
            treePidName: 'F_ParentId',     // pid字段名称
            height: 'full-160',
            toolbar: false,//工具栏
            cols: [[
                { type: 'checkbox' },
                { field: 'F_FullName', title: '名称', width: 250 },
                { field: 'F_EnCode', title: '编号', width: 200 },
                {
                    field: 'F_CategoryId', title: '分类', width: 120,
                    templet: function (d) {
                        return top.clients.dataItems["OrganizeCategory"][d.F_CategoryId] == null ? "" : top.clients.dataItems["OrganizeCategory"][d.F_CategoryId];
                    }
                },
                {
                    field: 'F_ManagerName', title: '负责人', width: 120
                },
                {
                    field: 'F_EnabledMark', title: '状态', width: 80,
                    templet: function (d) {
                        if (d.F_EnabledMark == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>有效</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>无效</span>";
                        }
                    }
                }
            ]],
        };
        commonTable.rendertreetable(options);
        common.val('adminform', tmp);
        if (Method == "Details") {
            common.setReadOnly('adminform');
        }
        form.render(); //重新渲染，防止radio/select等失效
        wcLoading.close();
        form.on('select(NodeRejectType)',
            function (data) {
                tmp.NodeRejectType = data.value;
            });

        form.on('select(NodeConfluenceType)',
            function (data) {
                tmp.NodeConfluenceType = data.value;
            });

        form.on('select(NodeDesignate)',
            function (data) {
                tmp.NodeDesignate = data.value;
                if (data.value === "SPECIAL_USER") {
                    $('#stoprole').addClass("layui-hide");
                    $('#department').addClass("layui-hide");
                    $('#departmentmanager').addClass("layui-hide");
                    $('#specialrole').addClass("layui-hide");
                    $('#specialuser').removeClass("layui-hide");
                    $('#alluser').addClass("layui-hide");
                } else if (data.value === "SPECIAL_ROLE") {
                    $('#stoprole').addClass("layui-hide");
                    $('#department').removeClass("layui-hide");
                    $('#departmentmanager').addClass("layui-hide");
                    $('#specialrole').removeClass("layui-hide");
                    $('#specialuser').addClass("layui-hide");
                    $('#alluser').addClass("layui-hide");
                } else if (data.value === "DEPARTMENT_MANAGER") {
                    $('#stoprole').addClass("layui-hide");
                    $('#department').removeClass("layui-hide");
                    $('#departmentmanager').removeClass("layui-hide");
                    $('#specialrole').addClass("layui-hide");
                    $('#specialuser').addClass("layui-hide");
                    $('#alluser').addClass("layui-hide");
                } else if (data.value === "MORE_USER_MANAGER") {
                    $('#stoprole').removeClass("layui-hide");
                    $('#department').addClass("layui-hide");
                    $('#departmentmanager').addClass("layui-hide");
                    $('#specialrole').removeClass("layui-hide");
                    $('#specialuser').addClass("layui-hide");
                    $('#alluser').addClass("layui-hide");
                }
                else {
                    $('#stoprole').addClass("layui-hide");
                    $('#department').addClass("layui-hide");
                    $('#departmentmanager').addClass("layui-hide");
                    $('#specialrole').addClass("layui-hide");
                    $('#specialuser').addClass("layui-hide");
                    $('#alluser').removeClass("layui-hide");
                }

            });

        //提供给上父页面调用
        getVal = function () {
            var users = [];
            var roles = [];
            var orgs = [];
            if (tmp.NodeDesignate === "SPECIAL_USER") {
                var checkStatus = table.checkStatus("UserId").data;
                for (var i = 0; i < checkStatus.length; i++) {
                    users.push(checkStatus[i].F_Id);
                }
            } else if (tmp.NodeDesignate === "SPECIAL_ROLE" || tmp.NodeDesignate === "MORE_USER_MANAGER") {
                var checkStatus = table.checkStatus("RoleId").data;
                for (var i = 0; i < checkStatus.length; i++) {
                    roles.push(checkStatus[i].F_Id);
                }
            } else if (tmp.NodeDesignate === "DEPARTMENT_MANAGER") {
                var checkStatus = table.checkStatus("OrganizeId").data;
                for (var i = 0; i < checkStatus.length; i++) {
                    orgs.push(checkStatus[i].F_Id);
                }
            }
            tmp.NodeRejectType = $('#NodeRejectType').val();
            tmp.NodeConfluenceType = $('#NodeConfluenceType').val();
            tmp.ThirdPartyUrl = $('#ThirdPartyUrl').val();
            tmp.NodeName = $('#NodeName').val();
            var vals = demo1.getValue("value");
            tmp.CanWriteFormItemIds = vals;
            var result = {
                NodeDesignateData: { //节点指定操作人
                    users: users,
                    roles: roles,
                    orgs: orgs,
                    currentDepart: $('#currentDepart').is(':checked') ? true : false,
                }
            };
            //  $.extend(result, vm.tmp);
            result = $.extend(tmp, result);
            return result;
        }
    })
</script>
