<?php 
  if (!isset($_SESSION['username'])) {
    $_SESSION['msg'] = "To access this page, Login is required";
    header('location: login.php');
  }  ?>
<!DOCTYPE>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* { box-sizing: border-box; }
body { margin: 0; }

.top-aa{ padding:15px; text-align: left; width: 100%; font-size: x-small; color: #848482; margin-bottom: 1%;}

.col-loan-a {  float: left;  width: 30%;  padding: 15px; }
.col-loan-b {  float: left;  width: 70%;  padding: 15px; }
.loan-a { float: left; width: 100%; background: white; padding: 15px; border-bottom: 2px solid #7CB9E8; }

.loan-a a {float: right; color:#7CB9E8 ; font-weight: bold;}
.loan-a p { padding: 10px; }

td {  padding:8px;border-bottom: 1px solid #ddd !important; font-size: xx-small; }

th {  padding:10px;border: 1px solid #ddd !important; background:#7CB9E8; color: white; opacity: .8;text-align: left;  font-size: x-small; border-spacing: 1;}

table { margin: auto; border-spacing: 1; width: 100%; float: left;  }

.no-results{float: left; margin-left: 5%; color: red; font-size: small;}

.loan-b { float: left; background: white; padding: 15px; width: 100%;  }
.table { float: left; margin-right: 3%; height: 80%; width: 100%; overflow-x: auto; scrollbar-width: thin; }

.btn-edit {background: white; border:  1px solid #7CB9E8 ;  color:#7CB9E8 ; padding: 5px;}
.btn-edit a {  color:#7CB9E8 ; text-decoration: none; font-size: x-small;}

.receipt {padding: auto; border: none; background: white; color: skyblue; }
.btn-edit:hover{background:#dde; border:  1px solid none; opacity: .6}


.btn-pending { background: orange; color: white; padding: 6px; border: none; border-radius: 3px; }

.btn-received{ background: #1cc88a; color: white; padding: 6px; border: none; border-radius: 3px; }


/* Clear floats after the col-loans */
.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width:600px) {
  .col-loan-b, .col-loan-a {  width: 100%;  padding:0px  }
  .col-loan-a { margin-bottom:5%; }
   .loan-a p { padding: 5px;  font-size: x-small;}

   }

</style>
</head>
<body>


<div class="row">

<div class="top-aa">
<a href="index.php" style="color: black; text-decoration: none; font-weight: bold;">Home </a>
<a>// Manage web orders</a>
</div>

  <div class="col-loan-a">
    <nav class="loan-a">
    <h4>Web Orders</h4>
    <hr style="opacity: .1;">
     <p> Approved orders <a>
        <?php $sql = "SELECT * FROM orders  where released = 1";     
$result = mysqli_query($conn,$sql); $rowcount=mysqli_num_rows($result);
  printf(" %d \n ",$rowcount);?>
     </a> </p>
     <hr style="opacity: .1;">
     <p> Pending orders    <a>
       <?php $sql = "SELECT * FROM orders  where released = 0";     
$result = mysqli_query($conn,$sql); $rowcount=mysqli_num_rows($result);
  printf(" %d \n ",$rowcount);?></a></p>

      <hr style="opacity: .1;">


     </nav>
  </div>
  
  <div class="col-loan-b">
<nav class="loan-b">

 <?php include 'order-search.php'; ?>


 <div class='table'>
<?php  require_once "inc/config.php";
 $sql = "SELECT * FROM orders WHERE released = '0' order by time DESC";
   if($result = $mysqli->query($sql)){
   if($result->num_rows > 0){
   echo "<table >";
   echo "<thead>";
   echo "<tr class='t1'>";
   echo "<th>Customer Name</th>";
   echo "<th>Phone no</th>";
   echo "<th>Residence</th>";
   echo "<th>Delivery Point</th>";
  echo "<th>Amount</th>";
   echo "<th>Payment mode</th>";
   echo "<th>Status</th>";
   echo "<th>Receipt No</th>";
      echo "<th>Time</th>";
     echo "</tr>";
   echo "</thead>";
   echo "<tbody>";
    while($row = $result->fetch_array()){
   echo "<tr>";
  echo "<td>" . $row['customer'] . "</td>";
   echo "<td>" . $row['phone'] . "</td>";   
   echo "<td>" . $row['residence']. "</td>";
   echo "<td>" . $row['delivery_point']. "</td>";
   echo "<td>" . $row['amount'] . "</td>";
   echo "<td>" . $row['mode'] . "</td>";

?>
<td>
<?php 
$s = $row['status'];  

if ($s == 0) {  echo "<button class='btn-pending'> Pending  </button>";}

 else {echo "<button class='btn-received'> Opened  </button>"; }



?> 

</td>

<?php 
echo
  "<td>
<form method ='post'> 
<input type='hidden' name='receipt_no' value=".$row['receipt_no']." />
<button class='receipt' type='submit' name='submit'  ><u> " . $row['receipt_no'] . " </u></button>
</form>
  </td>";

  // echo "<td>" . $row['session'] . "</td>";
  echo "<td>" . date_format( new DateTime ($row['time'] ),"d M, Y H: m: s"). "</td>";



 echo "</tr>";
         }
    echo "</tbody>";                            
    echo "</table>";
   // Free result set
       $result->free();
            } else{ echo "<p class='no-results'><em>Opps! no records.</em></p>";}
  
   } else{ echo "ERROR: Could not able to execute $sql. " . $mysqli->error;}
   // Close connection
       $mysqli->close();?>
</div>
</nav>
</div>

</div>

</body>
</html>

<script>
function opensales() {   document.getElementById("customerItems").style.display = "block"; }
function closesales() {   document.getElementById("customerItems").style.display = "none";  }

function showformHide() {
        let paymentmode = document.getElementById('payment')
        if (paymentmode.value == 1) {
                    document.getElementById('hidden-panel-c').style.display = 'none'
                     document.getElementById('hidden-panel-b').style.display = 'none'
        }
         else if (paymentmode.value == 2) {
             document.getElementById('hidden-panel-c').style.display = 'none'
             document.getElementById('hidden-panel-b').style.display = 'none'

        }

        else if (paymentmode.value == 3) {
             document.getElementById('hidden-panel-c').style.display = 'block'
             document.getElementById('hidden-panel-b').style.display = 'none'
        }

        else if (paymentmode.value == 4) {
             document.getElementById('hidden-panel-b').style.display = 'block'
              document.getElementById('hidden-panel-c').style.display = 'none'
        }

           else if (paymentmode.value == 5) {
             document.getElementById('hidden-panel-b').style.display = 'block'
              document.getElementById('hidden-panel-c').style.display = 'none'
        }

           else if (paymentmode.value == 6) {
             document.getElementById('hidden-panel-c').style.display = 'none'
              document.getElementById('hidden-panel-b').style.display = 'none'
        }

         else {
            document.getElementById(('hidden-panel-c') || ('hidden-panel-b')).style.display = 'none'
        }
    }

</script>

<style type="text/css">
  
  body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}

.bottom-total-a { float: left; width: 50%; padding: 15px; font-size: x-small; }
.bottom-total-b { float: left; width: 50%; padding: 15px; font-size: x-small; }

.bottom-total-b h4 {text-align: right;}
.form-popup {  display: none; margin-top: 2%;  z-index: 9; }

.c1 {float: left; width: 33.33%; padding: 10px;}
.c2 {float: left; width: 33.33%; padding: 10px;}

/* Add styles to the loan container */
.loan-cont {width:65%; margin: auto; padding: 15px;  background-color: white;  box-shadow: 2px 3px 3px 2px gray; position:absolute; top: 0; right: 10; margin-right: 3%; margin-top: 0%; margin-bottom: 2%;   }

.fa-close { float: right; color: red; margin-top:0%; font-size: large; }

.table { margin-right: 3%;  overflow-x: auto;scrollbar-width: thin; }


.form { float: left; width: 100%; padding: 1px; }
.form label { font-size: x-small; margin: 2%; }

.form input[type=text] { border-radius: 4px;  width: 100%;  background:whitesmoke; border:  1px solid  #f1f1f1; padding: 10px; font-size: x-small; margin-top: 1%; color: orange; font-weight: bold;}
.form  select { border-radius: 4px;  width: 100%;  background:whitesmoke; border:  1px solid  #f1f1f1; padding: 10px; font-size: x-small; margin-top: 1%;  }

/* Set a style for the submit/login button */
.form .btn {  background-color:skyblue;  color: white;  padding: 16px 20px;  border: none;
  cursor: pointer;  width: 100%;  margin-bottom:10px;  opacity: 0.8; }

/* Add some hover effects to buttons */
.form .btn:hover, .open-button:hover {  opacity: 1;}

 #hidden-panel-a , #hidden-panel-b, #hidden-panel-c { display: none; }



@media screen and (max-width:600px) {
  .loan-cont { width: 95%; height: auto;  margin-right: 0%; margin-top: 20%;  }
  .bottom-total-b, .bottom-total-a { width: 50%; float: left; } 
  .c1 , .c2 {float: left; width: 100%;}
  }

 </style>

<div class="form-popup" id="customerItems">
  <nav class="loan-cont">
<h4> <i class="fa fa-user" style="margin-right: 5%; margin-left: 1%;"></i>Ordered Items  <a onclick="closesales()"><i class="fa fa-close"></i> </a> </h4>

<?php  
if (isset($_POST["submit"])) {
$receipt_no = $_POST['receipt_no' ];

$sql_v = "UPDATE orders SET status = '1' WHERE receipt_no = '$receipt_no'";
$db->query($sql_v) ; 



$sql = "SELECT * FROM ordered_items where receipt_no = '$receipt_no'  order by Id  DESC";   
$result = mysqli_query($conn,$sql);  
echo "<script type= 'text/javascript'> opensales() </script>";

 ?>
<nav class="table">
<table >
 <tr>

  <th>Items</th>
  <th>Size</th>
  <th>Quantity</th>
  <th>Unit price</th>
  <th>Total Price</th>
  <th>Time ordered</th>
</tr>  

<?php while($row = mysqli_fetch_object($result)){ ?>  

<tr> 

<td><?php echo $row->name;?></td>
<td><?php echo $row->size;?></td>
<td><?php echo $row->quantity;?></td>
<td><?php echo $row->price;?></td>
<td><?php echo $row->total_price;?></td>
<td style="color:#848482; font-weight: bold;" ><?php echo date_format( new DateTime($row->time),"d,M, Y \t H:m:s");?></td>
 </tr>       
 <?php  }?>
 <?php  }?>
</table>
</nav>




<?php  $sql = "SELECT * FROM orders where receipt_no = '$receipt_no'  order by id  DESC";   
$result = mysqli_query($conn,$sql); $row = mysqli_fetch_object($result); ?>

<nav class="bottom-total-a">
   <p><b> Receipt No</b> - <?php echo $row->receipt_no;?>   </p>
  <h4>Total   - Ksh. <?php echo $row->amount;?> /=  </h4>
</nav>

<nav class="bottom-total-b">  

</nav>


<form action="" method="post" class="form">
  <h4> <i class="fa fa-user" style="margin-right: 5%; margin-left: 1%;"></i>Customer Billing   Details</h4>
  
  <input type="hidden" value= "<?php echo $row->receipt_no;?>" name="receipt_no" readonly>


  <nav class="c1">
    <label>Customer Name</label>
  <input type="text" placeholder="Enter Customer Name eg. Joskin Doe" value= "<?php echo $row->customer;?>" name="customer" readonly>
  </nav>


  <nav class="c2">
    <label>Phone</label>
  <input type="text" value="<?php echo $row->phone;?>" name="phone" readonly>
</nav>
  

  <nav class="c1">
    <label>Expected Pay</label>
  <input type="text"  value=" <?php echo $row->amount;?>" name="amount" readonly>
  </nav>

    <nav class="c1">
    <label>Residence</label>
  <input type="text"  value=" <?php echo $row->residence;?>" name="residence" readonly>
  </nav>

    <nav class="c1">
    <label>Delivery_point</label>
  <input type="text"  value=" <?php echo $row->delivery_point;?>" name="delivery_point" readonly>
  </nav>

<nav class="c1">
           <div id="drop-down" name="drop-down">
          <label>Payment Mode</label>
          <select name="payment" onclick="showformHide()" id="payment"   >
             <option value="1" > CASH </option>
             <option value="2" > MPESA </option>
             <option value="3" > BOTH (MPESA & CASH)</option>
             <option value="4" > PARTIAL PAYMENT (With Mpesa Deposit) </option>
             <option value="5" > PARTIAL PAYMENT (With Cash Deposit) </option>
             <option value="6" > ON LOAN </option>
          </select>
        </div>
 </nav>
 
 <nav class="c1">
  <div name="hidden-panel-c" id="hidden-panel-c">
           <label style="font-size:x-small;">Specify Cash Amount Received only </label>
          <input type="text" name="cash_amount"  style="background:whitesmoke; color: black; font-weight: lighter;" placeholder="3000" />
       </div>
     </nav>

  <nav class="c2">
       <div name="hidden-panel-b" id="hidden-panel-b">
           <label style="font-size:x-small;">Specify  Amount Received  </label>
          <input type="text" name="amount_a"  style="background:whitesmoke; color: black; font-weight: lighter" placeholder="2000" />
       </div>
  </nav>

       
   <button type="submit" name="release" class="btn">Release ordered Items </button>  
  </form>
</div>
</nav>
</div>



<?php include 'inc/config.php'; 
if (isset($_POST["release"])) {  $receipt_no = $_POST['receipt_no' ];
$username=$_SESSION['username'];  $customer= $_POST['customer'];  $phone= $_POST['phone']; $amount_a= $_POST['amount_a'];
$cash_amount= $_POST['cash_amount'];  $amount = $_POST['amount'];  $mode= $_POST['payment'];  

  if ($_POST['payment'] == 1)  { $cash = $amount; $balance = 0;    $mpesa =0; }
  else if ($_POST['payment'] == 2) {  $mpesa = $amount; $balance = 0;    $cash =0; }
  else if ($_POST['payment'] == 3) {  $cash = $cash_amount;  $mpesa = ($amount - $cash);  $balance = 0; }
  else if ($_POST['payment'] == 4) {  $mpesa = $amount_a;  $balance = ($amount - $mpesa);  $cash = 0; }
  else if ($_POST['payment'] == 5) {  $cash = $amount_a;  $balance = ($amount - $cash);  $mpesa = 0; }
  else if($_POST['payment'] == 6) { $cash = 0;  $balance = $amount;  $mpesa = 0; }

   if ($amount_a > $amount  )  { 
     echo "<script type= 'text/javascript'>alert('Sorry [ $username ] Deposit Exceeds Total pay required');</script>";
     echo "<script type='text/javascript'>location.href = 'index.php?link=101order';</script>";
      }
elseif ($cash_amount > $amount ) { 
    echo "<script type= 'text/javascript'>alert('Sorry [ $username ] Cash amount exceeds total pay required');</script>";
     echo "<script type='text/javascript'>location.href = 'index.php?link=101order';</script>";
       } 
else {
  $sql_1 = "INSERT INTO earnings ( customer, phone, amount, mode, cash, mpesa, balance,  receipt_no,   session ) 
            VALUES ('$customer', '$phone', '$amount', '$mode', '$cash', '$mpesa', '$balance', '$receipt_no', '$username')";


            $sql_2 = " UPDATE ordered_items SET released = '1' WHERE receipt_no = '$receipt_no'  ";

            $sql_3 = " UPDATE orders SET released = '1' WHERE receipt_no = '$receipt_no'  ";



            
             if ( ($db->query($sql_1) === TRUE) AND ($db->query($sql_2) === TRUE)  AND  ($db->query($sql_3) === TRUE)    ) {
 echo "<script type= 'text/javascript'>alert('order for [ $customer ] succesfully released.');</script>";
  echo "<script type='text/javascript'>location.href = 'index.php?link=101order';</script>";
         }

else{ echo "<script type= 'text/javascript'>alert('sorry  ! system is unable to release items ordered by [$customer].');</script>";  }


   }

   }
  



?>