@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/css/stepcss.css" rel="stylesheet" />
<link href="~/js/lay-module/formDesigner/formPreview.css" rel="stylesheet" />
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-carousel" id="adminform" lay-filter="adminform">
            <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">
                                <input type="text" id="F_Name" name="F_Name" required lay-verify="required"
                                       placeholder="表单名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label required">表单类型</label>
                            <div class="layui-input-block">
                                <select id="F_FrmType" name="F_FrmType" required lay-verify="required" lay-search lay-filter="FrmType">
                                    <option value="" selected>请选择</option>
                                    <option value="0">动态表单</option>
                                    <option value="1">自定义表单</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label">关联数据表</label>
                            <div class="layui-input-block">
                                <input type="text" id="F_DbName" name="F_DbName"
                                       placeholder="关联数据表" 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">
                                <select id="F_WebId" name="F_WebId" disabled lay-search lay-filter="WebId">
                                    <option value="" selected>请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide">
                            <label class="layui-form-label">归属部门</label>
                            <div class="layui-input-block">
                                <select id="F_OrganizeId" name="F_OrganizeId" lay-search>
                                    <option value="" selected>请选择</option>
                                </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 id="commonform" >
                            <iframe src="~/page/designer.html" id="formdesigner" onload="setIframeHeight(this)"style="width:100%;min-height:400px">
                            </iframe>
                        </div>
                        <fieldset class="layui-elem-field">
                            <div id="frmPreview"></div>
                        </fieldset>
                        <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" id="saveBtn">
                                    &emsp;保存&emsp;
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    //只读参数
    var designJson = {};
    var readonly = true;
    layui.use(['jquery', 'form', 'common', 'element', 'step'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common,
            step = layui.step,
            element = layui.element;
        var keyValue = $.request("keyValue");
        var ItemId = $.request("ItemId");
        step.render({
            elem: '#adminform',
            filter: 'adminform',
            width: '100%', //设置容器宽度
            stepWidth: '750px',
            height: '550px',
            stepItems: [{
                title: '基本信息'
            }, {
                title: '表单设计'
            }]
        });

        //权限字段
        common.authorizeFields('adminform');
        $(function () {
            initControl();
            if (!!ItemId) {
                $('#F_OrganizeId').val(ItemId);
            }
            if (!!keyValue) {
                common.ajax({
                    url: '/SystemManage/Form/GetFormJson',
                    dataType: 'json',
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        if (data.F_FrmType != 0) {
                            $("#frmPreview").load(document.location.origin + '/form/' + data.F_WebId + '.html');
                            $('#commonform').addClass('layui-hide');
                            $('#frmPreview').removeClass('layui-hide');
                            $('#frmPreview').parent().removeClass('layui-hide');
                        }
                        else {
                            $('#commonform').removeClass('layui-hide');
                            $('#frmPreview').addClass('layui-hide');
                            $('#frmPreview').parent().addClass('layui-hide');
                            designJson=JSON.parse(data.F_Content);
                            $("#formdesigner").attr('src',document.location.origin + '/page/designer.html');
                        }
                    }
                });
            }
            else {
                common.ajax({
                    url: document.location.origin + '/json/demo.json',
                    success: function (result) {
                       designJson = result;
                    }
                });
                $('#commonform').removeClass('layui-hide');
                $('#frmPreview').addClass('layui-hide');
                $('#frmPreview').parent().addClass('layui-hide');
                $("#formdesigner").attr('src',document.location.origin + '/page/designer.html');
            }
            form.render();
        });
        wcLoading.close();
        function initControl() {
            $("#F_OrganizeId").bindSelect({
                url: "/SystemOrganize/Organize/GetTreeSelectJson",
            });
            $("#F_WebId").bindSelect({
                url: "/SystemManage/Form/GetExtendForm",
                id: "FileName",
                text: "FileName"
            });
        }
        $('.pre').click(function () {
            step.pre('#adminform');
        });

        $('.next').click(function () {
            step.next('#adminform');
        });
        form.on('select(FrmType)',
            function (data) {
                if (!!data.value && data.value == 1) {
                    $('#F_WebId').removeAttr("disabled");
                    $('#F_DbName').addClass("layui-hide");
                    $('#F_DbName').val("");
                } else {
                    $('#F_WebId').attr("disabled", "disabled");
                    $('#F_WebId').val("");
                    $('#F_DbName').removeClass("layui-hide");
                }
                form.render();
            });
        form.on('select(WebId)',
            function (data) {
                if (!!data.value) {
                    $('#frmPreview').removeClass('layui-hide');
                    $('#commonform').addClass('layui-hide');
                    $("#frmPreview").load(document.location.origin + '/form/' + data.value + '.html');
                    form.render();
                }
                else {
                    $("#frmPreview").html("");
                }
            });
        var postData;
        form.on('submit(formStep)', function (data) {
            postData = $.extend(postData, data.field);
            if (postData.F_FrmType != 0) {
                $('#commonform').addClass('layui-hide');
                $('#frmPreview').removeClass('layui-hide');
                $('#frmPreview').parent().removeClass('layui-hide');
            }
            else {
                $('#commonform').removeClass('layui-hide');
                $('#frmPreview').addClass('layui-hide');
                $('#frmPreview').parent().addClass('layui-hide');
            }
            step.next('#adminform');
            return false;
        });
        //监听提交
        $(document).on('click', '#saveBtn', function () {
            //解析表单数据
            var formeditor = '';
            if (postData.F_FrmType != 1) {
                //获取表单设计器里的内容
                document.querySelector("iframe").contentWindow.document
                               .getElementsByClassName('saveJson')[0].click();
                formeditor =window.localStorage.getItem('layui_form_json');
                if (!formeditor) {
                    common.modalMsg("请编辑设计器", "warning");
                    return false;
                }
                postData.F_Content = formeditor;
            }
            if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
            common.submitForm({
                url: '/SystemManage/Form/SubmitForm?keyValue=' + keyValue,
                param: postData,
                success: function () {
                    common.reloadIframe("/SystemManage/Form/Index", 'data-search-btn');
                }
            })
            return false;
        });
    });
</script>

