<?php ob_start(); ?>
<div class="container-fluid py-4">
    <div class="row">
        <!-- Invoice Details -->
        <div class="col-lg-8">
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Invoice #<?= htmlspecialchars($invoice['invoice_number'] ?? '') ?></h5>
                    <div>
                        <span class="badge bg-primary fs-6"><?= ucfirst(htmlspecialchars($invoice['status'] ?? 'Draft')) ?></span>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row mb-4">
                        <div class="col-sm-6">
                            <h6 class="text-uppercase text-secondary text-xxs font-weight-bolder mb-2">Project Info</h6>
                            <p class="text-sm mb-1 fw-bold"><?= htmlspecialchars($invoice['project_name'] ?? 'N/A') ?></p>
                            <p class="text-sm mb-0">Contractor: <?= htmlspecialchars($invoice['contractor_name'] ?? 'N/A') ?></p>
                        </div>
                        <div class="col-sm-6 text-sm-end mt-sm-0 mt-3">
                            <h6 class="text-uppercase text-secondary text-xxs font-weight-bolder mb-2">Invoice Info</h6>
                            <p class="text-sm mb-1">Date: <?= htmlspecialchars($invoice['date'] ?? '') ?></p>
                            <p class="text-sm mb-1">Due: <?= htmlspecialchars($invoice['due_date'] ?? '') ?></p>
                            <p class="text-sm mb-0">PO: <?= htmlspecialchars($invoice['po_reference'] ?? 'N/A') ?></p>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Description</th>
                                    <th>Qty</th>
                                    <th>Unit Price</th>
                                    <th class="text-end">Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if(!empty($invoice['items'])): ?>
                                    <?php foreach($invoice['items'] as $item): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($item['description']) ?></td>
                                        <td><?= $item['qty'] ?></td>
                                        <td><?= formatCurrency((float)$item['unit_price'], $invoice['currency'] ?? DEFAULT_CURRENCY) ?></td>
                                        <td class="text-end"><?= formatCurrency((float)($item['qty'] * $item['unit_price']), $invoice['currency'] ?? DEFAULT_CURRENCY) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="row mt-4">
                        <div class="col-md-6">
                            <?php if(!empty($invoice['notes'])): ?>
                                <h6 class="text-sm">Notes:</h6>
                                <p class="text-sm text-secondary"><?= nl2br(htmlspecialchars($invoice['notes'])) ?></p>
                            <?php endif; ?>
                            <?php if(!empty($invoice['attachments'])): ?>
                                <h6 class="text-sm mt-3">Attachments:</h6>
                                <?php foreach ($invoice['attachments'] as $att): ?>
                                <a href="<?= APP_URL ?>/uploads/<?= htmlspecialchars($att['file_path']) ?>" class="btn btn-sm btn-outline-primary mb-1" download><?= htmlspecialchars($att['file_name']) ?></a><br>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                        <div class="col-md-4 offset-md-2">
                            <div class="d-flex justify-content-between mb-2">
                                <span>Subtotal:</span>
                                <span><?= formatCurrency((float)($invoice['subtotal'] ?? 0), $invoice['currency'] ?? DEFAULT_CURRENCY) ?></span>
                            </div>
                            <div class="d-flex justify-content-between mb-2">
                                <span>Tax Amount:</span>
                                <span><?= formatCurrency((float)($invoice['tax_amount'] ?? 0), $invoice['currency'] ?? DEFAULT_CURRENCY) ?></span>
                            </div>
                            <hr>
                            <div class="d-flex justify-content-between fw-bold fs-5">
                                <span>Total:</span>
                                <span><?= formatCurrency((float)($invoice['total'] ?? 0), $invoice['currency'] ?? DEFAULT_CURRENCY) ?></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Comments Section -->
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0">Comments</h6>
                </div>
                <div class="card-body">
                    <?php if(!empty($comments)): ?>
                        <?php foreach($comments as $comment): ?>
                        <div class="d-flex mb-3">
                            <div>
                                <h6 class="text-sm mb-0"><?= htmlspecialchars($comment['user_name']) ?> <span class="text-xs text-secondary ms-2"><?= $comment['created_at'] ?></span></h6>
                                <p class="text-sm mb-0"><?= nl2br(htmlspecialchars($comment['comment'])) ?></p>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <p class="text-sm text-secondary">No comments yet.</p>
                    <?php endif; ?>

                    <form action="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/comment" method="POST" class="mt-3">
                        <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?? '' ?>">
                        <div class="input-group">
                            <textarea name="comment" class="form-control" placeholder="Add a comment..." required rows="2"></textarea>
                            <button type="submit" class="btn btn-primary mb-0">Post</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Sidebar Actions & Timeline -->
        <div class="col-lg-4">
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0">Actions</h6>
                </div>
                <div class="card-body d-flex flex-column gap-2">
                    <a href="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/pdf" class="btn btn-outline-dark w-100"><i class="fa fa-file-pdf me-2"></i>Download PDF</a>

                    <?php if($invoice['status'] === 'draft'): ?>
                        <a href="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/edit" class="btn btn-outline-info w-100">Edit Invoice</a>
                        <form action="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/submit" method="POST">
                            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?? '' ?>">
                            <button type="submit" class="btn btn-primary w-100">Submit Invoice</button>
                        </form>
                    <?php endif; ?>

                    <?php if($invoice['status'] === 'submitted'): ?>
                        <form action="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/review" method="POST">
                            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?? '' ?>">
                            <button type="submit" class="btn btn-warning w-100">Mark Under Review</button>
                        </form>
                    <?php endif; ?>

                    <?php if(in_array($invoice['status'], ['submitted', 'under_review'])): ?>
                        <form action="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/approve" method="POST">
                            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?? '' ?>">
                            <button type="submit" class="btn btn-success w-100">Approve</button>
                        </form>
                        <button type="button" class="btn btn-danger w-100" data-bs-toggle="modal" data-bs-target="#rejectModal">Reject</button>
                    <?php endif; ?>

                    <?php if($invoice['status'] === 'approved'): ?>
                        <form action="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/paid" method="POST">
                            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?? '' ?>">
                            <button type="submit" class="btn btn-success w-100">Mark as Paid</button>
                        </form>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Status Timeline -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0">Status Timeline</h6>
                </div>
                <div class="card-body p-3">
                    <div class="timeline timeline-one-side">
                        <div class="timeline-block mb-3">
                            <span class="timeline-step">
                                <i class="fa fa-bell text-success text-gradient"></i>
                            </span>
                            <div class="timeline-content">
                                <h6 class="text-dark text-sm font-weight-bold mb-0">Created</h6>
                                <p class="text-secondary font-weight-bold text-xs mt-1 mb-0"><?= $invoice['created_at'] ?? 'N/A' ?></p>
                            </div>
                        </div>
                        <!-- Add other timeline items dynamically based on logs -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Reject Modal -->
<div class="modal fade" id="rejectModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>/reject" method="POST">
                <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?? '' ?>">
                <div class="modal-header">
                    <h5 class="modal-title">Reject Invoice</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label>Reason for Rejection</label>
                        <textarea name="reason" class="form-control" required rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger">Reject</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>

