<?php
session_start();
//error_reporting(0);
include('includes/config.php');
include_once('includes/settings.php');
if (strlen($_SESSION['login']) == 0) {
	header('location:.php');
} else {
	// code for billing address updation
	if (isset($_POST['update'])) {
		$baddress = $_POST['billingaddress'];
		$bstate = $_POST['bilingstate'];
		$bcity = $_POST['billingcity'];
		$bpincode = $_POST['billingpincode'];
		$query = mysqli_query($con, "update user_details set billingAddress='$baddress',billingState='$bstate',billingCity='$bcity',billingPincode='$bpincode' where user_id='" . $_SESSION['id'] . "'");
		if ($query) {
			echo "<script>alert('Billing Address has been updated');</script>";
		}
	}


	// code for Shipping address updation
	if (isset($_POST['shipupdate'])) {
		$saddress = $_POST['shippingaddress'];
		$sstate = $_POST['shippingstate'];
		$scity = $_POST['shippingcity'];
		$spincode = $_POST['shippingpincode'];
		$query = mysqli_query($con, "update user_details set shippingAddress='$saddress',shippingState='$sstate',shippingCity='$scity',shippingPincode='$spincode' where user_id='" . $_SESSION['id'] . "'");
		if ($query) {
			echo "<script>alert('Shipping Address has been updated');</script>";
		}
	}



	?>
	<!DOCTYPE html>
	<html lang="en">
	<?php include('includes/head.php'); ?>

	<body class="cnt-home">
		<header class="header-style-1">

			<!-- ============================================== TOP MENU ============================================== -->
			<?php include('includes/top-header.php'); ?>
			<!-- ============================================== TOP MENU : END ============================================== -->
			<?php include('includes/main-header.php'); ?>
			<!-- ============================================== NAVBAR ============================================== -->
			<?php include('includes/menu-bar.php'); ?>
			<!-- ============================================== NAVBAR : END ============================================== -->

		</header>
		<!-- ============================================== HEADER : END ============================================== -->

		<!-- Breadcrumb -->
		<div class="breadcrumb-container bg-light py-3 border-bottom mb-4">
			<div class="container">
				<nav aria-label="breadcrumb">
					<ol class="breadcrumb mb-0">
						<li class="breadcrumb-item"><a href="index.php" class="text-decoration-none"><i
									class="fa fa-home"></i> Home</a></li>
						<li class="breadcrumb-item active" aria-current="page">My Account</li>
						<li class="breadcrumb-item active" aria-current="page">Addresses</li>
					</ol>
				</nav>
			</div>
		</div>

		<!-- Body Content -->
		<div class="body-content pb-5">
			<div class="container">
				<div class="checkout-box shadow-sm rounded-4 overflow-hidden bg-white">
					<div class="row g-0">
						<!-- Main Content -->
						<div class="col-lg-8 border-end">
							<div class="p-4">
								<h3 class="display-6 fw-bold mb-4">Manage Addresses</h3>

								<div class="accordion accordion-flush" id="addressAccordion">
									<!-- Step 1: Billing Address -->
									<div class="accordion-item border rounded-3 mb-3">
										<h2 class="accordion-header" id="headingOne">
											<button class="accordion-button fw-bold fs-5 py-4" type="button"
												data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true"
												aria-controls="collapseOne">
												<span class="badge bg-primary rounded-circle me-3">1</span> Billing Address
											</button>
										</h2>

										<div id="collapseOne" class="accordion-collapse collapse show"
											aria-labelledby="headingOne" data-bs-parent="#addressAccordion">
											<div class="accordion-body p-4">
												<div class="d-flex align-items-center mb-4">
													<div class="bg-primary-light p-3 rounded-circle me-3">
														<i class="fa fa-file-invoice fa-lg text-primary"></i>
													</div>
													<h5 class="mb-0 fw-bold">Billing Details</h5>
												</div>

												<?php
												$query = mysqli_query($con, "select user_details.* from users left join user_details on user_details.user_id = users.id where users.id='" . $_SESSION['id'] . "'");
												while ($row = mysqli_fetch_array($query)) {
													?>
													<form method="post">
														<div class="row g-3">
															<div class="col-12 mb-3">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Billing Address">Street Address<span>*</span></label>
																<textarea class="form-control bg-light" name="billingaddress"
																	rows="3"
																	required="required"><?php echo $row['billingAddress']; ?></textarea>
															</div>

															<div class="col-md-4 mb-3">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Billing State ">State <span>*</span></label>
																<input type="text" class="form-control bg-light"
																	id="bilingstate" name="bilingstate"
																	value="<?php echo $row['billingState']; ?>" required>
															</div>
															<div class="col-md-4 mb-3">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Billing City">City <span>*</span></label>
																<input type="text" class="form-control bg-light"
																	id="billingcity" name="billingcity" required="required"
																	value="<?php echo $row['billingCity']; ?>">
															</div>
															<div class="col-md-4 mb-4">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Billing Pincode">Pincode <span>*</span></label>
																<input type="text" class="form-control bg-light"
																	id="billingpincode" name="billingpincode"
																	required="required"
																	value="<?php echo $row['billingPincode']; ?>">
															</div>

															<div class="col-12">
																<button type="submit" name="update"
																	class="btn btn-primary px-4 py-2 fw-bold shadow-sm">
																	<i class="fa fa-save me-2"></i> Update Billing Address
																</button>
															</div>
														</div>
													</form>
												<?php } ?>
											</div>
										</div>
									</div>

									<!-- Step 2: Shipping Address -->
									<div class="accordion-item border rounded-3 mb-3">
										<h2 class="accordion-header" id="headingTwo">
											<button class="accordion-button collapsed fw-bold fs-5 py-4" type="button"
												data-bs-toggle="collapse" data-bs-target="#collapseTwo"
												aria-expanded="false" aria-controls="collapseTwo">
												<span class="badge bg-primary rounded-circle me-3">2</span> Shipping Address
											</button>
										</h2>
										<div id="collapseTwo" class="accordion-collapse collapse"
											aria-labelledby="headingTwo" data-bs-parent="#addressAccordion">
											<div class="accordion-body p-4">
												<div class="d-flex align-items-center mb-4">
													<div class="bg-primary-light p-3 rounded-circle me-3">
														<i class="fa fa-truck fa-lg text-primary"></i>
													</div>
													<h5 class="mb-0 fw-bold">Shipping Details</h5>
												</div>

												<?php
												$query = mysqli_query($con, "select user_details.* from users left join user_details on user_details.user_id = users.id where users.id='" . $_SESSION['id'] . "'");
												while ($row = mysqli_fetch_array($query)) {
													?>
													<form method="post">
														<div class="row g-3">
															<div class="col-12 mb-3">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Shipping Address">Street Address<span>*</span></label>
																<textarea class="form-control bg-light" name="shippingaddress"
																	rows="3"
																	required="required"><?php echo $row['shippingAddress']; ?></textarea>
															</div>

															<div class="col-md-4 mb-3">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Shipping State ">State <span>*</span></label>
																<input type="text" class="form-control bg-light"
																	id="shippingstate" name="shippingstate"
																	value="<?php echo $row['shippingState']; ?>" required>
															</div>
															<div class="col-md-4 mb-3">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Shipping City">City <span>*</span></label>
																<input type="text" class="form-control bg-light"
																	id="shippingcity" name="shippingcity" required="required"
																	value="<?php echo $row['shippingCity']; ?>">
															</div>
															<div class="col-md-4 mb-4">
																<label
																	class="form-label small text-muted text-uppercase fw-bold"
																	for="Shipping Pincode">Pincode <span>*</span></label>
																<input type="text" class="form-control bg-light"
																	id="shippingpincode" name="shippingpincode"
																	required="required"
																	value="<?php echo $row['shippingPincode']; ?>">
															</div>

															<div class="col-12 text-end border-top pt-3 mt-4">
																<button type="submit" name="shipupdate"
																	class="btn btn-primary px-4 py-2 fw-bold shadow-sm">
																	<i class="fa fa-save me-2"></i> Update Shipping Address
																</button>
															</div>
														</div>
													</form>
												<?php } ?>
											</div>
										</div>
									</div>
								</div><!-- /.accordion -->
							</div>
						</div>

						<!-- Sidebar -->
						<div class="col-lg-4 bg-light p-0">
							<div class="sticky-top" style="top: 2rem;">
								<?php include('includes/myaccount-sidebar.php'); ?>
							</div>
						</div>
					</div><!-- /.row -->
				</div><!-- /.checkout-box -->
			</div><!-- /.container -->
		</div><!-- /.body-content -->

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

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

	</html>
<?php } ?>