<?php
session_start();

if (!isset($_SESSION['id'])) {
    header("Location: ../index.php");
    exit();
}

include("../conexion.php");

// Obtener el id del cliente
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($id <= 0) {
    header("Location: ../menu.php?page=clientes&error=id_invalido");
    exit();
}

// Verificar que el cliente existe
$check_sql = "SELECT id_cliente FROM cliente WHERE id_cliente = $id";
$check_result = mysqli_query($conexion, $check_sql);

if (mysqli_num_rows($check_result) == 0) {
    header("Location: ../menu.php?page=clientes&error=cliente_no_encontrado");
    exit();
}

// Eliminar cliente
$sql = "DELETE FROM cliente WHERE id_cliente = $id";

if (mysqli_query($conexion, $sql)) {
    header("Location: ../menu.php?page=clientes&success=eliminado");
    exit();
} else {
    header("Location: ../menu.php?page=clientes&error=error_eliminacion");
    exit();
}

mysqli_close($conexion);
?>