<?php
/*
 * @author: CloudZA(云之安) <2922619853@qq.com>
 * @hitokoto: 一场秋雨一场凉，秋心酌满泪为霜。
 * Copyright (c) 2022 by CloudZA, All Rights Reserved.
 */

include_once 'common.php';
if ( !isset($islogin)) header("Location: /");//非法访问
$title = '用户列表';
include_once 'partials/head.php';


?>
<div id="pjax-container">
    <!--导航-->
    <div class="bg-body-light">
        <div class="content content-full">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2">
                <div class="flex-grow-1">
                    <h1 class="h3 fw-bold mb-2">
                        <?php echo $title ?>
                    </h1>
                </div>
                <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-alt">
                        <li class="breadcrumb-item">
                            <a class="link-fx" href="javascript:void(0)">首页</a>
                        </li>
                        <li class="breadcrumb-item" aria-current="page">
                            <?php echo $title ?>
                        </li>
                    </ol>
                </nav>
            </div>
        </div>
</div>
    <!--导航结束-->
    <!--内容-->
<div class="content">
    <link rel="stylesheet" id="css-main" href="<?php echo __CSS__ ?>bootstrap-table.css">
    <div class="block block-rounded">
        <div class="block-header block-header-default">
            <h3 class="block-title"> 用户列表 </h3>
        </div>
        <div class="block-content">
            <form action="" method="POST" onsubmit="return false;" id="toolbar">
                <div class="btn-group">
                    <button type="button" class="btn btn-light btn-alt-secondary btn-xs" style="padding: 0.4375rem 0.925rem" onclick="showUserOp('add')">添加用户</button>
<!--                    <button type="button" class="btn btn-alt-secondary btn-xs dropdown-toggle dropdown-toggle-split" id="dropdown-split-alt-danger" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">-->
<!--                        <span class="visually-hidden">下拉</span>-->
<!--                    </button>-->
<!--                    <div class="dropdown-menu fs-sm" aria-labelledby="dropdown-split-alt-danger" style="">-->
<!--                        <a class="dropdown-item" href="javascript:userList()">筛选会员用户</a>-->
<!--                        <a class="dropdown-item" href="javascript:void(0)">筛选非会员用户</a>-->
<!--                    </div>-->
                </div>
                <a class="btn btn-light btn-alt-secondary btn-xs" style="padding: 0.4375rem 0.925rem" href="javascript:delChecks()">
                    <span class="d-sm-none">删除选中</span>
                    <span class="d-none d-sm-inline-block fw-semibold">删除选中</span>
                </a>

        </form>
            <table class="text-center fs-sm mb-2" id="table"></table>
        </div>
    </div>

    <div class="block block-rounded block-content" id="add_limit" style="display: none; margin-bottom: 0!important;">
        <div class="text-center fw-semibold fs-sm" id="lime_title">增加额度</div>
        <h2 class="content-heading border-bottom fs-xs mb-2 pb-1 text-dark" style="padding: 0" id="add_limit_uid_t">用户ID：</h2>
        <input type="hidden" id="operation">
        <input type="hidden" id="add_limit_uid">
        <input type="hidden" id="add_limits">
        <div class="mt-2 mb-2"><label class="form-label fs-sm text-muted" for="add_chat_limit">额度</label>
            <input type="text" class="form-control form-control-alt" id="add_chat_limit" name="add_chat_limit" required placeholder="例如100">
        </div>
        <a class="btn btn-sm btn-alt-primary me-1 mt-1 mb-3" href="javascript:editLimit()">保存</a>
</div>

    <div class="block block-rounded block-content" id="addOrEditUser" style="display: none; margin-bottom: 0!important;">
        <div class="text-center fw-semibold fs-sm" id="addOrEditTitle">添加用户</div>
        <input type="hidden" id="type" value="add">
        <input type="hidden" id="UID">
        <div class="mt-2 mb-2">
            <label class="form-label fs-sm text-muted" for="username">用户名</label>
            <input type="text" class="form-control form-control-alt" id="username" name="username" required placeholder="用户账号">
        </div>
        <div class="mt-2 mb-2">
            <label class="form-label fs-sm text-muted" for="user_email">邮箱</label>
            <input type="email" class="form-control form-control-alt" id="user_email" name="user_email" required placeholder="用户邮箱">
        </div>
        <div class="mt-2 mb-2" id="user_plans_div">
            <label class="form-label fs-sm text-muted" for="user_plans">套餐设置</label>
            <select class="form-select" id="user_plans" name="user_plans">
                <option value="0">不设置</option>
                <?php foreach (getPlans() as $value): ?>
                    <option value="<?php echo $value[ 'id' ] ?>"><?php echo $value[ 'name' ] ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="mt-2 mb-2"><label class="form-label fs-sm text-muted" for="user_pass">密码</label>
            <input type="text" class="form-control form-control-alt" id="user_pass" name="user_pass" required placeholder="设置用户密码">
        </div>
        <a class="btn btn-sm btn-alt-primary me-1 mt-1 mb-3" href="javascript:addOrEditUserOp()">保存</a>
