<?php
session_start();

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

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

$id = intval($_GET['id']);
$sql = "SELECT * FROM detalle_venta WHERE id_detalle = $id";
$resultado = mysqli_query($conexion, $sql);
$fila = mysqli_fetch_assoc($resultado);

if (!$fila) {
    header("Location: listar.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Editar Detalle de Venta</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.form-card{
    background:#fff;
    border-radius:15px;
    padding:30px;
    box-shadow:0 2px 15px rgba(0,0,0,.05);
}
.form-control,.form-select{
    border-radius:10px;
    border:2px solid #e0e0e0;
    padding:12px 15px;
}
.form-control:focus,.form-select:focus{
    border-color:#002855;
    box-shadow:0 0 0 .2rem rgba(0,40,85,.25);
}
.btn-primary{
    background:#002855;
    border:none;
    border-radius:10px;
    padding:12px 30px;
}
.btn-primary:hover{
    background:#003d7a;
}
.info-box{
    background:#e9ecef;
    border-radius:10px;
    padding:15px;
}
</style>
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-edit text-warning"></i> Editar Detalle de Venta</h2>
        <a href="listar.php" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Volver</a>
    </div>

    <div class="form-card">
        <div class="info-box mb-4">
            <strong>ID Detalle:</strong> #<?php echo $fila['id_detalle']; ?>
        </div>

        <form action="actualizar.php" method="POST">
            <input type="hidden" name="id_detalle" value="<?php echo $fila['id_detalle']; ?>">

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">ID Venta</label>
                    <input type="number" name="id_venta" class="form-control" value="<?php echo $fila['id_venta']; ?>" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Accesorio</label>
                    <select name="id_producto" class="form-select" required>
                        <?php
                        $res = mysqli_query($conexion,"SELECT id_accesorio,nombre FROM accesorio ORDER BY nombre");
                        while($p = mysqli_fetch_assoc($res)){
                            $sel = ($p['id_accesorio']==$fila['id_producto']) ? "selected" : "";
                            echo "<option value='{$p['id_accesorio']}' $sel>{$p['nombre']}</option>";
                        }
                        ?>
                    </select>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label class="form-label">Cantidad</label>
                    <input type="number" name="cantidad" class="form-control" value="<?php echo $fila['cantidad']; ?>" required>
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label">Precio Unitario</label>
                    <input type="number" step="0.01" name="precio_unitario" class="form-control" value="<?php echo $fila['precio_unitario']; ?>" required>
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label">Subtotal</label>
                    <input type="number" step="0.01" name="subtotal" class="form-control" value="<?php echo $fila['subtotal']; ?>" required>
                </div>
            </div>

            <div class="mt-3">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Actualizar Detalle</button>
                <a href="listar.php" class="btn btn-secondary">Cancelar</a>
            </div>
        </form>
    </div>
</div>
</body>
</html>

