<?php
session_start();
include_once '../common.php';
include_once '../conn.php';



$uid = $_GET["uid"];
$dailiid = $_GET["dailiid"];
$dailiid1 = $_GET["dailiid1"];
if ($dailiid == "admin") {
    echo $dailiid = "";
}
if (!isset($_SESSION['username'])) {
    echo "<script>alert('身份已失效请重新登录!')</script><script>location.href='index.php'</script>";
    exit;
}
$username = ubo($_SESSION['username']);
$sql1 = mysql_query("SELECT * FROM zhucema WHERE  user='$username'");
$row = mysql_fetch_array($sql1);
if ($row[zdsl] != "") {
    echo "<script>location.href='index.php'</script>";
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="gb2312">
    <title>代理列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="css/layui.css" media="all">
    <link rel="stylesheet" href="css/peizhi.css">
    <script src='newui/js/jquery.min.js' type="text/javascript"></script>
    <script src="newui/layui/layui.js"></script>
    <script src='newui/js/myc.js' type="text/javascript"></script>
    <script src="newui/js/bootstrap.min.js"></script>
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-body">
        <!--tab标签-->
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">代理列表</li>
                <?php
                $sql = mysql_query("SELECT count(1) FROM zhucema");
                $bs = mysql_fetch_array($sql)[0];
                ?>
                <li class="layui">共有代理 <?php if ($bs == null) { ?>0<?php } else { ?><?php echo $bs ?><?php } ?> 个</li>
            </ul>
            <div class="layui-tab-content">
                <form class="layui-form layui-form-pane" action="" method="get">
                    <div class="layui-inline">
                        <label class="layui-form-label">代理ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="uid" value="<?php if ($uid == null) {
                            } else {
                                echo $uid;
                            } ?>" placeholder="代理ID" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn">搜索</button>
                    </div>
                </form>
                <hr>
                <div class="layui-tab-item layui-show">
                    <table class="layui-table" lay-even="" lay-skin="row">
                        <thead>
                        <tr>
                            <th align="center">代理ID</th>
                            <th align="center">用户名</th>
                            <th align="center">代理密码</th>
                            <th align="center">代理级别</th>
                            <th align="center">邀请码</th>
                            <th align="center">玩家数</th>
                            <th align="center">操作</th>
                            <th align="center">删除</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $Page_size = 18;
                        $sql = "WHERE 1=1";
                        if ($uid) {
                            $sql .= " and user='$uid' ";
                        }
                        $result = mysql_query("select count(1) from zhucema   " . $sql . "  ");
                        $count =mysql_fetch_array($result)[0];
                        if ($count == 0) {
                            echo '<tr><td colspan=15 align="center">查询不到数据</td></tr>';
                        }
                        $Page_count = ceil($count / $Page_size);
                        $init = 1;
                        $Page_len = 7;
                        $max_p = $Page_count;
                        $Pages = $Page_count;
                        //判断当前页码
                        if (empty($_GET['page']) || $_GET['page'] < 0) {
                            $Page = 1;
                        } else {
                            $Page = $_GET['page'];
                        }
                        $offset = $Page_size * ($Page - 1);
                        $query = mysql_query("select * from  zhucema   " . $sql . "  order by id desc  limit $offset, $Page_size");
                        while ($a = mysql_fetch_array($query)) {

                            $sql = mysql_query("SELECT count(1) FROM user WHERE recommend='" . $a[ma] . "'");
                            $bs = mysql_fetch_array($sql)[0];
                            ?>

                            <tr>
                                <td align="center"><?php echo $a[id] ?></td>
                                <td align="center"><?php echo $a[user] ?></td>
                                <td align="center"><?php echo $a[pass] ?></td>
                                <td align="center"><?php if ($a[zdsl] == '1') {
                                        echo '总代理';
                                    } else if ($a[zdsl] == '0') {
                                        echo '次级代理';
                                    } else if ($a[zdsl] == null) {
                                        echo '超级管理员';
                                    } ?></td>
                                <td align="center"><?php echo $a[ma] ?></td>
                                <td align="center"><?php echo $bs ?></td>
                                <td align="center"><?php echo '' . ('<a href="./wanjia.php?dailiid=' . $a[ma] . '" class="layui-btn"><i class="layui-icon"   style="">&#xe615;</i> 查询该代理玩家 </font></a>') . '' ?></td>
                                <td align="center">
                                    <a class="layui-btn layui-btn-normal layui-btn-mini publish_btn"
                                       data-money="<?php echo $a[money] ?>" data-id="<?php echo $a[id] ?>"
                                       data-toggle="modal" data-target="#myPublic" data-user="<?php echo $a[user] ?>"
                                       data-pass="<?php echo $a[pass] ?>" data-nikname="<?php echo $a[ma] ?>"
                                       data-qq="<?php echo $a[zd] ?>">编辑</a>
                                    <a onClick="sc('<?php echo $a[id] ?>')"
                                       class="layui-btn layui-btn-danger layui-btn-mini ajax-delete">删除</a></td>
                            </tr>
                            <?php
                        }
                        $Page_len = ($Page_len % 2) ? $Page_len : $Pagelen + 1;//页码个数
                        $Pageoffset = ($Page_len - 1) / 2;//页码个数左右偏移量
                        $key .= "<li><a class='number' >当前第 $Page 页/共 $Pages 页</a></li>"; //第几页,共几页
                        if ($Page != 1) {
                            if ($uid) {
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=1&uid=$uid\">&laquo;</a></li>"; //首页
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . ($Page - 1) . "&uid=$uid\">&lt;</a></li>"; //上一页
                            } else {
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=1\">&laquo;</a></li>"; //首页
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . ($Page - 1) . "\">&lt;</a></li>"; //上一页
                            }
                        } else {
                            $key .= "<li><a>&laquo;</a> "; //首页
                            $key .= "<li><a >&lt;</a>"; //上一页
                        }
                        if ($Pages > $Page_len) {
//如果当前页小于等于左偏移 
                            if ($Page <= $Pageoffset) {
                                $init = 1;
                                $max_p = $Page_len;
                            } else {//如果当前页大于左偏移
//如果当前页码右偏移超出最大分页数 
                                if ($Page + $Pageoffset >= $Pages + 1) {
                                    $init = $Pages - $Page_len + 1;
                                } else {
//左右偏移都存在时的计算 
                                    $init = $Page - $Pageoffset;
                                    $max_p = $Page + $Pageoffset;
                                }
                            }
                        }
                        for ($i = $init; $i <= $max_p; $i++) {
                            if ($i == $Page) {
                                $key .= '<li class="active"><span>' . $i . '</span></li>';
                            } else {
                                if ($uid) {
                                    $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . $i . "&uid=$uid\">" . $i . "</a></li>";
                                } else {
                                    $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . $i . "\">" . $i . "</a></li>";
                                }
                            }
                        }
                        if ($Page != $Pages) {
                            if ($uid) {
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . ($Page + 1) . "&uid=$uid\">&gt;</a></li> ";//下一页
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page={$Pages}&uid=$uid\">&raquo;</a></li> "; //最后一页
                            } else {
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . ($Page + 1) . "\">&gt;</a></li> ";//下一页
                                $key .= "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?page={$Pages}\">&raquo;</a></li> "; //最后一页
                            }
                        } else {
                            $key .= "<li>&gt;</li>";//下一页
                            $key .= "<li>&raquo;</li>"; //最后一页
                        }
                        $key .= '';
                        ?>
                        </tbody>
                    </table>
                    <ul class="pagination">
                        <?php if ($count == "0") { ?><?php } else { ?><?php echo $key ?><?php } ?>
                    </ul>
