<?php include("conn.php");
$page='Manual Cart Management';?>
<?php  
 $connect = mysqli_connect("localhost", "root", "", "resto");  
 $query = "SELECT * FROM users";  
 $result = mysqli_query($connect, $query);  
 ?>  
 <!DOCTYPE html>  
 <html>  
 <?php include("common/head.php")?>
      
 <?php
$sql2="SELECT * FROM currency ";
  $result2 = mysqli_query($conn,$sql2)or die( mysqli_error($conn));
  $row2=mysqli_fetch_array($result2) ;
$currency=$row2['curency'];


 if (isset($_POST['Add_to_cart'])) {
     if (isset($_SESSION['cart'])) {
         $session_array_id=array_column($_SESSION['cart'],"id");
         if (!in_array($_GET['id'],$session_array_id)) {
          $session_array=array(
               'id'=>$_GET["id"],
               "image"=>$_POST['image'],
               "location"=>$_POST['location'],
               "name"=>$_POST['name'],
                "date"=>$_POST['dates'],
               "orderid"=>$_POST['orderid'],
               "discription"=>$_POST['discription'],
               "quantity"=>$_POST['quantity'],
                "price"=>$_POST['price']);
          $_SESSION['cart'][]=$session_array;
         }
     }
     else {
          $session_array=array(
               'id'=>$_GET["id"],
              "image"=>$_POST['image'],
              "location"=>$_POST['location'],
              "name"=>$_POST['name'],
               "date"=>$_POST['dates'],
              "orderid"=>$_POST['orderid'],
               "discription"=>$_POST['discription'],
               "quantity"=>$_POST['quantity'],
          "price"=>$_POST['price']);
          $_SESSION['cart'][]=$session_array;
     }
 }
 
 ?>
 
 <?php
if (isset($_GET['action'])) {
     if ($_GET['action']=='remove') {
          foreach($_SESSION['cart'] as  $key =>$value){
               if ($value['id']==$_GET['id']) {
                    unset($_SESSION['cart'][$key]);
                   
               }

          }
     }
}
if (isset($_GET['action'])) {
    if ($_GET['action']=='clear') {
unset($_SESSION['cart']);

    }}
?>

<?php 
if (!empty($_SESSION['cart'])) {

      $arr=count($_SESSION['cart']);
}
else {
    $arr="" ;
}

?>
      <body>  
     
<center>
<div id="myDIV" class="col-sm-12 invoice mt-4">
<a href="dashboard" class="btn m-2  btn-sm btn-success">Back To orders</a>
<a   href="cart.php?action=clear"  class="btn m-2 btn-sm btn-warning">Clear Order</a>
           <h5 class="invoice-title text-center">Complete Manual Order</h5>
           <hr class="bg-success">
           <h6 class="invoice-title text-center">Customer Details</h6>

<div class="row">
   

 <div class="col-sm-6">
    <h6 class="invoice-title text-center">Invoice</h6>
           <?php

$today= date("F j, Y ,h:ia");
$order_id='T'.date('ymdhi').mt_rand(10,100);
$yesterday = new DateTime('yesterday');
$yest=$yesterday->format('F j,Y');
$todays= date("F j, Y");

$total=0;
if (!empty($_SESSION['cart'])) {
     foreach($_SESSION['cart'] as$key => $value) {
       $arr=count($_SESSION['cart']);
          $total=   $total + ($value['price'] * $value['quantity']);
        $real_id= $value['orderid'];
$ord = sprintf('%03d',$real_id);
?>

<div class="  invoice-item   p-2">
<div class="remove"><a href="cart.php?action=remove&id=<?=$value['id']?>" class="btn  btn-outline-danger btn-block btn-sm"> <i class="fa fa-times" aria-hidden="true"></i></a></div>
           <div class="d-flex   ">
               <div class="img-invoice">
                   <img src="image/<?php echo $value['image'] ?>" alt="">
               </div>
               <div class="text-invoice">
                  <span class="text-white"><b><?php echo $value['name']?></b></span><br>
                  <span class="text-white"><?php echo $value['quantity']?>x</span> <br>
                  <span class="text-white"><?php echo $value['discription']?></span> <br>
                  <div class="menu-tag ">
                   <div class="row no-gutters">
                       <div class="invoice-price col-6">
                           <b> <b><?php echo $currency ?><?php echo number_format($value['price'],2)*$value['quantity']?></b></b>
                       </div>
                       

                   </div><br>
                   <input class="form-control " type="text" name="note" value="<?php echo $value['note']?>  "readonly>
                   
               </div>
               </div>
          

           </div>
           </div>
           
           <?php } }?>


    </div>

