36637-vm/edit_profile.php
2025-12-04 03:23:32 +00:00

123 lines
5.1 KiB
PHP

<?php
session_start();
// If user is not logged in, redirect to login page
if (!isset($_SESSION['agent_id'])) {
header("Location: login.php");
exit;
}
require_once 'db/config.php';
$agent_id = $_SESSION['agent_id'];
$errors = [];
$success_message = '';
// Fetch current agent data
$stmt = db()->prepare("SELECT name, email, phone, bio, subscription_status FROM agents WHERE id = ?");
$stmt->execute([$agent_id]);
$agent = $stmt->fetch();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Note: Subscription status is not editable by the user.
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$bio = trim($_POST['bio']);
if (empty($name)) {
$errors[] = 'El nombre es obligatorio.';
}
if (empty($email)) {
$errors[] = 'El email es obligatorio.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'El formato del email no es válido.';
}
// Check if email is being changed and if the new one already exists
if ($email !== $agent['email']) {
$stmt = db()->prepare("SELECT id FROM agents WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
$errors[] = 'El nuevo email ya está en uso por otra cuenta.';
}
}
if (empty($errors)) {
$stmt = db()->prepare("UPDATE agents SET name = ?, email = ?, phone = ?, bio = ? WHERE id = ?");
if ($stmt->execute([$name, $email, $phone, $bio, $agent_id])) {
$_SESSION['agent_name'] = $name; // Update session name
$success_message = 'Perfil actualizado con éxito.';
// Re-fetch data to display updated values
$stmt = db()->prepare("SELECT name, email, phone, bio, subscription_status FROM agents WHERE id = ?");
$stmt->execute([$agent_id]);
$agent = $stmt->fetch();
} else {
$errors[] = 'Hubo un error al actualizar el perfil.';
}
}
}
include 'templates/header.php';
?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h2>Editar Perfil</h2>
</div>
<div class="card-body">
<div class="mb-4 p-3 bg-light rounded border">
<h6 class="mb-2 text-muted">Estado de la Suscripción</h6>
<p class="mb-0">
<?php if ($agent['subscription_status'] === 'active'): ?>
<span class="badge bg-success fs-6">Activa</span>
<?php else: ?>
<span class="badge bg-warning text-dark fs-6">Inactiva</span>
<?php endif; ?>
</p>
</div>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p class="mb-0"><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($success_message): ?>
<div class="alert alert-success">
<p class="mb-0"><?php echo $success_message; ?></p>
</div>
<?php endif; ?>
<form action="edit_profile.php" method="post">
<div class="mb-3">
<label for="name" class="form-label">Nombre Completo</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($agent['name']); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Correo Electrónico</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($agent['email']); ?>" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Teléfono</label>
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($agent['phone'] ?? ''); ?>">
</div>
<div class="mb-3">
<label for="bio" class="form-label">Biografía</label>
<textarea class="form-control" id="bio" name="bio" rows="4"><?php echo htmlspecialchars($agent['bio'] ?? ''); ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Guardar Cambios</button>
<a href="dashboard.php" class="btn btn-secondary">Cancelar</a>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
include 'templates/footer.php';
?>