{{{ template "common/header" . }}}

<div class="ui grid">
   {{{ template "user/menu" . }}}

    <div class="twelve wide column">
        <div class="pageHeader">
            <div class="segment">
                <h3 class="ui dividing header">
                        <div class="content">
                            添加用户
                        </div>
                </h3>
            </div>
        </div>
        <form class="ui form fluid vertical segment">
            <input type="hidden" name="id" value="{{{.User.Id}}}">
            <div class="three fields">
                <div class="field">
                    <label>用户名</label>
                    <div class="ui small input">
                        <input type="text"  name="name" value="{{{.User.Name}}}" placeholder="请输入用户名">
                    </div>
                </div>
                <div class="field">
                    <label>邮箱</label>
                    <div class="ui small  input">
                        <input type="text"  name="email" value="{{{.User.Email}}}" placeholder="请输入邮箱">
                    </div>
                </div>
            </div>
            {{{if eq .User.Id 0}}}
            <div class="three fields">
                <div class="field">
                    <label>密码</label>
                    <input type="password"   name="password" placeholder="请输入密码">
                </div>

                <div class="field">
                    <label>确认密码</label>
                    <input type="password"   name="confirm_password" placeholder="请再次输入密码">
                </div>
            </div>
            {{{end}}}

            <div class="three fields">
                <div class="field">
                    <label>角色</label>
                    <input type="radio" name="is_admin" value="0" {{{if eq .User.IsAdmin 0}}}checked{{{end}}}> 普通用户
                    <input type="radio" name="is_admin" value="1" {{{if eq .User.IsAdmin 1}}}checked{{{end}}}> 管理员
                </div>

                <div class="field">
                    <label>状态</label>
                    <input type="radio" name="status" value="1" {{{if eq .User.Status 1}}}checked{{{end}}}> 启用
                    <input type="radio" name="status" value="0"
                           {{{if eq .User.Status 0}}}checked{{{end}}}
                    > 禁用
                </div>
            </div>

            <div class="ui primary submit button">保存</div>
            <a class="ui button" onclick="location.href='/user';">取消</a>
        </form>
    </div>
</div>


<script type="text/javascript">
    var $uiForm = $('.ui.form');
    $($uiForm).form(
            {
                onSuccess: function(event, fields) {
                    util.post('/user/store', fields, function(code, message) {
                        location.href = "/user"
                    });

                    return false;
                },
                fields: {
                    name: {
                        identifier  : 'name',
                        rules: [
                            {
                                type   : 'empty',
                                prompt : '请输入用户名'
                            },
                            {
                                type   : 'minLength[3]',
                                prompt : '长度不能少于3'
                            },
                            {
                                type   : 'maxLength[32]',
                                prompt : '长度不能超过32'
                            }
                        ]
                    },
                    password: {
                        identifier  : 'password',
                        rules: [
                            {
                                type   : 'empty',
                                prompt : '请输入密码'
                            },
                            {
                                type   : 'minLength[6]',
                                prompt : '长度不能少于6位'
                            }
                        ]
                    },
                    confirmPassword: {
                        identifier  : 'confirm_password',
                        rules: [
                            {
                                type   : 'empty',
                                prompt : '请再次输入密码'
                            },
                            {
                                type   : 'match[password]',
                                prompt : '两次输入密码不匹配'
                            }
                        ]
                    },
                    email: {
                        identifier  : 'email',
                        rules: [
                            {
                                type   : 'empty',
                                prompt : '请输入邮箱'
                            },
                            {
                                type   : 'email',
                                prompt : '邮箱格式错误'
                            }
                        ]
                    }
                },
                inline : true
            });
</script>

{{{ template "common/footer" . }}}