<?php
session_start();
require_once('../includes/admin-security.php');
include_once('includes/config.php');

$id = intval($_GET['id']);

if (isset($_POST['submit'])) {
    $name = mysqli_real_escape_string($con, $_POST['name']);
    $contact = mysqli_real_escape_string($con, $_POST['contactno']);
    $email = mysqli_real_escape_string($con, $_POST['email']);

    // Check if user has details entry
    $check = mysqli_query($con, "SELECT id FROM user_details WHERE user_id='$id'");
    if (mysqli_num_rows($check) > 0) {
        mysqli_query($con, "UPDATE user_details SET name='$name', contactno='$contact' WHERE user_id='$id'");
    } else {
        mysqli_query($con, "INSERT INTO user_details (user_id, name, contactno) VALUES ('$id', '$name', '$contact')");
    }

    // Update Email and Optional Password
    $updateQuery = "UPDATE users SET email='$email'";
    if (!empty($_POST['new_password'])) {
        $password = md5($_POST['new_password']);
        $updateQuery .= ", password='$password'";
        $_SESSION['msg'] = "User details updated successfully (Password Changed)";
    } else {
        $_SESSION['msg'] = "User details updated successfully";
    }
    $updateQuery .= " WHERE id='$id'";

    $updateUser = mysqli_query($con, $updateQuery);

    if (!$updateUser) {
        $_SESSION['error'] = "Error updating user";
    }
}

$query = mysqli_query($con, "SELECT u.email, u.regDate, ud.* FROM users u LEFT JOIN user_details ud ON u.id = ud.user_id WHERE u.id='$id'");
$row = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Edit User | Admin</title>
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="js/all.min.js" crossorigin="anonymous"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>
    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>
        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <div class="page-header mt-4">
                        <div class="d-flex justify-content-between align-items-center">
                            <h1><i class="fas fa-user-edit me-2"></i>Edit User</h1>
                            <a href="manage-users.php" class="btn btn-secondary">Back</a>
                        </div>
                    </div>

                    <?php if (isset($_SESSION['msg'])) { ?>
                        <div class="alert alert-success alert-dismissible fade show">
                            <?php echo $_SESSION['msg'];
                            unset($_SESSION['msg']); ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php } ?>

                    <div class="card mb-4">
                        <div class="card-header">User Details</div>
                        <div class="card-body">
                            <form method="post">
                                <div class="mb-3">
                                    <label class="form-label">Full Name</label>
                                    <input type="text" name="name" class="form-control"
                                        value="<?php echo htmlentities($row['name']); ?>" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Email Address</label>
                                    <input type="email" name="email" class="form-control"
                                        value="<?php echo htmlentities($row['email']); ?>" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Contact Number</label>
                                    <input type="text" name="contactno" class="form-control"
                                        value="<?php echo htmlentities($row['contactno']); ?>">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Shipping Address via Checkout</label>
                                    <textarea class="form-control" readonly
                                        disabled><?php echo htmlentities($row['shippingAddress'] . ' ' . $row['shippingCity'] . ' ' . $row['shippingState']); ?></textarea>
                                    <small class="text-muted">Addresses are updated by the user during checkout.</small>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Reset Password (Optional)</label>
                                    <input type="password" name="new_password" class="form-control"
                                        placeholder="Leave blank to keep current password">
                                </div>
                                <button type="submit" name="submit" class="btn btn-primary">Update User</button>
                            </form>
                        </div>
                    </div>
                </div>
            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>
    <script src="js/bootstrap.bundle.min.js"></script>
    <script src="js/scripts.js"></script>
</body>

</html>
