<?php

session_start();
include ('connect.php');

if(empty($_SESSION['user_login'])){

	header("Location: login.php");
}else{
  $start = 0 ;
  $limite = 100;
  $pageNow = 1;


  if(isset($_GET['page'])){
    if($_GET['page'] == 1){

      $start = 0 ;
      $pageNow = 1;

    }else{
      $start = ($_GET['page']-1)* $limite;
      $limite = 100;
      $pageNow = $_GET['page'];
    }
  }

  // $inicio = ($pg * $limite) - $limite;

  $project_name = $_GET['project_name'];
  $filter = "";
  if($project_name != "all"){
      $filter = "and project_name = '".$project_name."'";
  }


  $sql = "SELECT * FROM  requests WHERE span_status = '1' $filter and delete_status=0 order by id desc limit $start,$limite";
	   
  $result = mysqli_query($conn,$sql);

  $sql = "SELECT count(id) as total FROM  requests WHERE span_status = '1' $filter and delete_status=0 ";     
  $total = mysqli_query($conn,$sql);
  $totalRes = mysqli_fetch_assoc($total);	
}

?>


<!DOCTYPE html>

<html
  lang="en"
  class="light-style layout-menu-fixed"
  dir="ltr"
  data-theme="theme-default"
  data-assets-path="assets/"
  data-template="vertical-menu-template-free"
