<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>修改</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
    <style>
        .inputTxt {
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <form id="set-user" class="layui-form layui-form-pane" action="" lay-filter="set-user">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt">
                            <?php echo $pid ?>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt">
                            <?php echo $username ?>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="nickname" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="text" name="password" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>

    <script src="/component/layui/layui.js"></script>
    <script src="/component/pear/pear.js"></script>
    <script>
        layui.use(['form'], function () {
            let form = layui.form;

            form.val('set-user', {
                "nickname": "<?php echo $nickname ?>",
                "password": "<?php echo $password ?>",
            });

            form.on('submit(user-save)', function (obj) {
                let field = obj.field;
                (async () => {
                    const url = '/api/User/editUser';
                    const info = field;
                    const res = await fetch(url, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(info) });
                    if (res.status !== 200) {
                        layer.msg('请求失败，请重试！', { tips: 2, time: 1200 });
                        return false;
                    }
                    const rec_info = await res.json();
                    if (rec_info.code === 0) {
                        layer.msg(rec_info.msg, { icon: 1, time: 1200 }, () => {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(rec_info.msg, { icon: 2, time: 1200 });
                    }
                })()
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>