@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">任务组名</label>
                    <div class="layui-input-block">
                        <input type="text" id="F_JobGroup" name="F_JobGroup" maxlength="50" lay-verify="required" class="layui-input">
                    </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_JobName" name="F_JobName" maxlength="50" value="" lay-verify="required" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">任务类型</label>
                    <div class="layui-input-block" id="F_JobType">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">文件名称</label>
                    <div class="layui-input-block">
                        <select id="F_FileName" name="F_FileName" lay-verify="required">
                            <option value="">==请选择==</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="text" id="F_RequestUrl" name="F_RequestUrl" value="" lay-verify="required" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">请求头</label>
                    <div class="layui-input-block">
                        <textarea id="F_RequestHeaders" name="F_RequestHeaders" autocomplete="off" class="layui-textarea" placeholder="JSON格式"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">请求参数</label>
                    <div class="layui-input-block">
                        <textarea id="F_RequestString" name="F_RequestString" autocomplete="off" class="layui-textarea" placeholder="JSON格式"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">执行数据库</label>
                    <div class="layui-input-block">
                        <select id="F_JobDBProvider" name="F_JobDBProvider">
                            <option value="">==请选择==</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">执行SQL</label>
                    <div class="layui-input-block">
                        <textarea id="F_JobSql" name="F_JobSql" value="" lay-verify="required" autocomplete="off" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">Sql参数</label>
                    <div class="layui-input-block">
                        <textarea id="F_JobSqlParm" name="F_JobSqlParm" class="layui-textarea" autocomplete="off" placeholder="JSON格式"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">Cron表达式</label>
                    <div class="layui-input-block">
                        <input type="text" id="F_CronExpress" name="F_CronExpress" maxlength="50" lay-verify="required" value="" 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_DoItNow" id="F_DoItNow" checked="" value="true" title="立即执行">
                    </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_IsLog" id="F_IsLog" checked="" value="是" 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="layui-form-item layui-hide">
                    <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    layui.use(['jquery', 'form', 'laydate', 'common', 'optimizeSelectOption', 'cron'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common,
            cron = layui.cron,
            laydate = layui.laydate;
        var keyValue = $.request("keyValue");
        //权限字段
        common.authorizeFields('adminform');
        $(function () {
            initControl();
            if (!!keyValue) {
                common.ajax({
                    url: "/SystemSecurity/OpenJobs/GetFormJson",
                    dataType: "json",
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        if(data.F_IsLog=='是')
                        {
                            $('#F_IsLog').attr("checked", 'checked');
                        }
                        changeJobType(data.F_JobType);
                        cron.render({
                            elem: "#F_CronExpress",           // 绑定元素
                            value: data.F_CronExpress, // 默认值
                            done: function (cronStr) {  // 点击确定，或运行时，触发，参数为 cron 表达式字符串
                                $("#F_CronExpress").val(cronStr);
                            },
                        });
                    }
                });
            }
            else {
                changeJobType(0);
                cron.render({
                    elem: "#F_CronExpress",           // 绑定元素
                    value: "0/20 * * * * ?", // 默认值
                    done: function (cronStr) {  // 点击确定，或运行时，触发，参数为 cron 表达式字符串
                        $("#F_CronExpress").val(cronStr);
                    },
                });
            }
            form.render();
        });
        wcLoading.close();
        function initControl() {
            $("#F_FileName").bindSelect({
                url: "/SystemSecurity/OpenJobs/QueryLocalHandlers",
                id: "Key",
                text:"Description"
            });
            $("#F_JobDBProvider").bindSelect({
                url: "/SystemSecurity/OpenJobs/GetDBListJson",
                id:""
            });
            $("#F_JobType").bindRadio({
                data: top.clients.dataItems["JobType"],
                id: ""
            });
        }
        form.on('radio(F_JobType)', function (data) {
            changeJobType(data.value);
        });
        function changeJobType(value) {
            if (value == "0") {
                $('#F_FileName').parent().parent().removeClass('layui-hide');
                $('#F_FileName').attr('lay-verify', 'required');
                $('#F_RequestUrl').parent().parent().addClass('layui-hide');
                $('#F_RequestUrl').removeAttr('lay-verify', 'required');
                $('#F_RequestHeaders').parent().parent().addClass('layui-hide');
                $('#F_RequestString').parent().parent().addClass('layui-hide');
                $('#F_RequestUrl').val("");
                $('#F_RequestHeaders').val("");
                $('#F_RequestString').val("");
                $('#F_JobDBProvider').parent().parent().addClass('layui-hide');
                $('#F_JobDBProvider').removeAttr('lay-verify', 'required');
                $('#F_JobSql').parent().parent().addClass('layui-hide');
                $('#F_JobSql').removeAttr('lay-verify', 'required');
                $('#F_JobSqlParm').parent().parent().addClass('layui-hide');
                $('#F_JobDBProvider').val("");
                $('#F_JobSql').val("");
                $('#F_JobSqlParm').val("");
            }
            else if(value == "5")
            {
                $('#F_RequestUrl').parent().parent().addClass('layui-hide');
                $('#F_RequestUrl').removeAttr('lay-verify', 'required');
                $('#F_RequestHeaders').parent().parent().addClass('layui-hide');
                $('#F_RequestString').parent().parent().addClass('layui-hide');
                $('#F_RequestUrl').val("");
                $('#F_RequestHeaders').val("");
                $('#F_RequestString').val("");
                $('#F_FileName').parent().parent().addClass('layui-hide');
                $('#F_FileName').removeAttr('lay-verify');
                $('#F_JobDBProvider').parent().parent().removeClass('layui-hide');
                $('#F_JobDBProvider').attr('lay-verify', 'required');
                $('#F_JobSql').parent().parent().removeClass('layui-hide');
                $('#F_JobSql').attr('lay-verify', 'required');
                $('#F_JobSqlParm').parent().parent().removeClass('layui-hide');
            }
            else {
                $('#F_FileName').parent().parent().addClass('layui-hide');
                $('#F_FileName').removeAttr('lay-verify');
                $('#F_RequestUrl').parent().parent().removeClass('layui-hide');
                $('#F_RequestUrl').attr('lay-verify', 'required');
                $('#F_RequestHeaders').parent().parent().removeClass('layui-hide');
                $('#F_RequestString').parent().parent().removeClass('layui-hide');
                $('#F_FileName').val("");
                $('#F_JobDBProvider').parent().parent().addClass('layui-hide');
                $('#F_JobDBProvider').removeAttr('lay-verify', 'required');
                $('#F_JobSql').parent().parent().addClass('layui-hide');
                $('#F_JobSql').removeAttr('lay-verify', 'required');
                $('#F_JobSqlParm').parent().parent().addClass('layui-hide');
                $('#F_JobDBProvider').val("");
                $('#F_JobSql').val("");
                $('#F_JobSqlParm').val("");
            }
            form.render();
        }
        //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;
                }
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = data.field;
            if (!postData["F_EnabledMark"]) postData["F_EnabledMark"] = false;
            if (!postData["F_IsLog"]) postData["F_IsLog"] = "否";
            common.submitForm({
                url: "/SystemSecurity/OpenJobs/SubmitForm?keyValue=" + keyValue,
                param: postData,
                success: function () {
                    common.parentreload("data-search-btn");
                }
            })
            return false;
        });
    });
</script>