<div class="col-sm-6 ">
<form  method="post" action="order/order_bookin.php">

<div id="" class="form-group col-sm-6 mb-3">
        <label for="date">Select Date</label>
        <select id="inputState" name="date" class="form-control">
                        <option selected>Select Date</option>
                        <option value="<?php echo $todays ?>">Today</option>
                        <option value="<?php echo $yest ?>">Yesterday</option>
                       
                      </select>
               </div>

               <div class="form-group col-sm-6 mb-3">
               <label for="date">Select Time</label>
               <input type="time" name="time" id="" class="form-control" required>
               </div>
<div class="form-group col-sm-6 mb-3">
                 <label for="exampleInputEmail1">Full Name</label>
                 <input  id="mySelect" name="name" onchange="myFunction()" type="text"  class="form-control " id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Customer name">
               </div>
               <div class="form-group col-sm-6 mb-3">
                 <label for="exampleInputPassword1">Contact</label>
                 <input type="number"  name="number" class="form-control " placeholder="Contact Number" >
               </div>
<h5 class="invoice-title text-center "> Payment Method</h5>
           <div class="pament-button form-group text-center col-sm-6 mb-3">
              
           <select class=" form-control  " name="payment"  id="mySelect1" onchange="myFunction1()">
              <option value="nun">Select Payment Method</option>
              <option value="Cash">Cash</option>
              <option value="Momo">Momo</option>
         </select>
           </div>
            
             
               <div class="receipt   bg-light text-dark">
               <h5 class="invoice-title text-center">Receipt</h5>
                   <div class="reciept-title text-center">
                       <p>The Kamb  Restaurant 189,<br> Accra awoshi <br> 0540948579</p>
                   </div>

                   <div class="reciept-menu text-center">
                       <div class="reciept-items ">
                        <?php 
                      
                        ?>
                           <div class="receipt-list1 ml-1 "><p>Order :#<?php echo $ord ?></p> </div>
                           
                           <div class="receipt-list2  "> <p><?php echo date("F j, Y") ?>, <?php echo date("h:ia");?></p></div>
                           </div>
                           <div class="reciept-items ">
                            <div class="receipt-list1 ml-1 "><p id="demo"></p> </div>
                           
                            <input type="hidden" name="today" value="<?php  echo date("F j, Y ")?>">
                            <input type="hidden" name="time" value="<?php  echo date("h:ia" )?>">
                            
                            </div>
                            <?php

$order_id='T'.date('ymdhi').mt_rand(10,100);
$real_id=substr($order_id,-2);
$ord = sprintf('%03d',$real_id);                       
$total=0;
if (!empty($_SESSION['cart'])) {
     foreach($_SESSION['cart'] as$key => $value) {
       $arr=count($_SESSION['cart']);
          $total=   $total + ($value['price'] * $value['quantity']);
echo $value['note'];

          $sql7="SELECT  SUM(percent) FROM tax  WHERE status='1'";
          $result7 = mysqli_query($conn,$sql7)or die( mysqli_error($conn));
          $row7=mysqli_fetch_array($result7) ;
          
              $total_tax= $row7['SUM(percent)']/100+1;

     $pay_item=0;
     $pay_item=$pay_item+ ($value['price'] * $value['quantity']*$total_tax)

?>
                            <div class="reciept-items mt-2 ">
                                <div class="receipt-list1 ml-1 "><p><?php echo $value['name'] ." ". $value['quantity']?>X</p> </div>
                                <div class="receipt-list2  "> <p ><?php echo $currency ?><?php echo number_format($value['price'],2)?></p></div>
                            </div>  
                            
                            <input type="hidden" name="food_name[]" value="<?php echo $value['name']?>">
                            <input type="hidden" name="quantity[]" value="<?php echo $value['quantity']?>">
                            <input type="hidden" name="item_total[]" value="<?php echo $pay_item?>">
                            <input type="hidden" name="price[]" value="<?php echo $value['price']?>">
                            <input type="hidden" name="item_id[]" value="<?php echo $value['id']?>">
                            <input type="hidden" name="location[]" value="<?php echo $value['location']?>">
                            <input type="hidden" name="dates[]" value="<?php echo $value['date']?>">
                            <input type="hidden" name="orderid[]" value="<?php echo $order_id?>">
                            <input  type="hidden" name="note[]" value="<?php echo $value['note']?>  ">
                            

                            <?php } } ?>         
                   </div>
                
                   <div class="total">
                       <p >Subtotal : <?php echo $currency ?><?php echo number_format($total) ?></p>
                       <input type="hidden" name="subtotal" value="<?php echo $total?>">
                       <input type="hidden" name="by" value="<?php echo $id_user?>">
                       <?php

