<style>
    .notify{
        display: none;
    }
</style>
<div class="layui-layout layui-layout-admin" style="padding-left: 40px;margin-top: 20px;">
    <form class="layui-form" action="" method="post" >
        <div class="layui-form-item">
            <label class="layui-form-label mw200">任务名称</label>
            <div class="layui-input-inline mw400">
                <input type="text" name="task_name" id="task_name" lay-verify="required" autocomplete="off" placeholder="任务名称" class="layui-input" value="{{.task.TaskName}}">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label mw200">所属分组</label>
            <div class="layui-input-inline">
                <select name="group_id" lay-verify="required">
                {{range $k, $v := .taskGroup}}
                    <option value="{{$k}}" {{if eq $k $.task.GroupId}}selected{{end}}>{{$v}}</option>
                {{end}}
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">服务器资源</label>
            <div class="layui-inline">

                <div class="layui-input-inline">
                    <select name="server_id">
                        <option value="0">本地服务器</option>
                    {{range $k, $v := .serverGroup}}
                        <optgroup label="{{$v.GroupName}}">
                        {{range $kk, $vv := $v.Servers}}
                            <option value="{{$kk}}" {{if eq $kk $.task.ServerId}}selected{{end}} >{{$vv}}</option>
                        {{end}}
                        </optgroup>
                    {{end}}
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label mw200">任务说明</label>
            <div class="layui-input-inline mw400">
                <textarea name="description" id="description" rows="2" placeholder="请输入内容" class="layui-textarea">{{.task.Description}}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">是否单例</label>
            <div class="layui-input-inline ">

                <input type="radio" name="concurrent" lay-verify="concurrent" value="0" title="是"  {{if eq .task.Concurrent 0}}checked{{end}}>
                <input type="radio" name="concurrent" lay-verify="concurrent" value="1" title="否"  {{if eq .task.Concurrent 1}}checked{{end}}>

            </div>
            <div class="layui-form-mid layui-word-aux"><i class="fa fa-info-circle" aria-hidden="true" id="des"></i></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">时间表达式</label>
            <div class="layui-input-inline mw400">
                <input type="text" name="cron_spec" id="cron_spec" lay-verify="required" autocomplete="off" placeholder="时间表达式" class="layui-input" value="{{.task.CronSpec}}">
            </div>
            <div class="layui-form-mid layui-word-aux"><a href="/help" target="_blank"><i class="fa fa-question-circle" aria-hidden="true"></i></a></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">命令脚本</label>
            <div class="layui-input-inline mw400">
                <textarea name="command" id="command" rows="10" placeholder="请输入内容" class="layui-textarea">{{.task.Command}}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">超时设置</label>
            <div class="layui-input-inline mw400">
                <input type="text" name="timeout" id="timeout" lay-verify="required" autocomplete="off" placeholder="单位：秒，默认一天" class="layui-input" value="{{.task.Timeout}}">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label mw200">出错通知</label>
            <div class="layui-input-inline ">
                <input type="radio" name="is_notify" lay-verify="required" value="1" lay-filter="is_notify" title="是" {{if eq .task.IsNotify 1}}checked{{end}}>
                <input type="radio" name="is_notify" lay-verify="required" value="0" lay-filter="is_notify" title="否" {{if eq .task.IsNotify 0}}checked{{end}}>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item notify">
            <hr>
            <label class="layui-form-label mw200">通知类型</label>
            <div class="layui-input-inline mw400">
                <input type="radio" name="notify_type" lay-verify="required" value="0" title="邮件" {{if eq .task.NotifyType 0}}checked{{end}}>
                <input type="radio" name="notify_type" lay-verify="required" value="1" title="短信" {{if eq .task.NotifyType 1}}checked{{end}}>
                <input type="radio" name="notify_type" lay-verify="required" value="2" title="钉钉" {{if eq .task.NotifyType 2}}checked{{end}}>
                <input type="radio" name="notify_type" lay-verify="required" value="3" title="微信" {{if eq .task.NotifyType 3}}checked{{end}}>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>

        <div class="layui-form-item  notify">
            <label class="layui-form-label mw200">通知用户</label>
            <div class="layui-input-inline mw400 ">
            {{range $k, $v := .adminInfo}}
                <input type="checkbox" name="notify_user" lay-filter="notify_user" title="{{$v.RealName}}" value="{{$v.Id}}" lay-skin="primary" {{range $ks,$vs:=$.notify_user_ids}} {{if eq $v.Id $vs}}checked{{end}}{{end}}>
            {{end}}
                <input type="hidden" name="notify_user_ids" id="notify_user_ids" value="{{.task.NotifyUserIds}}">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <input type="hidden" name="id" id="id" value="0">
        <div class="layui-form-item">
            <label class="layui-form-label mw200"></label>
            <div class="layui-input-inline mw400">
                <button class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['form','element','layer','jquery'],function(){
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        var $ = layui.jquery;
            var error_info = "{{.flash.error}}";
            if(error_info){
                layer.msg(error_info,{icon: 2,shade:0.3},function () {
                    window.history.go(-1)
                })
                return;
            }
        $("#des").on('click',function () {
            layer.tips('设为“是”的话，如果该任务在上一个时间点还没执行完，则略过不执行', '#des', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
        })

        if ($("input[name=is_notify]:checked").val()==1){
            $(".notify").show();
        }
        form.on('radio(is_notify)', function(data){
            if(data.value==1){
                $(".notify").show()
            }else{
                $(".notify").hide()
            }
        });

        var notify_user_ids = [];
        form.on('checkbox(notify_user)', function(data){
            if(data.elem.checked==true){
                notify_user_ids.push(data.value)
            }else{
                $.each(notify_user_ids,function(index,item){
                    // index是索引值（即下标）   item是每次遍历得到的值；
                    if(item==data.value){
                        notify_user_ids.splice(index,1);
                    }
                });
            }
            $("#notify_user_ids").val(notify_user_ids.join(","));
        });

        form.on('submit(sub)', function(data){
            var form_data = data.field;
            $.post('{{urlfor "TaskController.AjaxSave"}}', form_data, function (out) {
                if (out.status == 0) {
                    layer.msg("复制成功!，请到列表中查看",{icon: 1,shade:0.3,time:1000},function () {
                        //self.location=document.referrer;
                        window.location.reload();
                    })
                } else {
                    layer.msg(out.message)
                }
            }, "json");
            return false;
        });

        //但是，如果你的HTML是动态生成的，自动渲染就会失效
        //因此你需要在相应的地方，执行下述方法来手动渲染，跟这类似的还有 element.init();
        form.render();
    });
</script>