@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/css/stepcss.css" rel="stylesheet" />
<script>
    layui.use(['form', 'step', 'common', 'dtree', 'optimizeSelectOption'], function () {
        var $ = layui.$,
            form = layui.form,
            dtree = layui.dtree,
            common = layui.common,
            step = layui.step;
        var keyValue = $.request("keyValue");
        //权限字段
        common.authorizeFields('stepForm');
        step.render({
            elem: '#stepForm',
            filter: 'stepForm',
            width: '85%', //设置容器宽度
            stepWidth: '300px',
            height: '450px',
            stepItems: [{
                title: '填写角色信息'
            }, {
                title: '设置功能权限'
                }, {
                    title: '设置字段权限'
                }]
        });
        $(function () {
            initControl();
            if (!!keyValue) {
                common.ajax({
                    url: "/SystemOrganize/Role/GetFormJson",
                    dataType: "json",
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('stepForm', data);
                    }
                });
            }
            form.render();
        });
        wcLoading.close();
        function initControl() {
            $("#F_CompanyId").bindSelect({
                url: "/SystemOrganize/SystemSet/GetListJson",
                id: "F_Id",
                text: "F_CompanyName"
            });
            $("#F_Type").bindSelect({
                data: top.clients.dataItems['RoleType'],
                id: "",
            });
        }
        //select验证
        form.verify({
            required: function (value, item) {
                var msg = "必填项不能为空";
                value = $.trim(value);
                var isEmpty = !value || value.length < 1;
                // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                if (item.tagName == 'SELECT' && isEmpty) {
                    $("html").animate({
                        scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                    }, 50);
                }
                if (isEmpty) {
                    return msg;
                }
            }
        });
        // 初始化树
        var DemoTree1 = dtree.render({
            elem: "#demoTree1",
            width: '200px',
            method: "GET",
            async: false,
            checkbar: true,
            line: true,  // 显示树线
            initLevel: 0,
            icon: "-1" , // 隐藏二级图标
            checkbarType: "p-casc",
            scroll:"#toolbarDiv1", // 绑定div元素
            url: "/SystemOrganize/RoleAuthorize/GetPermissionTree?v="+new Date().Format("yyyy-MM-dd hh:mm:ss"), // 使用url加载（可与data加载同时存在）
            request: { roleId: keyValue }
        });
        var role;
        form.on('submit(formStep)', function (data) {
            role = data.field
            if (!role["F_EnabledMark"]) role["F_EnabledMark"] = false;
            step.next('#stepForm');
            return false;
        });
        var moduleids = [];
        form.on('submit(formStep2)', function (data) {
            var params = dtree.getCheckbarNodesParam("demoTree1");
            for (var i = 0; i < params.length; i++) {
                moduleids.push(params[i].nodeId);
            }
            // 初始化树
            var DemoTree2 = dtree.render({
                elem: "#demoTree2",
                width: '200px',
                method: "POST",
                async: false,
                checkbar: true,
                line: true,  // 显示树线
                initLevel: 0,
                icon: "-1", // 隐藏二级图标
                checkbarType: "p-casc",
                scroll: "#toolbarDiv2", // 绑定div元素
                url: "/SystemOrganize/RoleAuthorize/GetPermissionFieldsTree?v=" + new Date().Format("yyyy-MM-dd hh:mm:ss"), // 使用url加载（可与data加载同时存在）
                request: { roleId: keyValue, moduleids: String(moduleids) }
            }); 
            step.next('#stepForm');
            return false;
        });
        form.on('submit(formStep3)', function (data) {
                var postData = role;
                var params = dtree.getCheckbarNodesParam("demoTree2");
                var note = [];
                for (var i = 0; i < params.length; i++) {
                    note.push(params[i].nodeId);
                }
                postData["permissionbuttonIds"] = String(moduleids);
                postData["permissionfieldsIds"] = String(note);
                common.submitForm({
                    url: "/SystemOrganize/Role/SubmitForm?keyValue=" + keyValue,
                    param: postData,
                    success: function () {
                        common.parentreload("data-search-btn");
                    }
                })
            return false;
        });

        $('.pre').click(function () {
            step.pre('#stepForm');
        });
    })
</script>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-carousel" id="stepForm" lay-filter="stepForm">
            <div carousel-item>
                <div>
                    <form class="layui-form layuimini-form">
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">归属公司</label>
                            <div class="layui-input-block">
                                <select id="F_CompanyId" name="F_CompanyId" lay-verify="required" lay-search>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">角色名称</label>
                            <div class="layui-input-block">
                                <input type="text" id="F_FullName" name="F_FullName" maxlength="50" lay-verify="required" class="layui-input" placeholder="请填写角色名称">
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">角色编号</label>
                            <div class="layui-input-block">
                                <input id="F_EnCode" name="F_EnCode" type="text" maxlength="50" lay-verify="required" class="layui-input" placeholder="请输入角色编号" />
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">角色类型</label>
                            <div class="layui-input-block">
                                <select id="F_Type" name="F_Type" lay-verify="required">
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">显示顺序</label>
                            <div class="layui-input-block">
                                <input type="number" pattern="[0-9]*" id="F_SortCode" name="F_SortCode" lay-verify="required|number" oninput="if(value.length>8)value=value.slice(0,8)" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label">选项</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="F_EnabledMark" id="F_EnabledMark" checked="" value="true" title="有效标识">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text layui-hide">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea id="F_Description" name="F_Description" class="layui-textarea" placeholder="请输入备注"></textarea>
                            </div>
                        </div>
                        <div class="form-group-bottom text-right">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formStep">
                                    &emsp;下一步&emsp;
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div>
                    <form class="layui-form layuimini-form">
                        <div class="layui-form-item ">
                            <div style="height: 350px;overflow: auto;" id="toolbarDiv1">
                                <ul id="demoTree1" class="dtree" data-id="0"></ul>
                            </div>
                        </div>
                        <div class="form-group-bottom text-right">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn layui-btn-primary pre">上一步</button>
                                <button class="layui-btn" lay-submit lay-filter="formStep2">
                                    &emsp;下一步&emsp;
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div>
                    <form class="layui-form layuimini-form">
                        <div class="layui-form-item ">
                            <div style="height: 350px;overflow: auto;" id="toolbarDiv2">
                                <ul id="demoTree2" class="dtree" data-id="0"></ul>
                            </div>
                        </div>
                        <div class="form-group-bottom text-right">
                            <div class="layui-input-block">
                                <button type="button" class="layui-btn layui-btn-primary pre">上一步</button>
                                <button class="layui-btn site-demo-active" lay-submit lay-filter="formStep3">
                                    确认保存
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
</div>