</body>
<script type="text/javascript">
    function sc(id) {
        if (confirm("确认删除吗")) {
            $.ajax({
                type: "post",
                url: "data/usersc.php",
                dataType: "json",
                async: true,
                data: {id: id},
                timeout: 10000,
                success: function (data) {
                    if (data.zt == '1') {
                        myc.toast({
                            msg: '删除成功'
                        });
                        location.reload();
                    }
                },
                error: function () {
                }
            });
        } else {
            return;
        }
//
    }

    $(function () {
        $("#publish_btn").click(function () {
            var id = $("#recipient-id").val();
            var pass = $("#recipient-pass").val();
            $.ajax({
                type: "post",
                url: "data/userxg.php",
                dataType: "json",
                async: true,
                data: {id: id, pass: pass},
                timeout: 10000,
                success: function (data) {
                    if (data.zt == '1') {
                        myc.toast({
                            msg: '更新成功'
                        });
                        location.reload();
                    } else {
                        myc.toast({
                            msg: data.msg
                        });
                    }
                },
                error: function () {
                }
            });
        });
    })
    $('.publish_btn').on('click', function () {
        $('#recipient-pass').val($(this).data('pass'));
        $('#recipient-id').val($(this).data('id'));
    })
</script>
<div class="modal fade" id="myPublic" tabindex="-1" role="dialog" aria-labelledby="myPublicLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <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="myModalLabel">修改代理资料</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" name="id" class="form-control" id="recipient-id">
                <div class="form-group">
                    <label class="control-label">密码</label>
                    <input type="text" name="pass" class="layui-input" id="recipient-pass">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" id="publish_btn" class="btn btn-primary">确认</button>
            </div>
        </div>
    </div>
</div>
</div>
</div>
</div>
</div>
</html>