</div>

    <script>
        function showUserOp (type, id = '',username = '',email = '')
        {
            if (type === 'add') {
                app.getval('#addOrEditTitle', '添加用户')
                app.getval('#type', 'add')
                $('#user_pass').attr('placeholder', '设置用户密码');
                $('#user_plans_div').show()
            }
            else {
                app.getval('#addOrEditTitle', '编辑用户')
                app.getval('#type', 'edit')
                app.getval('#UID', id)
                app.getval('#username',username)
                app.getval('#user_email',email)
                $('#user_plans_div').hide()
                $('#user_pass').attr('placeholder', '修改用户密码，不改请留空');
            }
            layer.open({
                type   : 1,
                shade  : false, // 遮障可点击，透明度为0.5
                title  : false,
                content: $('#addOrEditUser'),
                cancel : () => {

                },
            })
        }

        function addOrEditUserOp ()
        {
            app.ajax('ajax_table.php?act=add_user', {
                type      : app.getval('#type'),
                UID       : app.getval('#UID'),
                username  : app.getval('#username'),
                user_email: app.getval('#user_email'),
                user_plans: app.getval('#user_plans'),
                user_pass : app.getval('#user_pass'),
            }, res => {
                if (res.code === 200) {
                    app.btn(res.msg, '我知道了', null, () => {
                        app.pjax('user.php')
                    })
                }
                else {
                    app.btn(res.msg, '我知道了')
                }
            })
        }

        function delChecks (id = '')
        {
            let selected = [];
            if (id === '') {
                // 获取选中的行
                selected = $('#table').bootstrapTable('getSelections');
                if (selected.length === 0) {
                    app.notify('未选择记录', 'warning');
                    return;
                }
            }
            else {
                // 添加指定的id
                selected.push({id: id});
            }

            const checkbox = [];
            $.each(selected, function (key, item) {
                checkbox.push(item.id);
            });

            console.log(checkbox);
            app.btn('确定要删除选中的内容吗？删除之后不可恢复！', '确定', '取消', () => {
                app.ajax('ajax_table.php?act=delUser', {
                    id: checkbox
                }, res => {
                    if (res.code === 200) {
                        app.notify(res.msg, 'success')
                        setTimeout(() => {
                            app.pjax('user.php');
                        }, 1000)
                    }
                    else {
                        app.notify(res.msg, 'warning')
                    }
                })
            }, null)
        }

        function showChatLimit (type = true, id)
        {
            if (type) {
                app.getval('#lime_title', '增加额度')
                app.getval('#operation', 1)
            }
            else {
                app.getval('#lime_title', '减少额度')
                app.getval('#operation', 2)
            }
            app.getval('#add_limit_uid_t', '用户ID：' + id)
            app.getval('#add_limit_uid', id)
            layer.open({
                type   : 1,
                shade  : false, // 遮障可点击，透明度为0.5
                title  : false,
                content: $('#add_limit'),
                cancel : () => {
                    // app.notify('取消添加', 'info')
                },
            })
        }

        function editLimit ()
        {
            app.ajax('ajax_table.php?act=editUserChatLimit', {
                id   : app.getval('#add_limit_uid'),
                type : app.getval('#operation'),
                limit: app.getval('#add_chat_limit')
            }, res => {
                if (res.code === 200) {
                    app.btn(res.msg, '我知道了', null, () => {
                        app.pjax('user.php')
                    })
                }
                else {
                    app.btn(res.msg, '我知道了',)
                }
            })
        }

        // 设置用户资料(弹窗)
        function setUserPop (UID, nick, user, qq, email)
        {
            $("#UID").html('用户ID：' + UID),
                $("#UUID").val(UID),
                $("#user_nick").val(nick),
                $("#user_id").val(user),
                $("#user_qq").val(qq),
                $("#user_email").val(email)

            layer.open({
                type   : 1,
                shade  : false, // 遮障可点击，透明度为0.5
                title  : false,
                content: $('#user_info'),
                cancel : function () {
                    app.notify('取消修改', 'info')
                },
            });
        }

        // 设置用户资料
        function setUser (UID)
        {
            app.ajax('ajax_user.php?act=setUserInfo', {
                id        : UID,
                user_nick : app.getval("#user_nick"),
                user_id   : app.getval("#user_id"),
                user_pass : app.getval("#user_pass"),
                user_qq   : app.getval("#user_qq"),
                user_email: app.getval("#user_email")
            }, res => {
                if (res.code === 200) {
                    app.notify(res.msg, 'success')
                    setTimeout(() => {
                        app.pjax('user.php');
                    }, 300);
                }
                else {
                    app.btn(res.msg, '我知道了', null)
                }
            })
        }

        // 设置用户状态
        function setUserStatus (UID, status)
        {
            app.ajax('ajax_user.php?act=setUserStatus', {uid: UID, status: status}, res => {
                if (res.code === 200) {
                    app.notify(res.msg, 'success')
                    setTimeout(() => {
                        app.pjax('user.php');
                    }, 300);
                }
                else {
                    app.notify(res.msg, 'warning')
                }
            })
        }

        // 添加用户
        function addUser ()
        {
            app.ajax('ajax_user.php?act=addUser', {
                appid     : app.getval('#add_appid'),
                user_nick : app.getval("#add_nick"),
                user_id   : app.getval("#add_id"),
                user_pass : app.getval("#add_pass"),
                user_qq   : app.getval("#add_qq"),
                user_email: app.getval("#add_email")
            }, res => {
                if (res.code === 200) {
                    app.notify(res.msg, 'success')
                    setTimeout(() => {
                        app.pjax('user.php');
                    }, 300);
                }
                else {
                    app.btn(res.msg, '我知道了', null)
                }
            })
        }

        // 删除用户
        function delUser (UID)
        {
            app.btn('你确定要删除这个用户吗？', '删除', '取消', res => {
                app.ajax('ajax_user.php?act=delUser', {uid: UID}, res => {
                    if (res.code === 200) {
                        app.notify(res.msg, 'success')
                        setTimeout(() => {
                            app.pjax('user.php');
                        }, 300);
                    }
                    else {
                        app.btn(res.msg, '我知道了', null)
                    }
                })
            }, null)
        }

        app.ajax('ajax_table.php?act=user_list', null, res => {
            if (res.code === 200) {
                let columns = [
                        {
                            field   : 'UID',
                            checkbox: true,
                        },
                        {
                            field: 'id',
                            title: 'UID',
                        },
                        {
                            field: 'username',
                            title: '用户名'
                        },
                        {
                            field: 'email',
                            title: '邮箱'
                        },
                        {
                            field: 'plans',
                            title: '会员状态',
                            formatter (value, row, index)
                            {
                                let status = '';
                                let colour = ''
                                switch (value) {
                                    case '0':
                                        status = '未开通'
                                        colour = 'btn-alt-secondary'
                                        break;
                                    case '1':
                                        status = '正常'
                                        colour = 'btn-alt-success'
                                        break;
                                    case '2':
                                        status = '已过期'
                                        colour = 'btn-alt-danger'
                                        break;
                                    default:
                                        status = '未开通'
                                        colour = 'btn-alt-secondary'
                                }
                                return `<span class="btn btn-xs ${colour}">${status}</span>`
                            }

                        },
                        {
                            field: 'plans_name',
                            title: '会员类型',
                            formatter (value, row, index)
                            {
                                if (value === '0') {
                                    value = '未开通会员'
                                }
                                return value
                            }
                        },
                        {
                            field: 'chat_limit',
                            title: '对话额度',
                            formatter (value, row, index)
                            {
                                return value + '<a type="button" href="javascript:showChatLimit(true,\'' + row.id + '\')" class="mx-1 text-primary">[ + ] </a><a type="button" href="javascript:showChatLimit(false,\'' + row.id + '\')" class="mx-1 text-primary">[ - ] </a>'
                            }
                        },
                        {
                            field: 'effective_at',
                            title: '生效时间',
                            formatter (value, row, index)
                            {
                                if (!value) {
                                    value = '0000-00-00 00:00:00'
                                }
                                return value
                            }
                        },
                        {
                            field: 'expires_at',
                            title: '过期时间',
                            formatter (value, row, index)
                            {
                                if (!value) {
                                    value = '0000-00-00 00:00:00'
                                }
                                return value
                            }
                        },
                        {
                            field: 'id',
                            title: '操作',
                            formatter (value, row, index)
                            {
                                let html = '<button type="button" class="btn btn-sm btn-alt-primary dropdown-toggle" id="dropdown-default-alt-secondary" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">设置</button><div class="dropdown-menu fs-sm" aria-labelledby="dropdown-default-alt-secondary">';
                                html += '<a class="dropdown-item d-flex align-items-center justify-content-between space-x-1" href="javascript:showUserOp(\'edit\',\'' + row.id + '\',\'' + row.username + '\',\'' + row.email + '\')"><span>修改资料</span> </a><div class="dropdown-divider"></div><a class="dropdown-item d-flex align-items-center justify-content-between space-x-1" href="javascript:delChecks(\'' + row.id + '\')"><span>删除</span></a>';
                                html += '</div>';
                                return html;
                            }
                        }
                    ]
                ;
                table.initTable('table', columns, res.data.custom, null, res.data.page);
            }
            else {
                app.notify('接口请求失败', 'warning')
            }
        })
    </script>
</div>
</div>
    <!--内容结束-->
<?php include_once 'partials/foot.php'; ?>
