<?php
include('includes/connectdb.php'); 

if(!isset($_SESSION['user_id'])){
	header('location:trusteepharmacy.com');
}else{
	$user_id = $_SESSION['user_id'];
	$sql = mysqli_query($con, "SELECT * FROM account WHERE account_id = $user_id ");
	$row = mysqli_fetch_array($sql);
	$s_address = $row['s_address'];
	$email = $row['email'];
	$first_name = $row['first_name'];
}

include('includes/head.php');

$total_price = 0;
?>
<body>
    <div class="page-wrapper">
        <?php include('includes/header.php') ?>

        <main class="main">
            <nav aria-label="breadcrumb" class="breadcrumb-nav">
                <div class="container">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.html"><i class="icon-home"></i></a></li>
                        <li class="breadcrumb-item active" aria-current="page">Checkout</li>
                    </ol>
                </div><!-- End .container -->
            </nav>

            <div class="container">
                <ul class="checkout-progress-bar">
                    <li>
                        <span>Shipping</span>
                    </li>
                    <li class="active">
                        <span>Review &amp; Payments</span>
                    </li>
                </ul>
                <div class="row">
                    <div class="col-lg-4">
                        <div class="order-summary">
                            <h3>Summary</h3>

                            <h4>
                                <a data-toggle="collapse" href="#order-cart-section" class="collapsed" role="button" aria-expanded="false" aria-controls="order-cart-section"><?php echo count($_SESSION['cart_item']) ?> products in Cart</a>
                            </h4>

                            <div class="collapse" id="order-cart-section">
                                <table class="table table-mini-cart">
                                    <tbody>
									    <?php foreach($_SESSION['cart_item'] as $key => $val):  
										    $resPrice = $_SESSION['cart_item'][$key]['price'] * $_SESSION['cart_item'][$key]['qty'];
										?>
                                        <tr>
                                            <td class="product-col">
                                                <figure class="product-image-container">
                                                    <a href="product?pid=<?php echo $_SESSION['cart_item'][$key]['pid']; ?>" class="product-image">
                                                        <img src="<?php echo $_SESSION['cart_item'][$key]['image'];  ?>" alt="product">
                                                    </a>
                                                </figure>
                                                <div>
                                                    <h2 class="product-title">
                                                        <a href="product?pid=<?php echo $_SESSION['cart_item'][$key]['pid']; ?>"><?php echo $_SESSION['cart_item'][$key]['name'];  ?></a>
                                                    </h2>
                                                    <span class="product-qty">Qty: <?php echo $_SESSION['cart_item'][$key]['qty'] ?></span>
                                                </div>
                                            </td>
                                            <td class="price-col"> $ <?php echo number_format($_SESSION['cart_item'][$key]['price'] * $_SESSION['cart_item'][$key]['qty']) ?></td>
                                        </tr>
                                        <?php endforeach  ?>
                                    </tbody>    
                                </table>
                            </div><!-- End #order-cart-section -->
                        </div><!-- End .order-summary -->

                        <div class="checkout-info-box">
                            <h3 class="step-title">Ship To:
                                <a href="update-s_address" title="Edit" class="step-title-edit"><span class="sr-only">Edit</span><i class="icon-pencil"></i></a>
                            </h3>

                            <address>
                                <?php echo $s_address; ?>
								<span style="float:right">$100</span>
                            </address>
							
							
                        </div><!-- End .checkout-info-box -->

                        <div class="checkout-info-box">
                            <h3 class="step-title">Total: 
                                <a href="#" title="Edit" class="step-title-edit"><span class="sr-only"></i></a>
                            </h3>

                            <p>Total
							     <span style="float:right">$<?php echo number_format($total_price + 1000 );  ?></span>
							     <span style="display:none" id="tprice"><?php echo $total_price + 1000 ;  ?></span>
							</p>
                        </div><!-- End .checkout-info-box -->
                    </div><!-- End .col-lg-4 -->

                    <div class="col-lg-8 order-lg-first">
					    <h2 class="step-title">Complete Order:</h2>
                        <div class="checkout-payment">
                            <div id="new-checkout-address" class="show">
							    <div>
								     View your order summary. Click your payment option to complete the ordering process.
								     <p>You will receive a confirmation email with an order id when your order has been completed. </p>
								</div>
							</div><!-- End #new-checkout-address -->

                            <div class="form-group required-field">
								<label>Add a message </label>
								<input type="text" name="message" id="message" class="form-control">
							</div><!-- End .form-group -->

                            <div class="clearfix">
                                <a id="paypal-button-container" class="float-left" style="color:white">Place Order</a>
                            </div><!-- End .clearfix -->
                        </div><!-- End .checkout-payment -->

                    </div><!-- End .col-lg-8 -->
                </div><!-- End .row -->
            </div><!-- End .container -->

            <div class="mb-6"></div><!-- margin -->
        </main><!-- End .main -->

        <?php include('includes/footer.php') ?>
    </div><!-- End .page-wrapper -->

    <div class="mobile-menu-overlay"></div><!-- End .mobil-menu-overlay -->

    <?php include('includes/mobile-menu.php') ?>

    <a id="scroll-top" href="#top" title="Top" role="button"><i class="icon-angle-up"></i></a>

    <!-- Plugins JS File -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/plugins.min.js"></script>

    <!-- Main JS File -->
    <script src="assets/js/main.min.js"></script>
	
	<!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=AYlAX3CA7dmhB4Yb5vF6UJfiT6rEW8MtCqrBW9_TE4AkHpV4vMFgQII5kgxS-RPHeUiWIWezx9WxmYpl&currency=USD"></script>

    <script>
	    const totalPrice = $('#tprice').html();
		var message = $('#message').val();
		// Render the PayPal button into #paypal-button-container
        paypal.Buttons({

            // Set up the transaction
            createOrder: function(data, actions){
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: String(totalPrice)
                        }
                    }]
                });
            },

            // Finalize the transaction
            onApprove: function(data, actions){
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
					$.ajax({
						url	:	"process.php",
						type:	"POST",
						data	:	{
								place_order:1, 
								name: details.payer.name.given_name,
								email: 'oyerindedavid@gmail.com',
								message: message,
								order_id:response.reference
							},
						success	:	function(data){ 
							window.location.href = "message?status=success&value=Your order has been successfully placed. We will be in touch soon."; 
						}
					});
                    console.log('Transaction completed by ' + details.payer.name.given_name + '! Email' + + details.payer.email);
                });
            }

        }).render('#paypal-button-container');
    </script>
</body>
</html>