>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
    />

    <title>Span Data</title>

    <meta name="description" content="" />

    <!-- Favicon -->
    <link rel="icon" type="image/x-icon" href="assets/img/favicon/favicon.ico" />

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap"
      rel="stylesheet"
    />

   
    <link rel="stylesheet" href="assets/vendor/fonts/boxicons.css" />

    <!-- Core CSS -->
    <link rel="stylesheet" href="assets/vendor/css/core.css" class="template-customizer-core-css" />
    <link rel="stylesheet" href="assets/vendor/css/theme-default.css" class="template-customizer-theme-css" />
    <link rel="stylesheet" href="assets/css/demo.css" />

    <!-- Vendors CSS -->
    <link rel="stylesheet" href="assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.css" />

    <script src="assets/vendor/js/helpers.js"></script>

    <script src="assets/js/config.js"></script>
  </head>

  <body>
    <!-- Layout wrapper -->
    <div class="layout-wrapper layout-content-navbar">
      <div class="layout-container">
        

        <!-- Layout container -->
        <div class="layout-page">
          <!-- Navbar -->

         <?php include('navbar.php'); ?>

          <!-- / Navbar -->

          <!-- Content wrapper -->
          <div class="content-wrapper">
            <!-- Content -->

            <div class="container-xxl flex-grow-1 container-p-y">

            <div class="container-xxl flex-grow-1 container-p-y">
            <div class="card" id="offerAcceptDenyResultDiv">
            <span style="color:red;text-align: center;" id="offerAcceptDenyResult" ></span>
            </div>
              
              <!-- Basic Bootstrap Table -->
              <div class="card">
               <!--  <h5 class="card-header">Table Basic</h5> -->
                <div class="table-responsive">
                  <table class="table">
                    <thead>                   
                      <tr>
                        <th>ID</th>
                        <th>Project</th>
                        <th>Sender</th>
                        <!--<th>Message</th>-->
                         <th>Source</th>
                        <th>Date</th>
                        <th>Location</th>
                        <th>Mobile</th>
                        <th>IP</th>
                        <th>Email</th>
                        <th>Action</th>
                     
                      </tr>
                    
                    </thead>
                    <tbody class="table-border-bottom-0">

                    	<?php 
                      if(mysqli_num_rows($result) > 0){

          						while($row=mysqli_fetch_array($result))
                       
          						{ ?>

	                      <tr id="tr<?=$row['id']?>">
	                        <td><?php echo $row['id']?></td>
                          <td><?php echo $row['project_name']?></td>
                          <td><?php echo $row['name']?></td>
                            <td><?php echo (!empty($row['utm_source'])) ? $row['utm_source'] : "N/A" ?></td>
                          <!--<td><?php echo (!empty($row['message'])) ? $row['message'] : "N/A" ?></td>-->
                          <td><?php echo $row['request_datetime']?></td>
                          <td><?php echo $row['city']." (".$row['country'].")"?></td>
                          <td><?php echo "(".$row['calling_code'].") XXXXXX".substr($row['mobile'], -4);
                          ?></td>
                          <td><?php echo $row['ip_address']?></td>
                          <td><?php echo substr($row['email'], 0,3)."xxxx@gmail.com";?></td>
                          <td>
                          <a href="javascript:void(0);" onclick="movetomicrosites(<?=$row['id']?>)" title="Move to Microsites">
                                 <img src="assets\img\spam.jpg" width="24px;">
                                </a>
                          <?php if($_SESSION['admin_id'] == 1){ ?>
                          <span onclick="deletefunction(<?=$row['id']?>)"><img src="assets\img\delete.jpg" width="24px;"></span>
                          <?php } ?>
                        </td> 
                          	                        
	                      </tr>

                       <?php } } else{ echo '<tr><td colspan="8" style="text-align:center;">No Found Data</td></tr>'; }?>
                      
                     
                    </tbody>
                  </table>

                 
                </div>

                 <!-- (E2) PAGINATION -->
                <div class="pagination">
                <?php

                  $totalpages = ceil($totalRes['total']/$limite);
                 
                  if($totalpages > 1){
                    echo '<a class="btn btn-primary" href = "spam.php?project_name='.$_GET['project_name'].'&page=1">First</a>';
                  }
                 
                  $num = 1;
                  $showbuttoncount = 1;
                  if($pageNow > 2) {
                    $startfor = $pageNow -2;
                    $showbuttoncount = 3;
                    for($num =  $startfor; $num < ($startfor+2); $num ++) {                     
                     
                      if($pageNow==$num){
                        echo '<a class="btn btn-dark active mx-1" href = "spam.php?project_name='.$_GET['project_name'].'&page=' . $num . '">' . $num . ' </a>';
                        } else{
                        echo '<a class="btn btn-primary mx-1" href = "spam.php?project_name='.$_GET['project_name'].'&page=' . $num . '">' . $num . ' </a>';
                        }
                    }
                       
                    
                  }
                  
                  for($num =  $num; $num <= $totalpages; $num++) { 
                    $showbuttoncount++;
                      if($showbuttoncount > 5){                         
                        break;
                      }

                      if($pageNow==$num){
                      echo '<a class="btn btn-dark active mx-1" href = "spam.php?project_name='.$_GET['project_name'].'&page=' . $num . '">' . $num . ' </a>';
                      } else{
                      echo '<a class="btn btn-primary mx-1" href = "spam.php?project_name='.$_GET['project_name'].'&page=' . $num . '">' . $num . ' </a>';
                      }  
                  }

                  if($totalpages > 1){
                   echo '<a class="btn btn-primary" href = "spam.php?project_name='.$_GET['project_name'].'&page='.$totalpages.'">Last</a>';
                  }
                ?>
                </div>

              </div>
          

            </div>
            <!-- / Content -->


            <div class="content-backdrop fade"></div>
          </div>
          <!-- Content wrapper -->
        </div>
        <!-- / Layout page -->
      </div>

      
    </div>
    

    <script src="assets/vendor/libs/jquery/jquery.js"></script>
    <script src="assets/vendor/libs/popper/popper.js"></script>
    <script src="assets/vendor/js/bootstrap.js"></script>
    <script src="assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.js"></script>

    <script src="assets/vendor/js/menu.js"></script>
    <!-- endbuild -->

    <!-- Vendors JS -->

    <!-- Main JS -->
    <script src="assets/js/main.js"></script>

    <!-- Page JS -->

    <!-- Place this tag in your head or just before your close body tag. -->
    <script async defer src="https://buttons.github.io/buttons.js"></script>
  
  </body>
  <script type="text/javascript">
      
      function movetomicrosites(id){
          if(confirm("Are you sure you want move to microsites this?"))
        {
            
        document.getElementById('tr'+id).style.display = "none";
        var movetomicrosites = "true";
        $.ajax({
            header: { 'Access-Control-Allow-Origin':'*'},
            url : "all.php",
            data: {'movetomicrosites':movetomicrosites,'id':id},                              
            cache: 'false',
            type: 'post',
            dataType: "json",           
            success: function(data){
                if (data.status == 'success') {
                    $("#offerAcceptDenyResultDiv").addClass('alert-success').show();
                    $("#offerAcceptDenyResult").text(data.message);
                    setTimeout(function(){
                        location.reload();
                    }, 1000);
                }
                else {

                    $("#offerAcceptDenyResultDiv").addClass('alert-danger').show();
                    $("#offerAcceptDenyResult").text('Some error occurred, Please try again');

                }
                
            }
              
        })
      }
      }

      function deletefunction(id){

        if(confirm("Are you sure you want to delete this?"))
        {
        document.getElementById('tr'+id).style.display = "none";
        var deleteRequests = "true";
          $.ajax({
              header: { 'Access-Control-Allow-Origin':'*'},
              url : "all.php",
              data: {'deleteRequests':deleteRequests,'id':id},                              
              cache: 'false',
              type: 'post',
              dataType: "json",
              success: function(data){
                if (data.status == 'success') {
                    $("#offerAcceptDenyResultDiv").addClass('alert-success').show();
                    $("#offerAcceptDenyResult").text(data.message);
                    setTimeout(function(){
                        location.reload();
                    }, 1000);
                }
                else {

                    $("#offerAcceptDenyResultDiv").addClass('alert-danger').show();
                    $("#offerAcceptDenyResult").text('Some error occurred, Please try again');

                }
                
            }
                
          })
        }
      }
      
    </script>
</html>