include("conn.php");

$sql="select * from tax where status='1'";
$result = mysqli_query($conn,$sql)or die( mysqli_error($conn));
while ($row=mysqli_fetch_array($result)) {
$data=$row['id'];
$tax=$row['tax'];
$percent=$row['percent']; 

$sql7="SELECT  sum(percent) FROM tax  WHERE status='1'";
  $result7 = mysqli_query($conn,$sql7)or die( mysqli_error($conn));
  $row7=mysqli_fetch_array($result7) ;
  
      $total_tax= $row7[0];

$total_tax_payable=$total*$total_tax/100;
$amount_payable=$total+$total_tax_payable;




  ?>
                    
                       <p><?php  echo $tax ." ". $percent?>%</p>
                       

                       <?php }?>

                       <p>Discount:</p>
                       <p class=" bg-danger text-white text-center p-2">Total : <?php echo $currency ?><?php echo number_format($amount_payable) ?> </p>
                       <input type="hidden" name="total" value="<?php echo $amount_payable?>">
                       <input type="hidden" name="order_id" value="<?php echo $order_id?>">
                       <input type="hidden" name="order_type" value="1">
                       <span id="demo1"></span>
                   </div>
                   <div class="reciept-title text-center">
                       <p style="font-size: 12px;">Customer Copy<br> Thank you for Visiting <br>The Kamb  Restaurant</p>
                   </div>
                       
               </div>

               <button  name="submit" style="display:none;" id="payBtn"  class="btn btn-success btn-block m-2"> Confirm Payment</button>
               
           </div>
           

          </div>
          </form>

</div>  

</div>


        
       

         
</center>
    


          
       <!--  isotope plugin cdn  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/3.0.6/isotope.pkgd.min.js" integrity="sha256-CBrpuqrMhXwcLLUd5tvQ4euBHCdh7wGlDfNz8vbu/iI=" crossorigin="anonymous"></script>
    <script src="js/script.js"></script>
     <!-- jQuery js-->
 
     <!-- Bootstrap js -->
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
     <!-- jQuery Datatable js -->
     <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
     <!-- Bootstrap Datatable js -->    
     <script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>   
     
     <script>
function myFunction() {
  var x = document.getElementById("mySelect").value;
  document.getElementById("demo").innerHTML = "Bill to: " + x;
}
</script>
<script>
function myFunction1() {
  var x = document.getElementById("mySelect1").value;
  document.getElementById("demo1").innerHTML = "Payment Method: " + x;
  if (x === "Momo"||x === "Cash") { 

    document.getElementById("payBtn").style.display = "block";
  }
}
</script>
 <script>  
 $(document).ready(function(){  
      $('.view_data').click(function(){  
           var employee_id = $(this).attr("id");  
           $.ajax({  
                url:"user_status.php",  
                method:"post",  
                data:{employee_id:employee_id},  
                success:function(data){  
                     $('#employee_detail').html(data);  
                     $('#dataModal').modal("show");  
                }  
           });  
      });  
 });  
 </script>

</body>
