<?php
include "../include/config.php";
session_start();
$school=$_SESSION['school'];
if (isset($_SESSION['email'])){?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Results SMS </title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <style>
        body { 
            padding: 0px;
            margin: auto;
            background: white;
        }
                .dash{

            justify-content:center;
            display:flex;
            flex-wrap:wrap;
            width:100%;
            margin-top: 15px;
        }
a{
  text-decoration: none;
}
    .dash a {
        width:40%;

        background: white;
 box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px, rgba(0, 0, 0, 0.06) 0px 2px 4px -1px;
        padding:2px;
        margin:10px;
        border-radius:4px;
        color:gray;
        text-align: center;
        text-decoration: none;
    }
    .dash a:hover{
      color:#0ea17e;
    }

    .dash i{
      color:#556DCF;
    }
    .profile .box{
        background: linear-gradient(90deg, #0ea17e ,#6ddaa3);
        box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px, rgba(0, 0, 0, 0.06) 0px 2px 4px -1px;
        width:45%;
        margin:1px;
        box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
    }
    .profile{
        justify-content:center;
            display:flex;
            flex-wrap:wrap;
            width:100%;
            border-radius:4px ;
    }
    .balance{
      box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
      background: linear-gradient(90deg, #556DCF ,white);
      padding: 10px;
      width: 92%;
      text-align: center;
      border-radius: 5px;
      margin-top: -10px;
      border: none;
      color: white;
      box-shadow: 0px 6px 10px rgba(0, 0, 0, 3);
      height: 180px;
    }
    .balance h2{
      margin-top: -10px;
    }
    .balance p{
      color: white;
      margin-top: -10px;
    }
    .balance #i{
      float:left;
      border: 1px solid white;
      padding: 5px;
      border-radius: 50%;
      width: 40px;
      height: 40px;
      margin-left: 10px;
    }
    .balance span{
      float: left;
      margin-left: 10px;
      margin-top: 10px;
    }
    .procard i{
      padding: 25px;
      
      font-size: 60px;
      
      color: white;
    }
    .balance .btn{
      
      padding: 7px;
      font-size: 12px;
      float: right;
      margin-left:80px;
      margin-top:-50px;
    }        .container {
        }
        h1 {
            font-size: 28px;
            margin-bottom: 20px;
            text-align: center;
            color: white;
        }
        .sms-balance {
            font-size: 20px;
            margin-bottom: 20px;
            color: orange;
            text-align: center;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-bottom: 10px;
            font-weight: bold;
            color: #555;
        }
        .alert-danger, .alert-success{
        background: transparent;
        margin-top:10px;
        }
        input[type="submit"] {
            padding: 12px;
            background-color: #54b68f;
            color: #fff;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        input[type="submit"]:hover {
            background-color: #2980b9;
        }
        .header {
            background: #556DCF;
            margin: auto;
            height: 300px;
        }
        form {
            padding: 20px;
        }
        b {
            font-size: 60px;
            color: white;
        }
        .btn {
            color: gray;
            background: white;
            width: ;
            margin: 10px;
        }
        .form-control {
            width: 100%;
            margin: 5px;
        }
        .content{
            margin-left:180px;
        }
        @media only screen ans (max-width:500px){
         .content{
            margin-left:0px;
        }   
        }
    </style>
</head>
<!-- Modal -->
<div class="modal fade" id="tro" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Status</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
       <table class="table">
         <tr>
           <th>Term</th>
           <th>Year</th>
           <th>Status</th>
         </tr>
  <?php
$query = mysqli_query($conn, "SELECT * FROM schedule ORDER BY term DESC");
                while ($row = mysqli_fetch_array($query)) { ?>   
         <tr>
     <td><?php echo $row['term'];?></td>
      <td><?php echo $row['year'];?></td>
     <td><?php echo $row['status'];?></td>     
         </tr>
         <?php
         }
?>
       </table>
      </div>
    </div>
  </div>
  </div>


<body style="background:white; height:100vh">
  <?php include "../include/nav.php";?>
  <div class="content"> 
      <br><br>
      <div class="header">
      
        <br><br><br>
                 <center>
           
                    <div class="balance">

           <h3>SMS BALANCE</h3>

           <h2>
           
           
           
           <?php
           $query = "SELECT COUNT(*) AS total_sent FROM sms_logs WHERE message LIKE '%$school%'";

$result = mysqli_query($conn, $query);

// Check if query execution was successful
if ($result) {
    $row = mysqli_fetch_assoc($result); // Fetch the result as an associative array
    $total_sent = $row['total_sent'] ?? 0; // Use null coalescing operator to handle NULL values

// Display the total
} else {
    echo "Error in query: " . mysqli_error($conn); // Debugging error message
}
// Assuming $conn is your valid database connection
$school = mysqli_real_escape_string($conn, $school); // Prevent SQL injection

$query = "SELECT SUM(sms_count) AS total_sms FROM sms_purchases WHERE school_name='$school'";
$result = mysqli_query($conn, $query);

// Check if query execution was successful
if ($result) {
    $row = mysqli_fetch_assoc($result); // Fetch the result as an associative array
    $total_sms = $row['total_sms'] ?? 0; // Use null coalescing operator to handle NULL values
    $balance=$total_sms-$total_sent;
    echo $balance; // Display the total or 0 if no data exists
} else {
    echo "Error in query: " . mysqli_error($conn); // Debugging error message
}
?>



<span style="font-size:10px;float: center; margin-top:-30px">                   <i style="font-size:11px" class="fa fa-money"></i> ZMW</span></h2>
                   <i id="i" style="font-size:30px" class="fa fa-user"></i>   
                   <span> Hi Admin  </span>
                   
                   <img style="margin-top:-10px; float:right;"  src="chip.png" width="90px" >

<h4 style="color:yellow; font-size:12px;">SenderID:ZAMATECHNOL</h1>
<a href="senderid" style="color: white"><?php echo $school;?> </a>
         </div>
         
<a href="../senderid" class="btn btn-warning">Create custom SenderId</a>
      </div>

      <?php

if (isset($_GET['sms'])) {?>
<div class=" alert-success" style="text-align:center;">
<p><i class="fa fa-check-circle">  </i> <?php echo $_GET['sms'];?></p>
</div>
<?php
}
?>
<?php

if (isset($_GET['smsf'])) {?>
<div class="alert-danger" style="text-align:center;">
<p><i class="fa fa-warning">  </i> <?php echo $_GET['smsf'];?></p>
</div>
<?php
}
?>
               <div class="dash">


              <a data-toggle="modal" data-target="#exampleModalCentersms" style="text-decoration: none; "> 
                 <div class="box"  >

              <i style="font-size:30px" class="fa fa-share"></i>

                
                <p>SEND RESULTS</p>
            </div>
              </a>
         
       <a href="../buysms" style="text-decoration: none;"> 
            <div class="box"  >
              <i style="font-size:30px" class="fa fa-credit-card"></i>
                
                             <p>TOP UP</p>
            </div>
          
                        </a>
   <a href="smslogs.php"  style="text-decoration: none;"> 
                        <div class="box"  >

              <i style="font-size:30px" class="fa fa-cogs"></i>

                
                             <p>SMS LOGs</p>
            </div>
            
                          </a>
  <a data-toggle="modal" data-target="#parent" style="text-decoration: none;"> 
                        <div class="box"  >

              <i style="font-size:30px" class="fa fa-envelope"></i>

                
                <p>SMS PARENTS</p>
            </div>
                          </a>
         </div>
  
    
      
      
      
      
      
  </div>
</body>
</html>
<?php
} else {
    header("location:index.php");
}
?>

<!-- Modal -->
<div class="modal fade" id="exampleModalCentersms" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Send Results</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="sms_.php" method="POST">
          <p>
            <span style="color:red">Note:</span> If the network connection is interrupted during the transmission, do not refresh the page.
          </p>

          <!-- Select Session -->
          
          <select name="session" class="form-control" required>
            <option value="">Select Session</option>
            <?php
            $query = mysqli_query($conn, "SELECT * FROM session ORDER BY id DESC");
            while ($row = mysqli_fetch_array($query)) { ?>
              <option value="<?php echo $row['session']; ?>"><?php echo $row['session']; ?></option>
            <?php } ?>
          </select>

          <!-- Select Class -->
          
          <select name="class" class="form-control" required>
            <option value="">Select Class</option>
            <?php
            $query = mysqli_query($conn, "SELECT * FROM class ORDER BY id DESC");
            while ($row = mysqli_fetch_array($query)) { ?>
              <option value="<?php echo $row['class']; ?>"><?php echo $row['class']; ?></option>
            <?php } ?>
          </select>

          <!-- Select Year -->
          
          <select name="year" class="form-control" required>
            <option value="">Select Year</option>
            <?php
            $query = mysqli_query($conn, "SELECT * FROM year ORDER BY id DESC");
            while ($row = mysqli_fetch_array($query)) { ?>
              <option value="<?php echo $row['year']; ?>"><?php echo $row['year']; ?></option>
            <?php } ?>
          </select>

          <!-- Select Term -->
          
          <select name="term" class="form-control" required>
            <option value="">Select Term</option>
            <option value="1">Term 1</option>
            <option value="2">Term 2</option>
            <option value="3">Term 3</option>
          </select>
<input type="hidden" name="school" value="<?php echo $school; ?>">
<input type="hidden" name="balance" value="<?php echo $balance;?>">

          <!-- Submit Button -->
          <div class="modal-footer">
            <button type="submit" name="sms" class="btn btn-success">SEND SMS</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div> 




<!-- Modal -->
<div class="modal fade" id="parent" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">SMS Notification</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="message_.php" method="POST">
          <p>
            <span style="color:red">Note:</span> If the network connection is interrupted during the transmission, do not refresh the page.
          </p>

          <!-- Select Session -->
          
          <select name="session" class="form-control" required>
            <option value="">Select Session</option>
            <?php
            $query = mysqli_query($conn, "SELECT * FROM session ORDER BY id DESC");
            while ($row = mysqli_fetch_array($query)) { ?>
              <option value="<?php echo $row['session']; ?>"><?php echo $row['session']; ?></option>
            <?php } ?>
          </select>

          <!-- Select Class -->
          
          <select name="class" class="form-control" required>
            <option value="">Select Class</option>
            <?php
            $query = mysqli_query($conn, "SELECT * FROM class ORDER BY id DESC");
            while ($row = mysqli_fetch_array($query)) { ?>
              <option value="<?php echo $row['class']; ?>"><?php echo $row['class']; ?></option>
            <?php } ?>
          </select>

          <!-- Select Year -->
         

          <!-- Select Term -->
          
          
<input type="hidden" name="school" value="<?php echo $school; ?>">
<input type="hidden" name="balance" value="<?php echo $balance;?>">
 <textarea class="form-control" name="message" placeholder="Message..." rows="5"></textarea>

          <!-- Submit Button -->
          <div class="modal-footer">
            <button type="submit" name="sms" class="btn btn-success">Send</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>