<?php
require '../conn/conn.php';
require '../conn/function.php';
require 'admin_check.php';
$action=$_GET["action"];
$id=intval($_GET["id"]);

if($id>0){
  $S=getrs("select * from sl_short where S_id=".$id);

  $t1=getrs("select count(DISTINCT S_ip) as S_count from sl_statistics where S_lid=".$id,"S_count");
  $t2=getrs("select count(S_id) as S_count from sl_statistics where S_lid=".$id,"S_count");
  $t3=getrs("select count(DISTINCT S_ip) as S_count from sl_statistics where S_lid=".$id." and to_days(S_time) = to_days(now())","S_count");
  $t4=getrs("select count(S_id) as S_count from sl_statistics where S_lid=".$id." and to_days(S_time) = to_days(now())","S_count");
  $S_short=$S["S_short"];
}else{
  $S_short="全部短链";
  $t1=getrs("select count(DISTINCT S_ip) as S_count from sl_statistics","S_count");
  $t2=getrs("select count(S_id) as S_count from sl_statistics","S_count");
  $t3=getrs("select count(DISTINCT S_ip) as S_count from sl_statistics where to_days(S_time) = to_days(now())","S_count");
  $t4=getrs("select count(S_id) as S_count from sl_statistics where to_days(S_time) = to_days(now())","S_count");
}


$page=$_GET["page"];

if($page==""){
  $page=1;
}

if($action=="delall"){
  $id=$_POST["id"];
  if(count($id)>0) {
    $shu=0 ;
    for ($i=0 ;$i<count($id);$i++ ) {
      mysqli_query($conn,"delete from sl_statistics where S_id=".$id[$i]);
      $shu=$shu+1;
      $ids=$ids.$id[$i].",";
    }
    $ids= substr($ids,0,strlen($ids)-1);
    if($shu>0) {
      die("{\"msg\":\"success\",\"ids\":\"".$ids."\"}");
    } else {
      die("{\"msg\":\"删除失败\"}");
    }
  } else {
    die("{\"msg\":\"未选择要删除的内容\"}");
  }
}

if($action=="excel"){
  $sql="select sl_statistics.S_id,sl_short.S_url,sl_short.S_short,sl_statistics.S_IP,sl_statistics.S_add,sl_statistics.S_browser,sl_statistics.S_terminal,sl_statistics.S_time from sl_short,sl_statistics where sl_short.S_id=sl_statistics.S_lid";
  $result = mysqli_query($conn, $sql);
  $arr = array();
  while($row = mysqli_fetch_array($result)) {
    $row["S_url"]=urldecode($row["S_url"]);
  $count=count($row);
    for($i=0;$i<$count;$i++){ 
      unset($row[$i]);
    }
    array_push($arr,$row);
  } 

  exportToCsv(["ID","原始链接","短链接","IP","地址","浏览器","设备","点击时间"] , $arr ,'statistics_'.date('YmdHis'),[]);
  die();
}

if($id>0){
  $sql="select count(S_id) as S_count from sl_statistics where S_lid=$id";
}else{
  $sql="select count(S_id) as S_count from sl_statistics";
}
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$S_counts=$row["S_count"];
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>短链统计 - 后台管理</title>
<link rel="icon" href="../media/<?php echo $C_ico?>" type="image/ico">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/materialdesignicons.min.css" rel="stylesheet">
<link href="../css/style.min.css" rel="stylesheet">
<link rel="stylesheet" href="../js/jconfirm/jquery-confirm.min.css">
<style type="text/css">
.pagination{margin: 0px;}
</style>
</head>
  
<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
  <?php require 'nav.php';?>
    <main class="lyear-layout-content">
      <div class="container-fluid">
        <div class="row">
        <div class="col-md-12">  
<div class="card">
              <div class="card-header"><h4><?php echo $S_short?> - 点击统计 <a href="?id=0" class="btn btn-primary btn-sm">查看全部</a></h4>
                <a href="?action=excel" class="pull-right btn btn-info btn-sm">导出EXCEL</a>
              </div>
              <div class="card-body">
              
  <div class="row">
    <div class="col-md-3">总IP数：<?php echo $t1;?></div>
    <div class="col-md-3">总点击数：<?php echo $t2?></div>
    <div class="col-md-3">今日IP数：<?php echo $t3;?></div>
    <div class="col-md-3">今日点击数：<?php echo $t4;?></div>
  </div>
              </div>
              </div>
</div>

          <div class="col-md-12">
            <form id="list">
            <div class="card">
              
              
                  <table class="table">
                    <tr><th>选择</th><th>短链接</th><th>IP地址</th><th>浏览器</th><th>访问时间</th><th>电脑/移动端</th><th>地址</th></tr>
                    <?php
