<?php
session_start();
error_reporting(0);
include('includes/config.php');
include_once('includes/settings.php');
if (strlen($_SESSION['login']) == 0) {
    header('location:login.php');
} else {
    if (isset($_POST['update'])) {
        $name = $_POST['name'];
        $contactno = $_POST['contactno'];
        $query = mysqli_query($con, "update users set name='$name',contactno='$contactno' where id='" . $_SESSION['id'] . "'");
        if ($query) {
            echo "<script>alert('Your info has been updated');</script>";
        }
    }


    date_default_timezone_set('Asia/Kolkata');// change according timezone
    $currentTime = date('d-m-Y h:i:s A', time());


    if (isset($_POST['submit'])) {
        $sql = mysqli_query($con, "SELECT password FROM users where password='" . md5($_POST['cpass']) . "' && id='" . $_SESSION['id'] . "'");
        $num = mysqli_fetch_array($sql);
        if ($num > 0) {
            mysqli_query($con, "update users set password='" . md5($_POST['newpass']) . "', updationDate='$currentTime' where id='" . $_SESSION['id'] . "'");
            echo "<script>alert('Password Changed Successfully !!');</script>";
        } else {
            echo "<script>alert('Current Password not match !!');</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>
                    </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 Checkout Form -->
                        <div class="col-lg-8 border-end">
                            <div class="p-4">
                                <h3 class="display-6 fw-bold mb-4">Account Settings</h3>

                                <div class="accordion accordion-flush" id="accountAccordion">
                                    <!-- Step 1: My Profile -->
                                    <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> My Profile
                                            </button>
                                        </h2>

                                        <div id="collapseOne" class="accordion-collapse collapse show"
                                            aria-labelledby="headingOne" data-bs-parent="#accountAccordion">
                                            <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-user fa-lg text-primary"></i>
                                                    </div>
                                                    <h5 class="mb-0 fw-bold">Personal Information</h5>
                                                </div>

                                                <?php
                                                $query = mysqli_query($con, "SELECT * FROM users WHERE id='" . $_SESSION['id'] . "'");
                                                while ($row = mysqli_fetch_array($query)) {
                                                    ?>
                                                    <form method="post" class="needs-validation">
                                                        <div class="row g-3">
                                                            <div class="col-md-12 mb-3">
                                                                <label for="name"
                                                                    class="form-label small text-muted text-uppercase fw-bold">Full
                                                                    Name</label>
                                                                <div class="input-group">
                                                                    <span class="input-group-text bg-light border-end-0"><i
                                                                            class="fa fa-user text-muted"></i></span>
                                                                    <input type="text" class="form-control bg-light" id="name"
                                                                        name="name"
                                                                        value="<?php echo htmlentities($row['name']); ?>"
                                                                        required>
                                                                </div>
                                                            </div>

                                                            <div class="col-md-12 mb-3">
                                                                <label for="email"
                                                                    class="form-label small text-muted text-uppercase fw-bold">Email
                                                                    Address</label>
                                                                <div class="input-group">
                                                                    <span class="input-group-text bg-light border-end-0"><i
                                                                            class="fa fa-envelope text-muted"></i></span>
                                                                    <input type="email" class="form-control" id="email"
                                                                        value="<?php echo htmlentities($row['email']); ?>"
                                                                        readonly disabled>
                                                                </div>
                                                                <small class="text-muted mt-1 d-block"><i
                                                                        class="fa fa-info-circle me-1"></i> Email address cannot
                                                                    be changed</small>
                                                            </div>

                                                            <div class="col-md-12 mb-4">
                                                                <label for="contactno"
                                                                    class="form-label small text-muted text-uppercase fw-bold">Contact
                                                                    Number</label>
                                                                <div class="input-group">
                                                                    <span class="input-group-text bg-light border-end-0"><i
                                                                            class="fa fa-phone text-muted"></i></span>
                                                                    <input type="text" class="form-control bg-light"
                                                                        id="contactno" name="contactno"
                                                                        value="<?php echo htmlentities($row['contactno']); ?>"
                                                                        maxlength="10" required>
                                                                </div>
                                                            </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 Profile
                                                                </button>
                                                            </div>
                                                        </div>
                                                    </form>
                                                <?php } ?>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Step 2: Change Password -->
                                    <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> Change Password
                                            </button>
                                        </h2>
                                        <div id="collapseTwo" class="accordion-collapse collapse"
                                            aria-labelledby="headingTwo" data-bs-parent="#accountAccordion">
                                            <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-lock fa-lg text-primary"></i>
                                                    </div>
                                                    <h5 class="mb-0 fw-bold">Security Settings</h5>
                                                </div>

                                                <form method="post" name="chngpwd" onsubmit="return valid();">
                                                    <div class="row g-3">
                                                        <div class="col-12 mb-3">
                                                            <label for="cpass"
                                                                class="form-label small text-muted text-uppercase fw-bold">Current
                                                                Password</label>
                                                            <input type="password" class="form-control bg-light" id="cpass"
                                                                name="cpass" required>
                                                        </div>

                                                        <div class="col-md-6 mb-3">
                                                            <label for="newpass"
                                                                class="form-label small text-muted text-uppercase fw-bold">New
                                                                Password</label>
                                                            <input type="password" class="form-control bg-light"
                                                                id="newpass" name="newpass" required>
                                                        </div>

                                                        <div class="col-md-6 mb-4">
                                                            <label for="cnfpass"
                                                                class="form-label small text-muted text-uppercase fw-bold">Confirm
                                                                New Password</label>
                                                            <input type="password" class="form-control bg-light"
                                                                id="cnfpass" name="cnfpass" required>
                                                        </div>

                                                        <div class="col-12 text-end border-top pt-3 mt-4">
                                                            <button type="submit" name="submit"
                                                                class="btn btn-primary px-4 py-2 fw-bold shadow-sm">
                                                                <i class="fa fa-key me-2"></i> Change Password
                                                            </button>
                                                        </div>
                                                    </div>
                                                </form>
                                            </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 -->



        <script type="text/javascript">
            function valid() {
                if (document.chngpwd.cpass.value == "") {
                    alert("Current Password Filed is Empty !!");
                    document.chngpwd.cpass.focus();
                    return false;
                }
                else if (document.chngpwd.newpass.value == "") {
                    alert("New Password Filed is Empty !!");
                    document.chngpwd.newpass.focus();
                    return false;
                }
                else if (document.chngpwd.cnfpass.value == "") {
                    alert("Confirm Password Filed is Empty !!");
                    document.chngpwd.cnfpass.focus();
                    return false;
                }
                else if (document.chngpwd.newpass.value != document.chngpwd.cnfpass.value) {
                    alert("Password and Confirm Password Field do not match  !!");
                    document.chngpwd.cnfpass.focus();
                    return false;
                }
                return true;
            }
        </script>
        <?php include('includes/footer.php'); ?>
        <?php include('includes/foot.php'); ?>

    </body>

    </html>
<?php } ?>