<?php
/**
 * 用户等级管理
 */
$title = '用户等级列表';
include "header.php";

if (isset($_QET['default'])) {
    $re = $DB->query("DELETE FROM `sky_price`");
    if ($re) {
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('1','1','普通用户', '普通用户，和游客没什么区别!', '30', '3000', '0.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('2','2','铜牌代理', '铜牌代理，可以加盟分店了,其他用户在你加盟分店下单你可以获得提成!', '28', '2800', '10.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('3','3','银牌代理', '银牌代理，除了可以加盟分店外，还可以赚取下级提成，并且收益比上一级别更高！，推荐购买!', '26', '2600', '20.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('4','4','金牌代理', '金牌代理，除了可以加盟分店外，还可以赚取下级提成，并且收益比上一级别更高！，推荐购买!', '24', '2400', '30.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('5','5','小站长', '小站长，除了可以加盟分店外，还可以赚取下级提成，并且收益比上一级别更高！，推荐购买!', '22', '2200', '40.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('6','6','平台站长', '平台站长，除了可以加盟分店外，还可以赚取下级提成，并且收益比上一级别更高！，推荐购买!', '20', '2000', '50.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('7','7','高级站长', '高级站长，除了可以加盟分店外，还可以赚取下级提成，并且收益比上一级别更高！，推荐购买!', '18', '1800', '60.00','$date');");
        @$DB->query("INSERT INTO `sky_price` (`mid`,`sort`, `name`, `content`, `priceis`, `pointsis`, `money`, `addtime`)  VALUES ('8','8','领袖站长', '领袖站长，除了可以加盟分店外，还可以赚取下级提成，并且收益比上一级别更高！，推荐购买!', '16', '1600', '70.00','$date');");
        show_msg('温馨提示', '内置等级规则写入成功！', 1);
    } else show_msg('温馨提示', '原等级规则清空失败！' . $DB->error(), 4);
}

$sum = 12; #每页数量
$page = shop::page($sum, $_QET['page']); #取出翻页数据
$user_level = user::users_level($sum, $page['count'], $page['limit']);
$paging = shop::paging($user_level['count'], $_QET['page']); #取出翻页按钮数据

if (!empty($_QET['unset'])) {
    $a = $DB->query("DELETE FROM `sky_price` WHERE (`mid`='{$_QET['unset']}')"); #删除
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['unset'] . '删除成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['unset'] . '删除失败,原因->' . $DB->error(), '2');
}

if (!empty($_QET['state'])) {
    $a = $DB->query("UPDATE `sky_price` SET `state`='{$_QET['type']}' WHERE (`mid`='{$_QET['state']}')"); #删除
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['state'] . '等级状态切换成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['state'] . '状态切换失败,原因->' . $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="admin.level.add.php" class="btn btn-danger mb-2"><i
                                    class="layui-icon layui-icon-add-circle-fine mr-2"></i>
                            新增一个等级
                        </a>
                        <a href="?default" class="btn btn-success mb-2"><i
                                    class="layui-icon layui-icon-date mr-2"></i>
                            使用内置等级套餐(会清空原来的)
                        </a>
                    </div>
                </div>
                <code>密价等级根据排序来定,排序越高,等级越高！<!--，超过1级可加盟分店！--></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>操作</th>
                            <th>等级排序</th>
                            <th>等级状态</th>
                            <th>等级名称</th>
                            <th>出售价格</th>
                            <th>
                                订单利润<a href="javascript:layer.alert('此处很简单理解，就是每单商品订单，你可以赚的利润百分比！<br>如用户所购买商品的成本是100元，付款用户所在等级的订单利润是10%，则你可以在这个用户身上赚10元，以此类推！<br>若有个别商品需要设置更高或者更低的利润时，可以单独配置获得利润的百分比！',{title:'订单利润说明'})">[说明]</a>
                            </th>
                            <th>
                                兑换价格<a href="javascript:layer.alert('这一列参数也很好理解，就是用户花费<?= $conf['currency'] ?>兑换商品的成本倍数<br>如成本是100元，倍数是1500倍<br>则<?= $conf['currency'] ?>兑换价格为150000<?= $conf['currency'] ?>，以此类推！',{title:'兑换价格说明'})">[说明]</a>
                            </th>
                            <th>等级介绍</th>
                            <th>添加时间</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($user_level['data'] as $v) {
                            ?>
                            <tr style="white-space: nowrap;font-size: 0.9em">
                                <td>
                                    <a href="admin.level.add.php?mid=<?= $v['mid'] ?>" class="action-icon"> <i
                                                class="layui-icon layui-icon-set-fill"></i></a>
                                    <a href="javascript:level.unset(<?= $v['mid'] ?>)" class="action-icon"> <i
                                                class="layui-icon layui-icon-delete"></i></a>
                                </td>
                                <td class="table-user" id="<?= $v['mid'] ?>_level">
                                    <span class="badge badge-primary-lighten">V<?= $v['sort'] ?>密价</span>
                                    <a href="javascript:level.sort(<?= $v['mid'] ?>,1)" title="最顶部">↑</a> <a
                                            href="javascript:level.sort(<?= $v['mid'] ?>,2)" title="上移一格">▲</a><a
                                            href="javascript:level.sort(<?= $v['mid'] ?>,3)" title="下移一格">▼</a> <a
                                            href="javascript:level.sort(<?= $v['mid'] ?>,4)" title="最低部">↓</a>
                                </td>
                                <td>
                                    <a href="?state=<?= $v['mid'] ?>&type=<?= ($v['state'] == 1 ? 2 : 1) ?>"
                                       class="<?= ($v['state'] == 1 ? 'text-success' : 'text-danger') ?>"><?= ($v['state'] == 1 ? '启用' : '停用') ?></a>
                                </td>
                                <td>
                                    <span class="badge badge-success-lighten"><?= $v['name'] ?></span>
                                </td>
                                <td>
                                    <span class="badge badge-warning-lighten"><?= $v['money'] ?>元</span>
                                </td>
                                <td style="font-size: 1.3em">
                                    <span class="badge badge-secondary-lighten"
                                          title="如果商品成本为：100元，售价为：<?= 100 + (100 * ($v['priceis'] / 100)) ?>"><?= $v['priceis'] ?>%</span>
                                </td>
                                <td style="font-size: 1.3em">
                                    <span class="badge badge-secondary-lighten"
                                          title="如果商品成本为：100元，<?= $conf['currency'] ?>兑换价格为：<?= 100 * $v['pointsis'] ?><?= $conf['currency'] ?>"><?= $v['pointsis'] ?>倍</span>
                                </td>
                                <td>
                                    <?= $v['content'] ?>
                                </td>
                                <td>
                                    <?= $v['addtime'] ?>
                                </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" 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'] ?>"><?= $v['number'] ?></a>
                            </li>
                        <?php } ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?= $shop_list['count'] ?>" aria-label="Next">
                                <span aria-hidden="true">»</span>
                                <span class="sr-only">Next</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div> <!-- end card-body-->
        </div> <!-- end card-->
    </div> <!-- end col -->
</div>
<?php include "bottom.php"; ?>
<script>
    var level = {
        unset: function (a) {
            layer.alert('删除后不可撤销，是否确认删除？', {
                icon: 3, title: '温馨提示', btn: ['取消', '确定删除'], btn2: function (layero, index) {
                    window.location.href = '?unset=' + a;
                }
            })
        },
        sort: function (gid, type) { //修改排序
            $.ajax({
                type: 'POST',
                url: 'ajax.php?act=setlevelSort',
                data: {
                    gid: gid,
                    type: type
                },
                dataType: 'json',
                success: function (data) {
                    location.reload();
                },
                error: function (data) {
                    layer.msg('服务器错误');
                    return false;
                }
            });
        }
    }
</script>