if($id>0){
  $sql="select * from sl_statistics where S_lid=$id order by S_id desc limit ".(($page-1)*20).",20";
}else{
  $sql="select * from sl_statistics order by S_id desc limit ".(($page-1)*20).",20";
}

$result = mysqli_query($conn,  $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {

  echo "<tr id=\"".$row["S_id"]."\"><td><input type=\"checkbox\" name=\"id[]\" value=\"".$row["S_id"]."\"></td><td>".getrs("select * from sl_short where S_id=".$row["S_lid"],"S_short")." <a href=\"?id=".$row["S_lid"]."\" class=\"btn btn-primary btn-xs\">单独查看</a></td><td>".$row["S_ip"]."</td><td>".$row["S_browser"]."</td><td>".$row["S_time"]."</td><td>".$row["S_terminal"]."</td><td>".$row["S_add"]."</td></tr>";
    }
} 
                    ?>
                  </table>
                  
            </div>
            <label><input type="checkbox" id="selectAll" name="selectAll"> 全选</label>
            <button class="btn btn-sm btn-danger" type="button" onClick="delall()"><i class="fa fa-times-circle" ></i> 删除所选</button>

            <ul class="pagination" id="pagination" style="float: right;"></ul>
                  <input type="hidden" id="PageCount" runat="server" />
        <input type="hidden" id="PageSize" runat="server" value="20" />
        <input type="hidden" id="countindex" runat="server" value="20"/>
        <!--设置最多显示的页码数 可以手动设置 默认为7-->
        <input type="hidden" id="visiblePages" runat="server" value="7" />
        </form>
          </div>

        </div>
      </div>
    </main>
    <!--End 页面主要内容-->
  </div>
</div>



<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../js/main.min.js"></script>
<script src="../js/jconfirm/jquery-confirm.min.js"></script>
<script src="../js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="../js/lightyear.js"></script>
<script src="../js/jqPaginator.min.js" type="text/javascript"></script>
<script type="text/javascript">

  function loadData(num) {
            $("#PageCount").val("<?php echo $S_counts?>");
        }
function loadpage(id) {
    var myPageCount = parseInt($("#PageCount").val());
    var myPageSize = parseInt($("#PageSize").val());
    var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
    $("#countindex").val(countindex);

    $.jqPaginator('#pagination', {
        totalPages: parseInt($("#countindex").val()),
        visiblePages: parseInt($("#visiblePages").val()),
        currentPage: id,
        first: '<li class="first page-item"><a href="javascript:;" class="page-link">|<</a></li>',
        prev: '<li class="prev page-item"><a href="javascript:;" class="page-link"><i class="arrow arrow2"></i><</a></li>',
        next: '<li class="next page-item"><a href="javascript:;" class="page-link">><i class="arrow arrow3"></i></a></li>',
        last: '<li class="last page-item"><a href="javascript:;" class="page-link">>|</a></li>',
        page: '<li class="page page-item"><a href="javascript:;" class="page-link">{{page}}</a></li>',
        onPageChange: function (num, type) {
            if (type == "change") {
                window.location="?id=<?php echo $id?>&page="+num;
            }
        }
    });
}
$(function () {
    loadData(<?php echo $page?>);
    loadpage(<?php echo $page?>);

});


function delall() {
    $.alert({
        title: '确认删除',
        content: '确认删除？',
    buttons: {
      confirm: {
        text: '确认',
        btnClass: 'btn-primary',
        action: function(){
          $.ajax({
            url: '?action=delall',
            type: 'post',
            data: $("#list").serialize(),
            success: function(data) {
                data = JSON.parse(data);
                if (data.msg == "success") {
                  lightyear.notify('删除成功', 'success', 100);
                    id = data.ids.split(",");
                    for (var i = 0; i < id.length; i++) {
                        $("#" + id[i]).hide();
                    };
                } else {
                  lightyear.notify(data.msg, 'danger', 100);

                }
            }
        });
        }
      },
      cancel: {
        text: '取消',
        action: function () {

        }
      }
    }
    });

}

$('input[name="selectAll"]').on("click",function(){
        if($(this).is(':checked')){
            $('input[name="id[]"]').each(function(){
                $(this).prop("checked",true);
            });
        }else{
            $('input[name="id[]"]').each(function(){
                $(this).prop("checked",false);
            });
        }
    });


  var path=window.location.pathname;
  $("a").each(function(){
    if(path.indexOf($(this).attr("href"))!=-1){
      console.log($(this).attr("href"));
      $(this).parent("li").addClass("active");
      $(this).parent("li").parent("ul").parent("li").addClass("open active");
    }
  });
</script>
</body>
</html>