<?php session_start();
include_once('includes/config.php');
include_once('includes/settings.php');
if (strlen($_SESSION['id']) == 0) {
    header('location:logout.php');
} else {

    // For Address Insertion
    if (isset($_POST['submit'])) {
        $uid = $_SESSION['id'];
        //Getting Post Values
        $baddress = $_POST['baddress'];
        $bcity = $_POST['bcity'];
        $bstate = $_POST['bstate'];
        $bpincode = $_POST['bpincode'];
        $bcountry = $_POST['bcountry'];
        $saddress = $_POST['saddress'];
        $scity = $_POST['scity'];
        $sstate = $_POST['sstate'];
        $spincode = $_POST['spincode'];
        $scountry = $_POST['scountry'];

        $sql = mysqli_query($con, "insert into addresses(userId,billingAddress,biilingCity,billingState,billingPincode,billingCountry,shippingAddress,shippingCity,shippingState,shippingPincode,shippingCountry) values('$uid','$baddress','$bcity','$bstate','$bpincode','$bcountry','$saddress','$scity','$sstate','$spincode','$scountry')");
        if ($sql) {
            echo "<script>alert('You Address added successfully');</script>";
            echo "<script type='text/javascript'> document.location ='manage-addresses.php'; </script>";
        } else {
            echo "<script>alert('Something went wrong. Please try again.');</script>";
            echo "<script type='text/javascript'> document.location ='manage-addresses.php'; </script>";
        }
    }

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

    <body class="cnt-home">
        <header class="header-style-1">
            <?php include('includes/top-header.php'); ?>
            <?php include('includes/main-header.php'); ?>
            <?php include('includes/menu-bar.php'); ?>
        </header>

        <!-- Breadcrumb -->
        <div class="breadcrumb mt-2">
            <div class="container">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                        <li class="breadcrumb-item"><a href="my-profile.php">My Profile</a></li>
                        <li class="breadcrumb-item active">Manage Addresses</li>
                    </ol>
                </nav>
            </div>
        </div>

        <!-- Main Content -->
        <div class="body-content outer-top-xs">
            <div class="container">
                <!-- Section-->
                <section class="py-5">
                    <div class="container px-4  mt-5">




                        <h5>Already Listed Addresses</h5>
                        <?php
                        $uid = $_SESSION['id'];
                        $query = mysqli_query($con, "select * from addresses where userId='$uid'");
                        $count = mysqli_num_rows($query);
                        if ($count == 0):
                            echo "<font color='red'>No addresses Found.</font>";
                        else:
                            ?>
                            <form method="post">
                                <input type="hidden" name="grandtotal" value="<?php echo $grantotal; ?>">
                                <div class="row">
                                    <div class="col-6">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th colspan="4">
                                                        <h5>Billing Address</h5>
                                                    </th>
                                                </tr>
                                            </thead>
                                            <tr>
                                                <thead>
                                                    <th width="250">Adresss</th>
                                                    <th>City</th>
                                                    <th>State</th>
                                                    <th>Pincode</th>
                                                    <th>Country</th>

                                                </thead>
                                            </tr>
                                        </table>

                                    </div>
                                    <div class="col-6">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <th colspan="4">
                                                        <h5>Shipping Address</h5>
                                                    </th>
                                                </tr>
                                            </thead>
                                            <tr>
                                                <thead>
                                                    <th width="250">Adresss</th>
                                                    <th>City</th>
                                                    <th>State</th>
                                                    <th>Pincode</th>
                                                    <th>Country</th>

                                                </thead>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <!-- Fecthing Values-->
                                <?php while ($result = mysqli_fetch_array($query)) { ?>
                                    <div class="row">
                                        <div class="col-6">
                                            <table class="table">

                                                <tbody>

                                                    <tr>

                                                        <td width="250"><?php echo $result['billingAddress']; ?></td>
                                                        <td><?php echo $result['biilingCity']; ?></td>
                                                        <td><?php echo $result['billingState']; ?></td>
                                                        <td><?php echo $result['billingPincode']; ?></td>
                                                        <td><?php echo $result['billingCountry']; ?></td>
                                                    </tr>
                                                </tbody>
                                            </table>

                                        </div>
                                        <div class="col-6">
                                            <table class="table">
                                                <tbody>
                                                    <tr>
                                                        <td width="250"><?php echo $result['shippingAddress']; ?></td>
                                                        <td><?php echo $result['shippingCity']; ?></td>
                                                        <td><?php echo $result['shippingState']; ?></td>
                                                        <td><?php echo $result['shippingPincode']; ?></td>
                                                        <td><?php echo $result['shippingCountry']; ?></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>


                                <?php }endif; ?>

                        </form>

                        <hr />
                        <form method="post" name="address">

                            <div class="row">
                                <!--Billing Addresss --->
                            <div class="col-6">
                                <div class="row">
                                    <div class="col-9" align="center">
                                        <h5>New Billing Address</h5><br />
                                    </div>
                                    <hr />
                                </div>
                                <div class="row">
                                    <div class="col-3">Address</div>
                                    <div class="col-6"><input type="text" name="baddress" id="baddress"
                                            class="form-control" required></div>
                                </div>
                                <div class="row mt-3">
                                    <div class="col-3">City</div>
                                    <div class="col-6"><input type="text" name="bcity" id="bcity" class="form-control"
                                            required>
                                    </div>


                                </div>

                                <div class="row mt-3">
                                    <div class="col-3">State</div>
                                    <div class="col-6"><input type="text" name="bstate" id="bstate" class="form-control"
                                            required></div>
                                </div>

                                <div class="row mt-3">
                                    <div class="col-3">Pincode</div>
                                    <div class="col-6"><input type="text" name="bpincode" id="bpincode" pattern="[0-9]+"
                                            title="only numbers" maxlength="6" class="form-control" required></div>
                                </div>

                                <div class="row mt-3">
                                    <div class="col-3">Country</div>
                                    <div class="col-6"><input type="text" name="bcountry" id="bcountry"
                                            class="form-control" required></div>
                                </div>
                            </div>
                            <!--Shipping Addresss --->
                            <div class="col-6">
                                <div class="row">
                                    <div class="col-9" align="center">
                                        <h5>New Shipping Address</h5>
                                        <input type="checkbox" name="adcheck" value="1" />
                                        <small>Shipping Adress same as billing Address</small>
                                    </div>
                                    <hr />
                                </div>
                                <div class="row">
                                    <div class="col-3">Address</div>
                                    <div class="col-6"><input type="text" name="saddress" id="saddress"
                                            class="form-control" required></div>
                                </div>
                                <div class="row mt-3">
                                    <div class="col-3">City</div>
                                    <div class="col-6"><input type="text" name="scity" id="scity" class="form-control"
                                            required>
                                    </div>

                                </div>

                                <div class="row mt-3">
                                    <div class="col-3">State</div>
                                    <div class="col-6"><input type="text" name="sstate" id="sstate" class="form-control"
                                            required></div>
                                </div>

                                <div class="row mt-3">
                                    <div class="col-3">Pincode</div>
                                    <div class="col-6"><input type="text" name="spincode" id="spincode" pattern="[0-9]+"
                                            title="only numbers" maxlength="6" class="form-control" required></div>
                                </div>

                                <div class="row mt-3">
                                    <div class="col-3">Country</div>
                                    <div class="col-6"><input type="text" name="scountry" id="scountry"
                                            class="form-control" required></div>
                                </div>


                            </div>
                            <div class="row mt-3">
                                <div class="col-5">&nbsp;</div>
                                <div class="col-6"><input type="submit" name="submit" id="submit"
                                        class="btn btn-primary" value="Add" required></div>
                            </div>

                        </div>
                    </form>


                </div>


        </div>
    </div><!-- /.container -->
        </div><!-- /.body-content -->

        <!-- Footer -->
        <?php include('includes/footer.php'); ?>
        <?php include('includes/foot.php'); ?>
        <script type="text/javascript">
            $(document).ready(function () {
                $('input[type="checkbox"]').click(function () {
                    if ($(this).prop("checked") == true) {
                        $('#saddress').val($('#baddress').val());
                        $('#scity').val($('#bcity').val());
                        $('#sstate').val($('#bstate').val());
                        $('#spincode').val($('#bpincode').val());
                        $('#scountry').val($('#bcountry').val());
                    }

                });
            });
        </script>
    </body>

    </html>
<?php } ?>