<?php
$pageTitle = 'Edit Quotation';
require_once __DIR__ . '/../../app/includes/header.php';
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare('SELECT q.*, c.customer_name, c.phone, c.email, c.location, c.address FROM quotations q JOIN customers c ON c.id=q.customer_id WHERE q.id=?');
$stmt->execute([$id]); $quotation = $stmt->fetch();
if (!$quotation || (current_user()['role'] !== 'admin' && (int)$quotation['created_by'] !== current_user()['id'])) { 
    flash('error', 'Quotation not found or permission denied.'); redirect('/quotations/view.php'); 
}
$stmt = db()->prepare('SELECT * FROM quotation_items WHERE quotation_id=? ORDER BY id'); $stmt->execute([$id]); $items = $stmt->fetchAll();
$stmt = db()->prepare('SELECT * FROM quotation_loads WHERE quotation_id=? ORDER BY id'); $stmt->execute([$id]); $loads = $stmt->fetchAll();
$stmt = db()->prepare('SELECT * FROM quotation_import_items WHERE quotation_id=? ORDER BY id'); $stmt->execute([$id]); $importItems = $stmt->fetchAll();
$action = APP_BASE . '/quotations/update.php?id=' . $id;
require __DIR__ . '/form.php';
require_once __DIR__ . '/../../app/includes/footer.php';
