<?php
$page='Cart ';
$tittle='Cart';
include('conn.php')
?>


<?php include('common/head_online.php'); ?>

    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDTwYuEXjEjXd-dv6h70kCotbQROsJGSL4&callback=initMap&libraries=places&v=weekly" defer></script>
<?php 
$id_user='';
$fname='';
$address='';
$number='';
   if (isset($_SESSION['customers_id'])) {
    $id_user=$_SESSION['customers_id'];
    $sql = "SELECT * FROM customers WHERE customer_id='$id_user' ";
    $result=mysqli_query($conn,$sql);
        $row=mysqli_fetch_array($result);
        $fname=$row['fname'];
        $cus_id=$row['customer_id'];
        $number=$row['number'];
        $address=$row['location'];
        $log='Logout';
        $bg='btn-success';
    $link='online_logout';
    $pro_link='online_profile';

   }
   else{
    $log='Login';
    $bg='btn-primary';
    $link='online_login';
    $pro_link='online_login';
   }
   
?>
<style>

  .register{
display: none;
  }
</style>

<?php

include("conn.php");

if (isset($_POST['login'])) {
   $number=$_POST["number"];
   $pass=$_POST["password"];

//to prevent php injection

$number=stripcslashes($number);
$pass=stripcslashes($pass);


$sql01 = "SELECT * FROM customers WHERE number='$number' AND password ='$pass'";

$result01=mysqli_query($conn,$sql01);

   $row01=mysqli_fetch_array($result01);

   if($row01["number"]==$number && $row01['password'] == $pass )
   {   


 
     $_SESSION['fname']=$row01["fname"];
        $_SESSION['number']=$row01["number"];
        $_SESSION['customers_id']=$row01["customer_id"];

        echo"<script>alert('Login Successful'); window.location.href='online_carts'</script>";
       
       }
       
       else{
        echo"<script>alert('Your number or password is incorrect!'); </script>";
       }
    
   } 

 
   if (isset($_POST['register'])) {
    $fname=$_POST["fname"];
    $number=$_POST["number"];
    $address=$_POST["address"];
    $date=$_POST["date"];
    $pass=$_POST["password"];
    $Cpass=$_POST["Cpassword"];
    $order_type=1;
 
 //to prevent php injection
 
 
 
 
    if($pass==$Cpass )
    {   
      $sql01 = "SELECT * FROM customers WHERE number='$number' AND password =''";

      $result01=mysqli_query($conn,$sql01);
      
         $row01=mysqli_fetch_array($result01);
      
         if($row01["number"]==$number && $row01['password'] =='' )
         {   
          $sql03 = "UPDATE `customers` SET `password`='1540948579' WHERE number='$number'";
          if(mysqli_query($conn,$sql03)){
      
          
          }
          else{
            echo 'Error: '.mysqli_error($conn);
          }
        }
          $sql = "INSERT INTO `customers`(`fname`, `number`, `password`, `date`, `location`, `orderlocation`) VALUES ('$fname','$number','$pass','$date','$address','$order_type')";
      if(mysqli_query($conn,$sql)){
  
        echo"<script>alert('Registration was successful. You can now login '); window.location.href='online_carts'</script>";
      }
      else{
        echo 'Error: '.mysqli_error($conn);
      }
         
        
   
    
 
       
        
        }
        
        else{
         echo"<script>alert('Your password mix march'); </script>";
        }
     
    } 


?>





