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

if(!isset($_SESSION['user_id'])){
	header('location:checkout-shipping');
}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'];
}

if(isset($_POST['submit'])){
	
	$s_address = $_POST['s_address'];
	
	$sql = mysqli_query($con, "UPDATE account SET s_address = '$s_address' WHERE account_id = $user_id ");

	if($sql == true){
		header('location:checkout-shipping-2');
	}else{
		$output = '<div class="alert alert-danger alert-intro" role="alert">
					Failed to update shipping address. Please try again.
				</div>';
	}
}

include('includes/head.php'); 

?>
<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 class="active">
                        <span>Shipping</span>
                    </li>
                    <li>
                        <span>Review &amp; Payments</span>
                    </li>
                </ul>
                <div class="row">
                    <div class="col-lg-8">
                        <ul class="checkout-steps">
                            <li>
                                <h2 class="step-title">Shipping Address</h2>

                                <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);  ?>" method="POST">
								
								    <div class="form-group required-field">
                                        <label>You can update your shipping address </label>
                                        <input type="text" name="s_address" value="<?php echo $s_address;  ?>" class="form-control" required>
                                    </div><!-- End .form-group -->
                                    
                                    <div class="form-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div><!-- End .form-footer -->
								</form>
                            </li>
                         </ul>
                    </div><!-- End .col-lg-8 -->

                    <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'];
										  $total_price += $resPrice;
										?>
                                        <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']) ?></td>
                                        </tr>

                                        <?php endforeach  ?>
                                    </tbody>    
                                </table>
                            </div><!-- End #order-cart-section -->
                        </div><!-- End .order-summary -->
                    </div><!-- End .col-lg-4 -->
                </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>
</body>

</html>