<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <link rel="stylesheet" href="../../lib/layui/css/layui.css">
    <style>
        body { padding: 15px; }
        .search-form { margin-bottom: 15px; padding: 15px; background: #fff; border-radius: 4px; }
        .table-toolbar { margin-bottom: 10px; }
        .layui-table-view { border-radius: 4px; }
        .status-green { color: #5fb878; }
        .status-red { color: #ff5722; }
    </style>
</head>
<body>
    <div class="search-form">
        <form class="layui-form layui-form-pane" lay-filter="searchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="account" placeholder="请输入账号" class="layui-input" autocomplete="off">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入姓名" class="layui-input" autocomplete="off">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phone" placeholder="请输入手机号" class="layui-input" autocomplete="off">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="status" class="layui-select">
                            <option value="">全部</option>
                            <option value="1">正常</option>
                            <option value="0">停用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn" id="btnSearch"><i class="layui-icon layui-icon-search"></i> 搜索</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    
    <div class="table-toolbar">
        <button class="layui-btn layui-btn-sm" id="btnAdd"><i class="layui-icon layui-icon-add-1"></i> 添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" id="btnBatchDelete"><i class="layui-icon layui-icon-delete"></i> 批量删除</button>
    </div>
    
    <table id="tableList" lay-filter="tableList"></table>
    
    <!-- 操作按钮模板 -->
    <script type="text/html" id="toolbar">
        <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i> 编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i> 删除</a>
    </script>
    
    <!-- 状态模板 -->
    <script type="text/html" id="statusTpl">
        {{# if(d.status === '1') { }}
            <span class="status-green"><i class="layui-icon layui-icon-ok-circle"></i> 正常</span>
        {{# } else { }}
            <span class="status-red"><i class="layui-icon layui-icon-close-fill"></i> 停用</span>
        {{# } }}
    </script>

    <!-- 角色名称模板 -->
    <script type="text/html" id="roleNamesTpl">
        {{# var roles = d.roleList || []; }}
        {{# if(roles.length > 0) { }}
            {{ roles.map(function(r) { return r.roleName; }).join('、') }}
        {{# } else { }}
            -
        {{# } }}
    </script>
    
    <script src="../../lib/jquery.min.js"></script>
    <script src="../../lib/layui/layui.js"></script>
    <script src="../../js/config.js"></script>
    <script src="../../js/common.js"></script>
    <script>
        layui.use(['table', 'layer', 'form'], function() {
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            
            // 渲染表格
            var tableIns = TableRender.init({
                elem: '#tableList',
                url: Config.api.user.page,
                page: true,
                limits: [10, 20, 50, 100],
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'account', title: '账号', width: 120 },
                    { field: 'name', title: '姓名', width: 100 },
                    { field: 'phone', title: '手机号', width: 120 },
                    { field: 'email', title: '邮箱', width: 150 },
                    { field: 'status', title: '状态', width: 80, templet: '#statusTpl' },
                    { field: 'roles', title: '角色', width: 200, templet: '#roleNamesTpl' },
                    { field: 'createTime', title: '创建时间', width: 150 },
                    { field: 'remark', title: '备注', width: 150 },
                    { fixed: 'right', title: '操作',  align: 'center', toolbar: '#toolbar' }
                ]]
            });
            
            // 搜索
            $('#btnSearch').click(function() {
                var data = form.val('searchForm');
                table.reload('tableList', {
                    where: data,
                    page: { curr: 1 }
                });
            });
            
            // 添加
            $('#btnAdd').click(function() {
                Common.open({
                    title: '添加用户',
                    area: ['700px', '550px'],
                    content: 'user_form.html?id='
                });
            });
            
            // 批量删除
            $('#btnBatchDelete').click(function() {
                var checkStatus = table.checkStatus('tableList');
                var data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = data.map(function(item) { return item.id; });
                Common.confirm('确定要删除选中的 ' + data.length + ' 条数据吗？', function() {
                    Common.del(Config.api.user.remove + '/' + ids.join(','), function(res) {
                        Common.success('删除成功', function() {
                            table.reload('tableList');
                        });
                    });
                });
            });
            
            // 监听表格操作
            table.on('tool(tableList)', function(obj) {
                var data = obj.data;
                var event = obj.event;
                
                if (event === 'edit') {
                    Common.open({
                        title: '编辑用户',
                        area: ['700px', '550px'],
                        content: 'user_form.html?id=' + data.id
                    });
                } else if (event === 'del') {
                    Common.confirm('确定要删除用户 【' + data.name + '】 吗？', function() {
                        Common.del(Config.api.user.remove + '/' + data.id, function(res) {
                            Common.success('删除成功', function() {
                                obj.del();
                            });
                        });
                    });
                }
            });
        });
    </script>
</body>
</html>
