<?php
session_start();

if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 4) {
    header("Location: ../../index.php");
    exit();
}

include('../../includes/config.php');

$deposit_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($deposit_id <= 0) {
    die('Invalid Deposit ID.');
}

$stmt = $conn->prepare("
    SELECT bt.id, bt.transaction_date, bt.reference, bt.amount, bt.description, bt.supporting_document,
           bt.created_by, u.first_name, u.last_name,
           a.account_name, a.account_type, a.bank_name, a.account_number
    FROM bank_transactions bt
    LEFT JOIN accounts a ON bt.account_id = a.id
    LEFT JOIN users u ON bt.created_by = u.id
    WHERE bt.id = ? AND bt.type = 'deposit'
    LIMIT 1
");
$stmt->bind_param('i', $deposit_id);
$stmt->execute();
$result = $stmt->get_result();
$deposit = $result->fetch_assoc();
$stmt->close();

if (!$deposit) {
    die('Deposit not found.');
}

$uploadedFile = !empty($deposit['uploaded_file']) ? '../../uploads/' . htmlspecialchars($deposit['uploaded_file']) : null;
$recorder = trim($deposit['first_name'] . ' ' . $deposit['last_name']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Deposit</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
    body {
        font-family: 'Segoe UI', Tahoma, sans-serif;
        background: #222;
        color: #eee;
        margin: 0;
        padding: 0;
    }
    .container {
        max-width: 700px;
        margin: 50px auto;
        padding: 30px;
        background: rgba(255, 255, 255, 0.08);
        border-radius: 15px;
    }
    h2 {
        text-align: center;
        color: #ffa500;
        margin-bottom: 20px;
    }
    .details {
        background: rgba(255, 255, 255, 0.05);
        padding: 15px;
        border-radius: 10px;
    }
    .details div {
        margin-bottom: 10px;
    }
    .details strong {
        color: #ffa500;
        display: inline-block;
        width: 160px;
    }
    a.btn-back, a.btn-print, a.btn-reconcile {
        display: inline-block;
        background: #ffa500;
        color: #222;
        padding: 10px 16px;
        border-radius: 8px;
        text-decoration: none;
        font-weight: bold;
        margin-top: 20px;
        margin-right: 10px;
    }
    .file-preview img, .file-preview embed {
        max-width: 100%;
        max-height: 300px;
        margin-top: 10px;
        border-radius: 8px;
    }
</style>
</head>
<body>

<div class="container" id="printArea">
    <h2>Deposit Details</h2>

    <div class="details">
        <div><strong>Transaction Date:</strong> <?= htmlspecialchars(substr($deposit['transaction_date'], 0, 10)) ?></div>
        <div><strong>Reference:</strong> <?= htmlspecialchars($deposit['reference']) ?></div>
        <div><strong>Description:</strong> <?= htmlspecialchars($deposit['description'] ?: '-') ?></div>
        <div><strong>Amount:</strong> MWK <?= number_format($deposit['amount'], 2) ?></div>
        <div><strong>Account:</strong> <?= htmlspecialchars($deposit['account_name'] ?: 'N/A') ?></div>
        <div><strong>Account Type:</strong> <?= htmlspecialchars(ucfirst($deposit['account_type'] ?: 'N/A')) ?></div>
        <?php if ($deposit['account_type'] === 'bank'): ?>
            <div><strong>Bank Name:</strong> <?= htmlspecialchars($deposit['bank_name'] ?: '-') ?></div>
            <div><strong>Account Number:</strong> <?= htmlspecialchars($deposit['account_number'] ?: '-') ?></div>
        <?php endif; ?>
        <div><strong>Recorded By:</strong> <?= $recorder ?: 'N/A' ?></div>
        
        <?php if ($uploadedFile): ?>
            <div><strong>Attached Document:</strong></div>
            <div class="file-preview">
                <?php
                    $ext = strtolower(pathinfo($uploadedFile, PATHINFO_EXTENSION));
                    if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) {
                        echo '<img src="'. $uploadedFile .'" alt="Document">';
                    } elseif ($ext === 'pdf') {
                        echo '<embed src="'. $uploadedFile .'" type="application/pdf">';
                    } else {
                        echo '<a href="'. $uploadedFile .'" target="_blank">View Document</a>';
                    }
                ?>
            </div>
        <?php endif; ?>
    </div>

    <a href="javascript:history.back()" class="btn-back">&larr; Back</a>
    <a href="#" onclick="printDiv()" class="btn-print">🖨️ Print</a>
    <a href="../accountant/bank_reconciliation.php?reference=<?= urlencode($deposit['reference']) ?>" class="btn-reconcile" target="_blank">🔗 Reconcile</a>
</div>

<script>
function printDiv() {
    const printContents = document.getElementById('printArea').innerHTML;
    const originalContents = document.body.innerHTML;
    document.body.innerHTML = printContents;
    window.print();
    document.body.innerHTML = originalContents;
    location.reload();
}
</script>

</body>
</html>
