<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$invoice_id = $_GET['id'] ?? '';

if (empty($invoice_id)) {
    header('Location: list_invoices.php');
    exit();
}

// Get invoice details
$sql = "SELECT i.*, l.lease_code, u.unit_number, p.property_name, p.location_area,
               t.first_name, t.last_name, t.email, t.phone
        FROM invoices i
        JOIN leases l ON i.lease_id = l.lease_id
        JOIN units u ON l.unit_id = u.unit_id
        JOIN properties p ON u.property_id = p.property_id
        JOIN lease_tenants lt ON l.lease_id = lt.lease_id AND lt.is_primary = 1
        JOIN tenants t ON lt.tenant_id = t.tenant_id
        WHERE i.invoice_id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $invoice_id);
$stmt->execute();
$result = $stmt->get_result();
$invoice = $result->fetch_assoc();

if (!$invoice) {
    header('Location: list_invoices.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice <?php echo htmlspecialchars($invoice['invoice_number']); ?></title>
    <style>
        @media print {
            .no-print { display: none; }
        }
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        .invoice-header {
            border-bottom: 2px solid #2d7a2d;
            padding-bottom: 20px;
            margin-bottom: 20px;
        }
        .invoice-body {
            margin-bottom: 20px;
        }
        .invoice-footer {
            border-top: 2px solid #2d7a2d;
            padding-top: 20px;
            margin-top: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 8px;
            text-align: left;
        }
        .text-end {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="no-print" style="margin-bottom: 20px;">
        <button onclick="window.print()" class="btn btn-primary">Print</button>
        <a href="invoice_details.php?id=<?php echo $invoice_id; ?>" class="btn btn-secondary">Back</a>
    </div>

    <div class="invoice-header">
        <h2>INVOICE</h2>
        <p><strong>Invoice #:</strong> <?php echo htmlspecialchars($invoice['invoice_number']); ?></p>
        <p><strong>Date:</strong> <?php echo date('M d, Y', strtotime($invoice['invoice_date'])); ?></p>
        <p><strong>Due Date:</strong> <?php echo date('M d, Y', strtotime($invoice['due_date'])); ?></p>
    </div>

    <div class="invoice-body">
        <div style="display: flex; justify-content: space-between; margin-bottom: 30px;">
            <div>
                <h4>Bill To:</h4>
                <p><strong><?php echo htmlspecialchars($invoice['first_name'] . ' ' . $invoice['last_name']); ?></strong></p>
                <p><?php echo htmlspecialchars($invoice['email']); ?></p>
                <p><?php echo htmlspecialchars($invoice['phone']); ?></p>
            </div>
            <div>
                <h4>Property:</h4>
                <p><?php echo htmlspecialchars($invoice['property_name']); ?></p>
                <p>Unit: <?php echo htmlspecialchars($invoice['unit_number']); ?></p>
                <p><?php echo htmlspecialchars($invoice['location_area']); ?></p>
            </div>
        </div>

        <table border="1" style="width: 100%; border-collapse: collapse;">
            <thead>
                <tr style="background-color: #f0f0f0;">
                    <th>Description</th>
                    <th class="text-end">Amount</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><?php echo htmlspecialchars($invoice['description'] ?? $invoice['type']); ?></td>
                    <td class="text-end">KES <?php echo number_format($invoice['total_amount'], 2); ?></td>
                </tr>
                <tr>
                    <td><strong>Total Amount</strong></td>
                    <td class="text-end"><strong>KES <?php echo number_format($invoice['total_amount'], 2); ?></strong></td>
                </tr>
                <?php if ($invoice['balance'] > 0): ?>
                    <tr>
                        <td>Amount Paid</td>
                        <td class="text-end">KES <?php echo number_format($invoice['total_amount'] - $invoice['balance'], 2); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Balance Due</strong></td>
                        <td class="text-end"><strong>KES <?php echo number_format($invoice['balance'], 2); ?></strong></td>
                    </tr>
                <?php else: ?>
                    <tr>
                        <td><strong>Status</strong></td>
                        <td class="text-end"><strong>Fully Paid</strong></td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <div class="invoice-footer">
        <p class="text-center"><small>Thank you for your business!</small></p>
    </div>
</body>
</html>
