<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{resource}}/mini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{resource}}/mini/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form">
                <div class="layui-form-item">
                    <label class="layui-form-label required">管理账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="required" lay-reqtext="管理账号不能为空"
                            placeholder="用于登录的账号" value="{{username}}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">账号昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="nickname" lay-verify="required" lay-reqtext="手机不能为空"
                            placeholder="用于对外展示的昵称" value="{{nickname}}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">绑定邮箱</label>
                    <div class="layui-input-block">
                        <input type="email" name="email" placeholder="绑定您的个人邮箱" value="{{email}}" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
                        <button class="layui-btn layui-btn-warm" id="cpwdBtn">更改密码</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{resource}}/mini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="{{resource}}/mini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
        layui.use(['form', 'miniTab', 'jquery'], function () {
            var form = layui.form,
                layer = layui.layer,
                miniTab = layui.miniTab,
                $ = layui.jquery;

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                // var index = layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // }, function () {
                //     layer.close(index);
                //     miniTab.deleteCurrentByIframe();
                // });

                $.post('{{domain}}/admin/api/user/self?do=update&which=userdata',{
                    username: data.field.username,
                    nickname: data.field.nickname,
                    email: data.field.email
                },function(res){
                    if(res.data == 'OK'){
                        // 如果信息更新成功，则刷新页面
                        layer.msg('账号信息更新成功！');
                        setTimeout(() => {
                            location.reload();
                        },1000);
                    }else if(res.msg == '参数不完整' || res.msg == '数据保存失败'){
                        layer.msg('系统接口错误，请联系开发者');
                    }else if(res.msg == '用户名已存在'){
                        layer.msg('用户名已经存在，试试其他名字吧');
                    }else if(res.msg == '邮箱格式错误'){
                        layer.msg('您的邮箱格式错误，请修改后重试');
                    }else{
                        layer.msg('系统未知错误，错误信息：' + res.msg());
                    }
                });

                return false;
            });

            $('#cpwdBtn').click(function () {
                layer.prompt({
                    title: '请输入您的新账号密码',
                    formType: 1
                }, function (pass, index) {
                    layer.close(index);
                    $.post('{{domain}}/admin/api/user/self?do=update&which=password',{
                        newpasswd: pass
                    },function(data){
                        if(data.data == 'OK'){
                            layer.msg('密码更改成功...');
                        }else{
                            layer.msg(data.msg);
                        }
                    });
                });
            });

        });
    </script>
</body>

</html>