<?php   



session_start();

    include('functions/userfunctions.php');

    include('includes/header.php');

    include('authenticate.php');

$defaultEmail = $_SESSION['auth_user']['email'];
$defaultPhone = $_SESSION['auth_user']['phone'];
$defaultName = $_SESSION['auth_user']['name'];

?>


    <div class="py-3 bg-primary">

      <div class="container">

        <h6 clasS="text-white">

            <a href="index.php">

                 Home / 

            </a>

            <a href="checkout.php">

                 Checkout

            </a>

        </h6>

      </div>

    </div>



    <div class="container py-5">

        <div class="card">

            <div class="card-body shadow">

                <form id="orderForm" method="POST">

                    <div class="row">

                        <div class="col-md-7">

                            <h5>Details</h5>

                            <hr>

                            <div class="row">

                                <div class="col-md-6 mb-3">
                                    <?php $backupuser_id = $_SESSION['auth_user']['user_id']; ?>
                                    <input type="hidden" name="suid" value="<?= $backupuser_id;?>">

                                    <label class="fw-bold">Name</label>

                                    <input type="text" name="name" required placeholder="Enter your full name" class="form-control" value="<?= $defaultName;?>">

                                </div>

                                <div class="col-md-6 mb-3">

                                    <label class="fw-bold">E-mail</label>

                                    <input type="email" name="email" required placeholder="Enter your email" class="form-control" value="<?= $defaultEmail;?>">

                                </div>

                                <div class="col-md-6 mb-3">

                                    <label class="fw-bold">Phone</label>

                                    <input type="text" name="phone" required placeholder="Enter your phone number" class="form-control" value="<?= $defaultPhone;?>">

                                </div>

                                <div class="col-md-6 mb-3">

                                    <label class="fw-bold">Room / Restaurant Table No</label>

                                    <input type="text" name="pincode" required placeholder="Enter room/table number" class="form-control">

                                </div>

                                <div class="col-md-12 mb-3">

                                    <label class="fw-bold">Note (Please Dont Leave It Empty)</label>

                                    <textarea name="address" required class="form-control"rows="5"></textarea>

                                </div>

                            </div>
                            

                        </div>



                        <div class="col-md-5">

                            <div class="row align-items-center d-none d-md-flex">

                                <div class="col-md-7">

                                    <h6>Order Details</h6>

                                </div>

                                <div class="col-md-2">

                                    <h6>Quantity</h6>

                                </div>

                                <div class="col-md-3">

                                    <h6>Unit Price</h6>

                                </div>

                            </div>

                                <?php $items = getCartItems();

                                $totalPrice = 0;

                                foreach($items as $citem)

                                {

                                    ?>

                                    <div class="card product_data shadow-sm mb-3">

                                        <div class="row align-items-center">

                                        <!--görsel ve isim

                                            <div class="col-md-2">

                                                <img src="uploads/<?=$citem['image']?>" alt="Menu Item" width="80px" class="p-2">

                                            </div>

                                            <div class="col-md-5">

                                                <h5 class="p-2"><?=$citem['name']?></h5>

                                            </div>

                                        -->

                                            <div class="col-12 col-md-7">

                                                <h5 class="p-2"><?=$citem['name']?></h5>

                                            </div>

                                            <div class="col-6 col-md-2">

                                                <h5 class="p-2"> <?=$citem['prod_qty']?> x</h5>

                                            </div>

                                            <div class="col-6 col-md-3">

                                                <h5 class="p-2"><?=$citem['selling_price']?> ₺</h5>

                                            </div>

                                            

                                            

                                        </div>

                                    </div>

                                    <?php

                                    $totalPrice += $citem['selling_price'] * $citem['prod_qty'];

                                }

                                ?>

                                <hr>

                                

                                <h5 class="mt-3">Total Price : <span class="float-end fw-bold"><?= $totalPrice ?> ₺</span></h5>

                                <div class="paymentmethod mt-5">
                                    <h6>Payment Method</h6>
                                    <label>
                                        <input type="radio" name="payment_method" value="credit_card" checked>
                                        <span>Credit Card</span>
                                    </label>
                                    <br>
                                    <label>
                                        <input type="radio" name="payment_method" value="TOM">
                                        <span>TOM System</span>
                                    </label>
                                </div>

                                <div class="">

                                    <input type="hidden" name="payment_mode" id="paymentMode" value="credit_card">
                                    <input type="hidden" name="totalprice" value="<?= $totalPrice ?>">

                                    <!--<button type="submit"  name="placeOrderBtn2" class="btn btn-primary float-end mt-5 w-100">Confirm and place order | COD </button>-->
                                    <?php $current_hour = date('H');
                                    if ($current_hour >= 5 && $current_hour < 20) {
                                        ?>
                                            <button type="button" onclick="submitForm2()" class="btn btn-primary float-end mt-5 w-100">Confirm and place order</button>
                                        <?php
                                    } else {
                                        ?>
                                        <button type="button" class="btn btn-primary float-end mt-5 w-100">Our kitchen is open between 08:00 - 23:00</button>
                                        <?php
                                    }
                                    ?>
                                   
                                </div>

                        </div>

                    </div>

                </form>

            </div>

        </div>

    </div>

    

<script>
    function submitForm2() {
        var form = document.getElementById('orderForm');
        var selectedPaymentMethod = document.querySelector('input[name="payment_method"]:checked').value;
        
        // Gizli inputtan toplam tutarı al ve sayısal (float) değere çevir
        var totalPrice = parseFloat(document.querySelector('input[name="totalprice"]').value);

        // Tutar 0 veya daha küçükse işlemi durdur
        if (totalPrice <= 0) {
            alert("The shopping cart total is 0 TL, so you cannot proceed to the payment step. Please add items to your cart before proceeding.");
            return; // Fonksiyondan çık, formu submit etme!
        }

        // Update payment_mode based on selected payment method
        if (selectedPaymentMethod === 'credit_card') {
            document.getElementById('paymentMode').value = 'credit_card';
            form.action = 'payment.php'; // Set action to payment.php for credit card
        } else if (selectedPaymentMethod === 'TOM') {
            document.getElementById('paymentMode').value = 'TOM';
            form.action = 'tompayment.php'; // Set action to tompayment.php for TOM System
        }

        form.submit(); // Submit the form with updated action URL and payment_mode value
    }
</script>




 <?php //include('sanalpost_php/threed-payment.php');?> 

    <?php include('includes/footer.php');?>