<?php
/**
 * 货源管理
 */

$title = '用户管理列表';
include "header.php";
$sum = 8; #每页数量
$page = shop::page($sum, $_QET['page']); #取出翻页数据
$user_list = user::user_list($sum, $page['count'], $page['limit'], $_QET['id']); #获取商品列表
$paging = shop::paging($user_list['count'], $_QET['page']); #取出翻页按钮数据
if (!empty($_QET['unset'])) {
    $a = $DB->query("DELETE FROM `sky_user` WHERE (`id`='{$_QET['unset']}')"); #删除
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['unset'] . '删除成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['unset'] . '删除失败,原因->' . $DB->error(), '2');
}
if (!empty($_QET['money'])) { #积分加款
    if (empty($_QET['id'])) show_msg('温馨提示', '请填写完整！', '2');
    $id = (int)$_QET['id'];
    $rue = $DB->get_row("SELECT * FROM `sky_user` WHERE `id` = $id LIMIT 1");
    if (!$rue) show_msg('温馨提示', '用户' . $rue . '不存在,请核对后再进行操作！');
    $money = (float)$_QET['money'];
    if ((float)$rue['currency'] < $money) {
        userlog('积分充值', '站长在后台为用户' . $id . '手动增加了' . ($money - (float)$rue['currency']) . '点积分！', $id, ($money - (float)$rue['currency']));
    } else {
        userlog('积分扣除', '站长在后台为用户' . $id . '手动扣除了' . ((float)$rue['currency'] - $money) . '点积分！', $id, ((float)$rue['currency'] - $money));
    }
    $a = $DB->query("UPDATE `sky_user` SET `currency`='$money' WHERE (`id`='$id')"); #修改
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['id'] . '编辑成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['id'] . '编辑失败,原因->' . $DB->error(), '2');
}
if (!empty($_QET['moneys'])) { #余额加款
    if (empty($_QET['id'])) show_msg('温馨提示', '请填写完整！', '2');
    $id = (int)$_QET['id'];
    $rue = $DB->get_row("SELECT * FROM `sky_user` WHERE `id` = $id LIMIT 1");
    if (!$rue) show_msg('温馨提示', '用户' . $rue . '不存在,请核对后再进行操作！');
    $money = (float)$_QET['moneys'];
    if ((float)$rue['money'] < $money) {
        userlog('后台加款', '站长在后台为用户' . $id . '手动加款了' . ($money - (float)$rue['money']) . '元余额！', $id, ($money - (float)$rue['money']));
    } else {
        userlog('后台扣款', '站长在后台为用户' . $id . '手动扣除了' . ((float)$rue['money'] - $money) . '元余额！', $id, ((float)$rue['money'] - $money));
    }
    $a = $DB->query("UPDATE `sky_user` SET `money`='$money' WHERE (`id`='$id')"); #修改
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['id'] . '编辑成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['id'] . '编辑失败,原因->' . $DB->error(), '2');
}
if (!empty($_QET['state'])) {
    $a = $DB->get_row("SELECT * FROM `sky_user` WHERE `id` = '{$_QET['state']}' LIMIT 1 ");
    if ($a['state'] == 1) {
        $b = 2;
    } else $b = 1;
    $b = $DB->query("UPDATE `sky_user` SET `state`='$b' WHERE (`id`='{$_QET['state']}')"); #删除
    if ($b) {
        show_msg('成功提示', '编号:' . $_QET['state'] . '修改成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['state'] . '修改失败,原因->' . $DB->error(), '2');
}
if (!empty($_QET['uidup'])) {
    if (empty((int)$_QET['uidup']) || empty((int)$_QET['up'])) show_msg('温馨提示', '请填写完整！', '2');
    $a = $DB->query("UPDATE `sky_user` SET `grade`='{$_QET['up']}'  WHERE (`id`='{$_QET['uidup']}')"); #删除
    if ($a) {
        userlog('等级变化', '站长在后台将您的用户等级设置为：' . $_QET['up'], $_QET['uidup']);
        show_msg('成功提示', '编号:' . $_QET['uidup'] . '等级设置成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['uidup'] . '等级设置失败,原因->' . $DB->error(), '2');
}
?>
<!-- third party css -->
<link href="<?= $cdnserver == null ? '../' : $cdnserver ?>assets/css/vendor/dataTables.bootstrap4.css" rel="stylesheet"
      type="text/css"/>
<link href="<?= $cdnserver == null ? '../' : $cdnserver ?>assets/css/vendor/responsive.bootstrap4.css" rel="stylesheet"
      type="text/css"/>
<!-- third party css end -->
<!-- App css -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4">
                        <a href="javascript:query()" class="btn btn-danger mb-2"><i
                                    class="layui-icon layui-icon-add-circle-fine mr-2"></i>
                            查找用户
                        </a>
                    </div>
                </div>
                <code class="text-16">用户总余额之和为：<?= $DB->count("SELECT sum(money) FROM `sky_user`"); ?>元</code>
                <div class="table-responsive">
                    <table class="table table-centered table-striped dt-responsive nowrap w-100"
                           id="products-datatable">
                        <thead>
                        <tr style="white-space: nowrap">
                            <th>ID</th>
                            <th>KPID</th>
                            <th>名称</th>
                            <th>头像</th>
                            <th>用户订单</th>
                            <th>绑定手机号</th>
                            <th>用户域名</th>
                            <th>用户等级</th>
                            <th>用户日志</th>
                            <th>上级编号</th>
                            <th>余额</th>
                            <th>剩余额度</th>
                            <th>登陆IP</th>
                            <th>用户状态</th>
                            <th>最近登陆</th>
                            <th>注册时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($user_list['data'] as $v) {
                            ?>
                            <tr style="white-space: nowrap;font-size: 0.9em">
                                <td>
                                    <a href="admin.order.list.php?uid=<?= $v['id'] ?>" title="查看该用户的订单"
                                       class="badge badge-info-lighten">
                                        <?= $v['id'] ?>
                                    </a>
                                </td>
                                <td class="table-user">
                                    <a href="javascript:layer.alert('<?= $v['user_idu'] ?>',{icon:1,title:'用户唯一识别码'})">查看</a>
                                </td>
                                <td>
                                    <?= $v['name'] ?>
                                </td>
                                <td>
                                    <img src="<?= $v['image'] ?>" class="rounded-circle bg-warning"
                                         style="width: 1.5em;height: 1.5em"/>
                                </td>
                                <td>
                                    <a href="admin.order.list.php?uid=<?= $v['id'] ?>" title="查看该用户的订单"
                                       class="badge badge-primary">
                                        Ta的订单
                                    </a>
                                </td>
                                <td>
                                    <?= ($v['mobile'] == '' ? '未绑定' : '<a href="tel:' . $v['mobile'] . '">' . $v['mobile'] . '</a>') ?>
                                </td>
                                <td class="text-primary">
                                    <?= ($v['domain']==''?'未配置':$v['domain']) ?>
                                </td>
                                <td onclick="update(<?= $v['id'] ?>,<?= $v['grade'] ?>)">
                                    V<?= $v['grade'] ?>密价
                                </td>
                                <td>
                                    <a href="admin.user.log.php?uid=<?= $v['id'] ?>" class="badge badge-danger-lighten">
                                        用户日志
                                    </a>
                                </td>
                                <td>
                                    <span onclick="location.href='admin.user.list.php?id=<?= $v['superior'] ?>'"
                                          class="badge badge-dark-lighten">
                                        <?= $v['superior'] ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-primary-lighten"
                                          onclick="moneys(<?= $v['money'] ?>,<?= $v['id'] ?>)">
                                        <?= $v['money'] ?>元
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-warning"
                                          onclick="money(<?= $v['currency'] ?>,<?= $v['id'] ?>)">
                                        <?= $v['currency'] . $conf['currency'] ?>
                                    </span>
                                </td>
                                <td>
                                    <?= $v['ip'] ?>
                                </td>
                                <td>
                                    <?= $v['state'] ?>
                                </td>
                                <td>
                                    <?= $v['recent_time'] ?>
                                </td>
                                <td>
                                    <?= $v['found_date'] ?>
                                </td>
                                <td>
                                    <a href="javascript:unset(<?= $v['id'] ?>)" class="action-icon"> <i
                                                class="layui-icon layui-icon-delete"></i></a>
                                </td>
                            </tr>
                        <?php } ?>

                        </tbody>
                    </table>
                </div>
                <nav>
                    <ul class="pagination pagination-rounded" style="overflow: auto">
                        <li class="page-item">
                            <a class="page-link" href="?page=1<?= !empty($_QET['id']) ? '&id=' . $_QET['id'] : '' ?>"
                               aria-label="Previous">
                                <span aria-hidden="true">«</span>
                                <span class="sr-only">Previous</span>
                            </a>
                        </li>
                        <?php foreach ($paging as $v) { ?>
                            <li class="page-item <?= $v['active'] ?>"><a class="page-link"
                                                                         href="?page=<?= $v['number'] ?><?= !empty($_QET['id']) ? '&id=' . $_QET['id'] : '' ?>"><?= $v['number'] ?></a>
                            </li>
                        <?php } ?>
                        <li class="page-item">
                            <a class="page-link"
                               href="?page=<?= $shop_list['count'] ?><?= !empty($_QET['id']) ? '&id=' . $_QET['id'] : '' ?>"
                               aria-label="Next">
                                <span aria-hidden="true">»</span>
                                <span class="sr-only">Next</span>
                            </a>
                        </li>
                        <?php if (!empty($_QET['id'])) { ?>
                            <li class="page-item">
                                <a class="page-link" href="?page=1" aria-label="Nexst">
                                    <span aria-hidden="true">查看全部</span>
                                    <span class="sr-only">Nexst</span>
                                </a>
                            </li>
                        <?php } ?>
                    </ul>
                </nav>
            </div> <!-- end card-body-->
        </div> <!-- end card-->
    </div> <!-- end col -->
</div>
<?php include "bottom.php"; ?>
<script>
    function unset(a) {
        layer.alert('删除后不可撤销，是否确认删除？', {
            icon: 3, title: '温馨提示', btn: ['取消', '确定删除'], btn2: function (layero, index) {
                window.location.href = '?unset=' + a;
            }
        })
    }

    function moneys(money, id) {
        layer.prompt({
            formType: 0,
            value: money,
            title: '请输入变动金额,点击下方按钮继续',
            btn: ['确定']
        }, function (value, index, elem) {
            window.location.href = '?moneys=' + value + '&id=' + id;
            layer.close(index);
        });
    }

    function money(money, id) {
        layer.prompt({
            formType: 0,
            value: money,
            title: '请输入额度,点击确定会修改成功',
        }, function (value, index, elem) {
            window.location.href = '?money=' + value + '&id=' + id;
            layer.close(index);
        });
    }

    function query() {
        layer.prompt({
            formType: 0,
            value: '',
            title: '请填写用户名或ID查找用户!',
        }, function (value, index, elem) {
            location.href = '?id=' + value;
        });
    }

    function update(id, up) {
        layer.prompt({
            formType: 0,
            value: up,
            title: '请输入需要修改的用户等级！',
        }, function (value, index, elem) {
            window.location.href = '?uidup=' + id + '&up=' + value;
            layer.close(index);
        });
    }
</script>
