<?php
session_start();
include_once '../../common.php';
include_once '../../conn.php';
$uid = $_GET["uid"];
$dailiid = $_GET["dailiid"];
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] != "1") {
    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 where zd='$username' ");
                $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">
                        <a class="layui-btn pub_btn" data-toggle="modal" data-target="#myPublic2">添加二级代理</a>
                    </div>

                    <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">代理</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>
                            <th align="center">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $Page_size = 18;
                        $sql = "WHERE zd='$username'";
                        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 . " and user!='$username'   order by id desc  limit $offset, $Page_size");
                        $num   =  0;
                        $day = strtotime(date("Y-m-d"), time());
                        $jinri = $day + 86400;
                        $jinri = date("Y-m-d H:i:s", $jinri);
                        $zuori = $day - 86400;
                        $zuori = date("Y-m-d H:i:s", $zuori);
                        $day = date("Y-m-d H:i:s", $day);
                        while ($a = mysql_fetch_array($query)) {
                            $sql0 = mysql_query("select * from user where recommend='" . $a[ma] . "' ");
                            $bs0 = mysql_num_rows($sql0);


                            $sql = mysql_query("select sum(rmb) from chongzhi where recommend  ='" . $a[ma] . "' and 1=1 ");
                            $bs = mysql_result($sql, 0);;

                            $sql1 = mysql_query("select sum(rmb) from chongzhi where recommend  ='" . $a[ma] . "' and 1=1  and shijian>='" . $zuori . "' and shijian<'" . $day . "' ");
                            $bs1 = mysql_result($sql1, 0);;

                            $ssql = mysql_query("select sum(rmb) from chongzhi where recommend  ='" . $a[ma] . "' and 1=1  and shijian>='" . $day . "' and shijian<'" . $jinri . "' ");
                            $bs3 = mysql_result($ssql, 0);;
                            ?>

                            <tr>
                                <td align="center"><?php echo $a[user] ?></td>
                                <td align="center"><?php echo $bs0 ?>人</td>
                                <td align="center"><?php if ($bs3 == null) { ?>￥0元<?php } else { ?>￥<?php $xs1 = round($bs3, 2);
                                        echo $xs1; ?> 元<?php } ?></td>
                                <td align="center"><?php if ($bs1 == null) { ?>￥0元<?php } else { ?>￥<?php $xs1 = round($bs1, 2);
                                        echo $xs1; ?> 元<?php } ?></td>
                                <td align="center"><?php if ($bs == null) { ?>￥0元<?php } else { ?>￥<?php $xs1 = round($bs, 2);
                                        echo $xs1; ?> 元<?php } ?></td>
                                <td align="center"><?php if ($bs == null) { ?>￥0元<?php } else { ?>￥<?php $xs1 = round($bs, 2);
                                        echo $xs1 * ($a[reward] / 100 * 0.96); ?> 元<?php } ?>
                                    &emsp;<br>(<?php echo $a[reward] ?>%比例 - 4%支付平台手续费)
                                </td>
                                <td align="center"><?php echo $a[remarks] ?></td>
                                <td align="center"><a class="layui-btn pub_btn" data-toggle="modal"
                                                      data-target="#myPublic3"
                                                      data-user="<?php echo $a[user] ?>">修改分成比例</a>
                                    <?php echo '' . ('<a href="./dingdan.php?uid=' . $a[ma] . '" class="layui-btn"><i class="layui-icon"   style="">&#xe615;</i> 详细流水 </font></a>') . '' ?>
                                </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>
                            </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>
                </div>
                <div class="modal fade" id="myPublic2" 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">添加二级代理【密码默认123456,宣传码等于用户名】</h4>
                            </div>
                            <div class="modal-body">

                                <div class="form-group">
                                    <label class="control-label">用户名</label>
                                    <input type="text" class="layui-input" id="name" placeholder="输入用户名">
                                    <label class="control-label">分成比例</label>
                                    <input type="text" class="layui-input" id="reward" placeholder="请填小于100的数字，默认80">
                                    <label class="control-label">备注</label>
                                    <input type="text" class="layui-input" id="remarks" placeholder="可为空">
                                </div>

                                <div class="form-group">
                                    <label class="control-label">所属总代</label>
                                    <select type="text" class="layui-input" id="daili" placeholder="请输入代理邀请码">
                                        <?php
                                        echo '<option value="' . $username . '">' . $username . '</option>';
                                        ?>
                                    </select>
                                </div>

                                <div class="modal-footer" style="text-align: left;">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                    <button type="button" id="pub_btn" class="btn btn-primary">确认</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="myPublic3" tabindex="-1" role="dialog" aria-labelledby="myPublicLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-body">

                                <div class="form-group">
                                    <label class="control-label">分成比例</label>
                                    <input type="text" class="layui-input" id="reward2" placeholder="请填小于100的数字">.
                                    <label class="control-label">备注</label>
                                    <input type="text" class="layui-input" id="remarks2" placeholder="为空则不修改">
                                </div>

                                <div class="modal-footer" style="text-align: left;">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                    <button type="button" id="pub_btn2" class="btn btn-primary">确认</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <script type="text/javascript">
                    $(function () {
                        var name = '';
                        $('.pub_btn').on('click', function () {
                            name = $(this).attr('data-user');
                        })
                        $("#pub_btn").click(function () {
                            var name = $("#name").val();
                            var daili = $("#daili").val();
                            var reward = $("#reward").val();
                            var remarks = $("#remarks").val();
                            $.ajax({
                                type: "post",
                                url: "data/erjiadd.php",
                                dataType: "json",
                                async: true,
                                data: {name: name, daili: daili, reward: reward, remarks: remarks},
                                timeout: 10000,
                                success: function (data) {
                                    if (data.zt == '1') {
                                        myc.toast({
                                            msg: '添加成功'
                                        });
                                        location.reload();
                                    } else {
                                        myc.toast({
                                            msg: data.msg
                                        });
                                    }
                                },
                                error: function () {
                                }
                            });
                        });
                        $("#pub_btn2").click(function () {
                            var reward = $("#reward2").val();
                            var remarks2 = $("#remarks2").val();
                            $.ajax({
                                type: "post",
                                url: "data/erjiadd.php",
                                dataType: "json",
                                async: true,
                                data: {name: name, reward: reward, type: 'reward',remarks:remarks2},
                                timeout: 10000,
                                success: function (data) {
                                    if (data.zt == '1') {
                                        myc.toast({
                                            msg: '修改成功'
                                        });
                                        location.reload();
                                    } else {
                                        myc.toast({
                                            msg: data.msg
                                        });
                                    }
                                },
                                error: function () {
                                }
                            });
                        });
                    })
                </script>
            </div>
        </div>
    </div>
</body>
</html>
</div>
</body>
</html>