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

if (!isset($_SESSION['user_id']) || !isset($_GET['journal_id'])) {
    echo '<div class="alert alert-danger">Unauthorized access</div>';
    exit();
}

$journal_id = intval($_GET['journal_id']);

// Get journal entry header
$header_sql = "SELECT je.*, u.full_name as created_by_name, p.full_name as posted_by_name
               FROM journal_entries je
               LEFT JOIN users u ON je.created_by = u.user_id
               LEFT JOIN users p ON je.posted_by = p.user_id
               WHERE je.journal_id = ?";
$stmt = $conn->prepare($header_sql);
$stmt->bind_param("i", $journal_id);
$stmt->execute();
$header = $stmt->get_result()->fetch_assoc();

if (!$header) {
    echo '<div class="alert alert-danger">Journal entry not found</div>';
    exit();
}

// Get journal entry lines
$lines_sql = "SELECT jel.*, coa.account_code, coa.account_name
              FROM journal_entry_lines jel
              JOIN chart_of_accounts coa ON jel.account_id = coa.account_id
              WHERE jel.journal_id = ?
              ORDER BY jel.line_number";
$stmt = $conn->prepare($lines_sql);
$stmt->bind_param("i", $journal_id);
$stmt->execute();
$lines_result = $stmt->get_result();
?>

<style>
.journal-detail-header {
    background: #f8fafc;
    padding: 1rem;
    border-radius: 8px;
    margin-bottom: 1.5rem;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid #e2e8f0;
}

.detail-label {
    font-weight: 600;
    color: #64748b;
}

.detail-value {
    color: #1e293b;
}

.journal-lines-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

.journal-lines-table th {
    background: #f8fafc;
    padding: 0.75rem;
    text-align: left;
    font-weight: 600;
    border-bottom: 2px solid #e2e8f0;
}

.journal-lines-table td {
    padding: 0.75rem;
    border-bottom: 1px solid #f1f5f9;
}

.total-row {
    background: #f8fafc;
    font-weight: 700;
}
</style>

<div class="journal-detail-header">
    <div class="detail-row">
        <span class="detail-label">Journal Number:</span>
        <span class="detail-value"><?php echo htmlspecialchars($header['journal_number']); ?></span>
    </div>
    <div class="detail-row">
        <span class="detail-label">Entry Date:</span>
        <span class="detail-value"><?php echo date('d M Y', strtotime($header['entry_date'])); ?></span>
    </div>
    <div class="detail-row">
        <span class="detail-label">Entry Type:</span>
        <span class="detail-value">
            <span class="badge bg-primary"><?php echo str_replace('AUTO_', '', $header['entry_type']); ?></span>
        </span>
    </div>
    <div class="detail-row">
        <span class="detail-label">Status:</span>
        <span class="detail-value">
            <span class="badge bg-<?php echo $header['status'] === 'POSTED' ? 'success' : ($header['status'] === 'VOID' ? 'danger' : 'warning'); ?>">
                <?php echo $header['status']; ?>
            </span>
        </span>
    </div>
    <div class="detail-row">
        <span class="detail-label">Created By:</span>
        <span class="detail-value"><?php echo htmlspecialchars($header['created_by_name']); ?></span>
    </div>
    <?php if ($header['posted_by_name']): ?>
    <div class="detail-row">
        <span class="detail-label">Posted By:</span>
        <span class="detail-value"><?php echo htmlspecialchars($header['posted_by_name']); ?> on <?php echo date('d M Y H:i', strtotime($header['posted_at'])); ?></span>
    </div>
    <?php endif; ?>
    <div class="detail-row" style="border-bottom: none;">
        <span class="detail-label">Description:</span>
        <span class="detail-value"><?php echo htmlspecialchars($header['description']); ?></span>
    </div>
</div>

<h6 class="mb-3">Journal Entry Lines</h6>

<table class="journal-lines-table">
    <thead>
        <tr>
            <th>#</th>
            <th>Account Code</th>
            <th>Account Name</th>
            <th>Description</th>
            <th style="text-align: right;">Debit</th>
            <th style="text-align: right;">Credit</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        $total_debit = 0;
        $total_credit = 0;
        while ($line = $lines_result->fetch_assoc()): 
            $total_debit += $line['debit_amount'];
            $total_credit += $line['credit_amount'];
        ?>
        <tr>
            <td><?php echo $line['line_number']; ?></td>
            <td><?php echo htmlspecialchars($line['account_code']); ?></td>
            <td><?php echo htmlspecialchars($line['account_name']); ?></td>
            <td><?php echo htmlspecialchars($line['description'] ?? '-'); ?></td>
            <td style="text-align: right; color: #059669; font-weight: 600;">
                <?php echo $line['debit_amount'] > 0 ? 'KES ' . number_format($line['debit_amount'], 2) : '-'; ?>
            </td>
            <td style="text-align: right; color: #dc2626; font-weight: 600;">
                <?php echo $line['credit_amount'] > 0 ? 'KES ' . number_format($line['credit_amount'], 2) : '-'; ?>
            </td>
        </tr>
        <?php endwhile; ?>
        <tr class="total-row">
            <td colspan="4"><strong>TOTALS</strong></td>
            <td style="text-align: right;"><strong>KES <?php echo number_format($total_debit, 2); ?></strong></td>
            <td style="text-align: right;"><strong>KES <?php echo number_format($total_credit, 2); ?></strong></td>
        </tr>
        <tr>
            <td colspan="6" style="text-align: center; padding: 1rem;">
                <?php if (abs($total_debit - $total_credit) < 0.01): ?>
                    <span class="badge bg-success" style="font-size: 1rem; padding: 0.5rem 1rem;">
                        <i class="ri-checkbox-circle-line me-1"></i>BALANCED
                    </span>
                <?php else: ?>
                    <span class="badge bg-danger" style="font-size: 1rem; padding: 0.5rem 1rem;">
                        <i class="ri-close-circle-line me-1"></i>OUT OF BALANCE
                    </span>
                <?php endif; ?>
            </td>
        </tr>
    </tbody>
</table>

<?php if ($header['notes']): ?>
<div class="mt-3">
    <h6>Notes</h6>
    <div class="alert alert-info">
        <?php echo nl2br(htmlspecialchars($header['notes'])); ?>
    </div>
</div>
<?php endif; ?>