<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">登录账号</label>
        <div class="layui-input-inline">
            <input type="text" name="login_name" readonly id="login_name" lay-verify="required" autocomplete="off" placeholder="登录账号" class="layui-input" value="{{.admin.login_name}}">
        </div>
        <div class="layui-form-mid layui-word-aux">*登录不允许修改</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="real_name" id="real_name" lay-verify="required" autocomplete="off" placeholder="真实姓名" class="layui-input" value="{{.admin.real_name}}">
        </div>
        <div class="layui-form-mid layui-word-aux">*</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-inline">
            <input type="text" name="phone" lay-verify="phone|required" autocomplete="off" placeholder="手机号码" class="layui-input" value="{{.admin.phone}}">
        </div>
        <div class="layui-form-mid layui-word-aux">*</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">电子邮箱</label>
        <div class="layui-input-inline">
            <input type="text" name="email" id="email" lay-verify="email" autocomplete="off" placeholder="电子邮箱" class="layui-input" value="{{.admin.email}}">
        </div>
        <div class="layui-form-mid layui-word-aux">*</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">钉钉通知</label>
        <div class="layui-input-inline">
            <input type="text" name="dingtalk" id="dingtalk" autocomplete="off" placeholder="钉钉access_token" class="layui-input" value="{{.admin.dingtalk}}">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">微信帐号</label>
        <div class="layui-input-inline">
            <input type="text" name="wechat" id="wechat" autocomplete="off" placeholder="微信openid" class="layui-input" value="{{.admin.wechat}}">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">重置密码</label>
        <div class="layui-input-inline">
            <input type="radio" name="reset_pwd" value="1" title="重置">
            <input type="radio" name="reset_pwd" value="2" title="不重置" checked>
        </div>
        <div class="layui-form-mid layui-word-aux">默认密码:george518</div>
    </div>
    {{if ne .admin.id 1}}
    <div class="layui-form-item">
        <label class="layui-form-label">选择角色</label>
        <div class="layui-input-block">
            {{range $k, $v := .role}}
                <input type="checkbox" name="role_ids" lay-filter="role_ids" title="{{$v.role_name}}" value="{{$v.id}}" {{if $v.checked}}checked{{end}}>
            {{end}}
        </div>
        <div class="layui-form-mid layui-word-aux">*</div>
    </div>
    {{end}}
    
    <input type="hidden" id="roleids" name="roleids" value="{{.admin.role_ids}}">
    <input type="hidden" id="id" name="id" value="{{.admin.id}}">
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <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>
var $;
    layui.config({
            base : "js/"
    }).use(['form','element','layer','jquery'],function(){
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        var $ = layui.jquery;
        var role_ids_str = "{{.admin.role_ids}}"
        var role_ids = role_ids_str.split(",");

        var error_info = "{{.flash.error}}";
        if(error_info){
            layer.msg(error_info,{icon: 2,shade:0.3},function () {
                window.history.go(-1)
            })
            return;
        }
        form.on('checkbox(role_ids)', function(data){
            if(data.elem.checked==true){
                role_ids.push(data.value)
            }else{
                $.each(role_ids,function(index,item){  
                    // index是索引值（即下标）   item是每次遍历得到的值；
                    if(item==data.value){
                        role_ids.splice(index,1);
                    }
                });
            }
            $("#roleids").val(role_ids.join(","));
        });

        form.on('submit(sub)', function(data){
            var form_data = $("form").serialize();
            $.post('{{urlfor "AdminController.AjaxSave"}}', form_data, function (out) {
                if (out.status == 0) {
                    layer.msg("操作成功",{icon: 1,shade:0.3,time:1000},function () {
                        window.parent.deleteCurrentTab();
                    })
                } else {
                    layer.msg(out.message)
                }
            }, "json");
            return false;
        });
      //但是，如果你的HTML是动态生成的，自动渲染就会失效
      //因此你需要在相应的地方，执行下述方法来手动渲染，跟这类似的还有 element.init();
      form.render();
}); 
</script>