<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once(__DIR__ . '/../src/Database.php');
require_once(__DIR__ . '/../src/PDFGenerator.php');

$db = new Database();
$pdfGenerator = new PDFGenerator();
$errorMsg = '';

// Обработка добавления счета
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['customer']) && isset($_POST['amount'])) {
        try {
            $db->addInvoice($_POST['customer'], $_POST['description'], $_POST['amount'], $_POST['date'], $_POST['prepuesto']);

        } catch (Exception $e) {
            $errorMsg = 'Error adding invoice: ' . $e->getMessage();
        }
    }

    // Обработка генерации PDF
    if (isset($_POST['generate_pdf'])) {
        try {
            $invoices = $db->getAllInvoices();
            $pdfGenerator->generate($invoices);
        } catch (Exception $e) {
            $errorMsg = 'Error generating PDF: ' . $e->getMessage();
        }
    }
}

try {
    $invoices = $db->getAllInvoices();
} catch (Exception $e) {
    $errorMsg = 'Error fetching invoices: ' . $e->getMessage();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Invoice App</title>
</head>
<body>
    <h1>Invoice Application</h1>
    <form method="post">
        Customer: <input type="text" name="customer"><br>
        Description: <input type="text" name="description"><br>
        Amount: <input type="number" name="amount" step="0.01"><br>
        Date: <input type="text" name="date"><br>
        Prepuesto: <input type="text" name="prepuesto"><br>
        <input type="submit" value="Add Invoice">
    </form>

    <?php if (!empty($errorMsg)): ?>
        <p style="color: red;"><?= htmlspecialchars($errorMsg) ?></p>
    <?php endif; ?>

    <hr>

    <h2>Invoices</h2>
    <table>
        <tr>
            <th>ID</th>
            <th>Customer</th>
            <th>Description</th>
            <th>Amount</th>
            <th>Date</th>
            <th>Prepuesto</th>
            <th>Actions</th>
        </tr>
        <?php foreach ($invoices as $invoice): ?>
        <tr>
            <td><?= htmlspecialchars($invoice['id']) ?></td>
            <td><?= htmlspecialchars($invoice['customer']) ?></td>
            <td><?= htmlspecialchars($invoice['description']) ?></td>
            <td>$<?= number_format($invoice['amount'], 2) ?></td>
            <td><?= htmlspecialchars($invoice['date']) ?></td>
            <td><?= htmlspecialchars($invoice['prepuesto']) ?></td>
            <td>
                <form method="post">
                    <input type="hidden" name="generate_pdf" value="1">
                    <button type="submit">Save as PDF</button>
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>

    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</body>
</html>
