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

190 lines
7.8 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';
// Fetch agent details, including subscription status
$agent_id = $_SESSION['agent_id'];
$stmt = db()->prepare("SELECT subscription_status FROM agents WHERE id = ?");
$stmt->execute([$agent_id]);
$agent = $stmt->fetch();
$is_active = ($agent && $agent['subscription_status'] === 'active');
$errors = [];
$success_message = '';
// Handle Post Request
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_property'])) {
if (!$is_active) {
$errors[] = 'Necesitas una suscripción activa para añadir nuevas propiedades.';
} else {
$title = trim($_POST['title']);
$description = trim($_POST['description']);
$price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
$location = trim($_POST['location']);
$bedrooms = filter_input(INPUT_POST, 'bedrooms', FILTER_VALIDATE_INT);
$bathrooms = filter_input(INPUT_POST, 'bathrooms', FILTER_VALIDATE_INT);
$type = trim($_POST['type']);
if (empty($title)) {
$errors[] = 'El título es obligatorio.';
}
if ($price === false || $price <= 0) {
$errors[] = 'Se requiere un precio válido.';
}
if (empty($location)) {
$errors[] = 'La ubicación es obligatoria.';
}
if (empty($type)) {
$errors[] = 'El tipo es obligatorio.';
}
if (empty($errors)) {
$stmt = db()->prepare(
"INSERT INTO properties (agent_id, title, description, price, location, bedrooms, bathrooms, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
if ($stmt->execute([$agent_id, $title, $description, $price, $location, $bedrooms, $bathrooms, $type])) {
$success_message = '¡Propiedad agregada con éxito!';
} else {
$errors[] = 'Error al guardar la propiedad.';
}
}
}
}
// Fetch properties for the logged-in agent
$stmt = db()->prepare("SELECT * FROM properties WHERE agent_id = ? ORDER BY created_at DESC");
$stmt->execute([$agent_id]);
$properties = $stmt->fetchAll();
require_once 'templates/header.php';
?>
<h1 class="mb-4">Gestionar Propiedades</h1>
<?php if ($success_message): ?>
<div class="alert alert-success" role="alert">
<?php echo htmlspecialchars($success_message); ?>
</div>
<?php endif; ?>
<?php if (!empty($errors)):
?>
<div class="alert alert-danger" role="alert">
<?php foreach ($errors as $error):
?>
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($is_active):
?>
<!-- Add Property Form -->
<div class="card mb-4">
<div class="card-header">
<h2 class="h5 mb-0">Añadir Nueva Propiedad</h2>
</div>
<div class="card-body">
<form action="properties.php" method="POST">
<input type="hidden" name="add_property" value="1">
<div class="mb-3">
<label for="title" class="form-label">Título <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Descripción</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="price" class="form-label">Precio <span class="text-danger">*</span></label>
<div class="input-group">
<span class="input-group-text">$</span>
<input type="number" class="form-control" id="price" name="price" step="0.01" required>
</div>
</div>
<div class="col-md-6 mb-3">
<label for="location" class="form-label">Ubicación <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="location" name="location" required>
</div>
</div>
<div class="row">
<div class="col-md-4 mb-3">
<label for="bedrooms" class="form-label">Habitaciones</label>
<input type="number" class="form-control" id="bedrooms" name="bedrooms" min="0">
</div>
<div class="col-md-4 mb-3">
<label for="bathrooms" class="form-label">Baños</label>
<input type="number" class="form-control" id="bathrooms" name="bathrooms" min="0">
</div>
<div class="col-md-4 mb-3">
<label for="type" class="form-label">Tipo <span class="text-danger">*</span></label>
<select class="form-select" id="type" name="type" required>
<option value="">Elegir...</option>
<option value="Sale">En Venta</option>
<option value="Rent">En Alquiler</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary"><i class="bi bi-plus-circle"></i> Añadir Propiedad</button>
</form>
</div>
</div>
<?php else: ?>
<div class="alert alert-warning" role="alert">
<h4 class="alert-heading">Suscripción Inactiva</h4>
<p>Tu suscripción no está activa. Por favor, contacta al administrador para activarla y poder añadir nuevas propiedades.</p>
</div>
<?php endif; ?>
<!-- Existing Properties List -->
<div class="card">
<div class="card-header">
<h2 class="h5 mb-0">Tus Anuncios</h2>
</div>
<div class="card-body">
<?php if (empty($properties)):
?>
<p class="text-center text-muted">Aún no has publicado ninguna propiedad. Añade una usando el formulario de arriba.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Título</th>
<th>Ubicación</th>
<th>Precio</th>
<th>Tipo</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<?php foreach ($properties as $property): ?>
<tr>
<td><?php echo htmlspecialchars($property['title']); ?></td>
<td><?php echo htmlspecialchars($property['location']); ?></td>
<td>$<?php echo htmlspecialchars(number_format($property['price'], 2)); ?></td>
<td><span class="badge bg-primary"><?php echo htmlspecialchars(ucfirst($property['type'])); ?></span></td>
<td>
<a href="#" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil-square"></i> Editar</a>
<a href="#" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i> Eliminar</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
<?php require_once 'templates/footer.php'; ?>