<body>
 

    <!-- Large modal -->
  <div id="dataModal1" class="modal fade bd-example-modal-sm" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class=" modal-dialog ">
      
    <div  id="employee_detail1" class="modal-content text-dark">
      <!--login form--->
      <div id="login" class="login  my-2">
     <h2 class="text-center">Login</h2>
              <form method="post">
              <div class=" row no-gutters">
    
                 
    <div class="col-sm-12 my-2">
    <label class="m-1"for="number">Phone number:</label>
                <div class="d-flex col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-telephone"></i></span>
                    <input  type="number" class="form-control "aria-label="fname" placeholder="Enter Phone number" name="number" aria-describedby="addon-wrapping">
                  </div>
    </div>

    <div class="col-12 my-2">
    <label class="m-1"for="number">Password:</label>
                <div class="d-flex col-12 mb-1">
                <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-key"></i></span>
                    <input  type="password" class="form-control "placeholder=" Enter password" required aria-label="fname" name="password" aria-describedby="addon-wrapping">
                  </div>
    </div>
 
    <div class="col-12 text-center my-2">
    <button type="submit"  name="login" class="btn btn-secondary m-1 ">Login</button>
    </div>
    <p class="text-center m-4"> have account? <a  onclick="myFunction()"class="text-primary   ">Register</a></p>

                  
    </div>
    </form>
  </div>
  
           
              
      
     <!--register form--->
     <div id="register" class="register  my-2">
     <h2 class="text-center">Register</h2>
              <form method="post">
              <div class=" row no-gutters">
    <div class="col-6">
    <label class="m-1"for="number">Full Name:</label>
                <div class="d-flex col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-person text-success"></i></span>
                    <input  type="text" class="form-control "placeholder="User name" aria-label="fname" name="fname" aria-describedby="addon-wrapping" required>
                  </div>
    </div>
 
                  <div class="col-6">
                  <label class="m-1"for="number">Contact:</label>
                <div class="d-flex flex-nowrap col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-telephone"></i></span>
                    <input type="text" class="form-control "placeholder="Phone number" aria-label="Username"   name="number" aria-describedby="addon-wrapping" required>

                     
                    <input type="hidden" name="date" value="<?php echo date("F j, Y") ?>">
                  </div>
                  </div>
                  
                 
    <div class="col-sm-12">
    <label class="m-1"for="address">Address:</label>
                <div class="d-flex col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-geo-alt"></i></span>
                    <input  type="text" class="form-control " placeholder="Describe location" aria-label="location" name="address" aria-describedby="addon-wrapping">
                  </div>
    </div>

    <div class="col-6">
    <label class="m-1"for="number">Password:</label>
                <div class="input-group col-12 mb-1">
                   
                    <input  type="password" class="form-control "placeholder="Set password" aria-label="fname" name="password" aria-describedby="addon-wrapping" required>
                  </div>
    </div>
 
                  <div class="col-6">
                  <label class="m-1"for="number"> Confirm Password:</label>
                <div class="input-group flex-nowrap col-12 mb-1">
                  
                    <input type="text" class="form-control "placeholder="Confirm password" aria-label="Username"   name="Cpassword" aria-describedby="addon-wrapping">
                  </div>
                  </div>

                  <div class="col-12 text-center mt-2">
                  <button type="submit"  name="register" class="btn btn-primary m-1 ">Submit</button>
                  </div>
               
                  <p class="text-center m-4"> have account? <a  onclick="myFunction()"class="text-primary   ">Login</a></p>

             
  </div>
              </form>
             
                </div>
                <!--register form end-->            

                </div>
                <!--login form end-->



</div>
              
              
    </div>
  </div>
</div>
    <!---ads 1--->






<!-- Modal Add users -->
<div  class="" >
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header ">
      </div>

      <div class="">

    
  <h4 class="text-center">Cart</h4>
 
  <a   href="online_order?action=clear"  class="btn  btn-sm btn-warning">Clear Order</a>
  <a   href="online_order"  class="btn  btn-sm btn-success">Continue Order</a>
  <form class=" " method="post" action="submit_order.php">
  <?php
  $month = date("F");

  $order_id='T'.date('ymdhi').mt_rand(10,100);
  $real_id=substr($order_id,-2);
  $ord = sprintf('%03d',$real_id);  
   $sql2="SELECT * FROM currency ";
  $result2 = mysqli_query($conn,$sql2)or die( mysqli_error($conn));
  $row2=mysqli_fetch_array($result2) ;
