<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
    <script src="/static/js/fun.js"></script>
    <title>用户列表</title>

    <style>
        .box {
            width: 80%;
            margin: auto;
            margin-top: 60px;
            padding-left: 10px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="row col-md-8">
            <button type="button" class="btn btn-primary" onclick="add()">新增</button>
            <a href="/index" type="button" class="btn btn-default">返回</a>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>
                            id
                        </th>
                        <th>
                            用户名
                        </th>
                        <th>
                            创建时间
                        </th>
                        <th>
                            删除
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($list as $value) {
                    ?>
                        <tr>
                            <td><?php echo $value['id']; ?></td>
                            <td><?php echo $value['username']; ?></td>
                            <td><?php echo date('Y-m-d H:i:s', $value['create_time']); ?></td>
                            <td>
                                <a type="button" class="btn btn-primary" href="javascript:add(<?php echo $value['id']; ?>)">
                                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
                                <a type="button" class="btn btn-danger" href="javascript:del(<?php echo $value['id']; ?>)">
                                    <span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
                            </td>
                        </tr>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 新增 Modal  -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
        <div class="modal-dialog" role="document" style="margin-top: 200px;">
            <div class="modal-content modal-add">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="addModalLabel">新增</h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" name="userid" id="userid" value="0">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" class="form-control" id="username" placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" placeholder="请输入密码">
                    </div>
                    <div class="form-group">
                        <label for="password">确认密码</label>
                        <input type="password" class="form-control" id="repassword" placeholder="请再次输入密码">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" onclick="save()">SAVE</button>
                </div>
            </div>
        </div>
    </div>
    <!-- model end -->

    <script>
        function del(id) {
            $.post('/userdelete', {
                'userid': id,
            }, function(res) {
                console.log(res);
                if (res.code == 0) {
                    alert('操作成功');

                    window.location.reload();
                } else {
                    alert(res.msg);
                }
                return false;
            }, 'json');
        }

        function add(userid) {
            userid = userid || 0;

            if (userid != 0) {
                $.get('userinfo', {
                    'userid': userid,
                }, function(res) {
                    $("#userid").val(res.info.id);
                    $("#username").val(res.info.username);
                    $("#username").attr('disabled', true);
                }, 'json')
            }

            $("#addModal").modal('show');
        }

        function save() {
            let userid = document.querySelector('#userid').value;
            let username = document.querySelector('#username').value;
            let password = document.querySelector('#password').value;
            let repassword = document.querySelector('#repassword').value;

            if (isNull(password)) {
                alert("密码不能为空.");
                return false;
            }

            if (!pwdFormat(password)) {
                alert("密码需6位以上.");
                return false;
            }

            if (password != repassword) {
                alert("两次密码不一致.");
                return false;
            }

            $.post(
                '/usersave', {
                    "userid": userid,
                    "username": username,
                    "password": password,
                    "repassword": repassword
                },
                function(res) {
                    // console.log(res);
                    if (res.code == 0) {
                        alert(res.msg);
                        window.location.reload();
                    } else {
                        alert(res.msg);
                        return false;
                    }
                }, 'json');
        }
    </script>
</body>

</html>