$currency=$row2['curency'];
$total=0;
$discount=0;
$total_price=0;
if (!empty($_SESSION['cart'])) {
     foreach($_SESSION['cart'] as$key => $value) {
       $arr=count($_SESSION['cart']);
       $total_new_item_price=    ($value['new_price'] * $value['quantity']);
       $total_old_item_price=   ($value['price'] * $value['quantity']);
          $total=   $total + ($value['new_price'] * $value['quantity']);
          $total_price=  $total_price + ($value['price'] * $value['quantity']);
          $discount=$total_price-$total;
        $real_id= $value['orderid'];
$ord = sprintf('%03d',$real_id);
?>
  <div style="background-color:#DDD8DCFF; border-radius:20px;" class="  m-2 ">
  <div class="row no-gutters rounded-3 p-2 m-1 border-0 ">
                    <div class="col-4 p-0  col-sm-2">
                        <div class="blog-img position-relative overflow-hidden">
                            <img style="width: 70px;" class="img-cart " src="image/<?php echo $value['image'] ?>" alt="">
                           </div>
                    </div>
                    <div class="col-6  col-sm-8">
                        <p style="font-size: 15px;" class="tittle" class="mb-1"><?php echo $value['name']?>   </p>
                <h3 class="my-2">
                    <small  id="" class="align-top " style="font-size:15px; line-height: 1px; "><b><?php echo $currency?><?php echo number_format($value['price'])?></b> <span class="" style="font-size:small;">x</span><?php echo $value['quantity']?></small>
                </h3>

                <p style="font-size: 12px;" ><u><?php echo $value['note']?></u></p>
                <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 $total_new_item_price?>">
                            <input type="hidden" name="price[]" value="<?php echo $value['price']?>">
                            <input type="hidden" name="new_price[]" value="<?php echo $value['new_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="month[]" value="<?php echo  date("F");?>">
                            <input type="hidden" name="orderid[]" value="<?php echo $order_id?>">
                            <input  type="hidden" name="note[]" value="<?php echo $value['note']?>  ">


                    </div>
                    <div class="col-2 col-sm-2 text-center">
                        <a class="p-2"  style="font-size: 30px;" href="online_order?action=remove&id=<?=$value['id']?>"><i class="bi bi-trash text-danger   "></i></a>
                      <!--  <a class="p-2"  style="font-size: 30px;" href="edit_cart.php?action=remove&id=<?=$value['id']?>"><i class="bi bi-pencil text-success   "></i></a> --->
                    </div>
                </div>
  </div>
  <?php }}?>


  
    <div  style=" border-radius:20px;"  class="customer-details  ">
    <h6 class="text-center">Delivery Details</h6>
  <div class=" row no-gutters">
    <div class="col-6">
    <label class="m-1"for="number">Customer Name:</label>
                <div class="input-group col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-person text-success"></i></span>
                    <input  type="text" class="form-control "value="<?php echo $fname?>" aria-label="fname" name="fname" aria-describedby="addon-wrapping" disabled><input type="hidden" name="fname" value="<?php echo $fname?>">
                  </div>
    </div>
 
                  <div class="col-6">
                  <label class="m-1"for="number">Delivery Contact:</label>
                <div class="input-group flex-nowrap col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-telephone"></i></span>
                    <input type="text" class="form-control "value="<?php echo $number?>" aria-label="Username"   name="receiver_contact" aria-describedby="addon-wrapping">
                    <input type="hidden" name="contact" value="<?php echo $number?>">
                  </div>
                  </div>
                  
                 
    <div class="col-sm-12">
    <label class="m-1"for="number">Delivery Address:</label>
                <div class="d-flex col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi bi-geo-alt"></i></span>
                    <input id="location-input"  type="text" class="form-control "value="<?php echo $address?>" aria-label="fname" name="address" aria-describedby="addon-wrapping">
                    <input type="hidden" name="table" value="0">
                  </div>
    </div>

                  <div class="col-12">
                  <label class="m-1"for="number">Payment Method:</label>
                  <div class="d-flex flex-nowrap col-12 mb-1">
                    <span class="input-group-text  text-success" id="addon-wrapping"><i class="bi  bi-cash"></i></span>
                     <select class="form-control" name="payment">
                       <option value="nun">payment Method</option>
                       <option value="0">Cash on delivery</option>
                       <option value="1">Momo</option>
                      
                     </select>
                     </div>
                  </div>
               
  </div>
    </div>
  <div style="background-color: white; border-radius:20px;" class="p-2 text-dark border m-1 ">
                <p class="my-1">Items Total:  <span style="float: right;font-size:20px; line-height: 2px;"><b><?php echo $currency?><?php echo number_format($total)?></b></span></p>
                <p class="my-1">Discount:  <span style="float: right;font-size:20px; line-height: 2px;"><b><?php echo $currency?><?php echo number_format($discount)?></b></span></p>
                <p class="my-1">Subtotal:  <span style="float: right;font-size:20px; line-height: 2px;"><b><?php echo $currency?><?php echo number_format($total)?></b></span></p>
                <input type="hidden" name="total" value="<?php echo $total?>">
                        <input type="hidden" name="discount" value="<?php echo $discount?>">
                       <input type="hidden" name="order_id" value="<?php echo $order_id?>">
                       <input type="hidden" name="order_type" value="1">
                       <input type="hidden" name="by" value="<?php echo $id_user?>">
                       <input type="hidden" name="time" value="<?php echo date("G:i")?>">
            </div>
            <button  name="submit" style="background-color:#982BAD;" id="payBtn"  class="btn  btn-block  text-white my-2">Submit Order</button>
            </form>

      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>
   <script>

function initMap() {
    const input = document.getElementById("location-input");
    var options = {
            
            componentRestrictions: {country: "GH"}
        };
    const autocomplete = new google.maps.places.Autocomplete(input, options);
    
    autocomplete.addListener("place_changed", () => {
      
      const place = autocomplete.getPlace();
      
        if (!place.geometry) {
          // User entered the name of a Place that was not suggested and
          // pressed the Enter key, or the Place Details request failed.
          //window.alert("No details available for input: '" + place.name + "'");
          swal("Please fill all the *Required fields","Click okay to continue", "warning");
          return;
      }

      
    });
}
    </script>


<script>
  function myFunction() {
  var x = document.getElementById("login");
  var y = document.getElementById("register");
  if (x.style.display === "none") {
    x.style.display = "block";
    y.style.display = "none";
  } else {
    x.style.display = "none";
    y.style.display = "block";
  }
}
</script>

     <script>
       $(document).ready(function(){  
    
          
           var employee_ids = $(this).attr("id");
           var user = <?php echo $login_status?>;
              if (user === 0) {

    setTimeout(function() {
     $('#dataModal1').modal("show");
     
    }, 100);
} 
 
 });  
</script>
</body>
