<?php
// admin/accounts/view-expense.php
require_once __DIR__ . '/../../includes/config.php';
require_once __DIR__ . '/../../includes/functions.php';

if (!isAdmin()) {
    header('Location: ../login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: expenses.php');
    exit;
}

// Get expense details
$stmt = $pdo->prepare("SELECT e.*, v.name as vendor_name 
                       FROM expenses e 
                       LEFT JOIN vendors v ON e.vendor_id = v.id 
                       WHERE e.id = ?");
$stmt->execute([$id]);
$expense = $stmt->fetch();

if (!$expense) {
    header('Location: expenses.php');
    exit;
}

include '../header.php';
?>

<style>
.view-card {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    margin-bottom: 20px;
}

.view-card .card-title {
    font-weight: 600;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid #e2e8f0;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

.info-item {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px 20px;
}

.info-item .label {
    color: #64748b;
    font-size: 13px;
    font-weight: 500;
    display: block;
    margin-bottom: 5px;
}

.info-item .value {
    font-size: 16px;
    font-weight: 600;
    color: #1e293b;
}

.info-item .value.text-danger { color: #ef4444; }

.action-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    flex-wrap: wrap;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-arrow-up me-2"></i> Expense Details</h2>
        <div>
            <a href="expenses.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <div class="view-card">
        <h5 class="card-title">
            <i class="fa-solid fa-receipt me-2"></i>
            Expense #<?php echo $expense['expense_no']; ?>
        </h5>

        <div class="info-grid">
            <div class="info-item">
                <span class="label">Expense Number</span>
                <span class="value"><code><?php echo $expense['expense_no']; ?></code></span>
            </div>
            <div class="info-item">
                <span class="label">Category</span>
                <span class="value"><?php echo htmlspecialchars($expense['category']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Description</span>
                <span class="value"><?php echo htmlspecialchars($expense['description']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Amount</span>
                <span class="value text-danger"><?php echo number_format($expense['amount']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Payment Method</span>
                <span class="value"><?php echo ucfirst($expense['payment_method']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Expense Date</span>
                <span class="value"><?php echo date('d M Y', strtotime($expense['expense_date'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Vendor</span>
                <span class="value"><?php echo htmlspecialchars($expense['vendor_name'] ?? 'N/A'); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Reference No</span>
                <span class="value"><?php echo $expense['reference_no'] ?: 'N/A'; ?></span>
            </div>
            <?php if ($expense['note']): ?>
            <div class="info-item" style="grid-column: 1 / -1;">
                <span class="label">Note</span>
                <span class="value"><?php echo nl2br(htmlspecialchars($expense['note'])); ?></span>
            </div>
            <?php endif; ?>
            <div class="info-item">
                <span class="label">Created At</span>
                <span class="value"><?php echo date('d M Y, h:i A', strtotime($expense['created_at'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Created By</span>
                <span class="value"><?php echo $expense['created_by'] ? 'Admin' : 'System'; ?></span>
            </div>
        </div>

        <div class="action-buttons">
            <a href="edit-expense.php?id=<?php echo $expense['id']; ?>" class="btn btn-warning">
                <i class="fa-solid fa-edit"></i> Edit
            </a>
            <a href="print-expense.php?id=<?php echo $expense['id']; ?>" class="btn btn-primary" target="_blank">
                <i class="fa-solid fa-print"></i> Print
            </a>
            <button onclick="deleteExpense(<?php echo $expense['id']; ?>)" class="btn btn-danger">
                <i class="fa-solid fa-trash"></i> Delete
            </button>
        </div>
    </div>
</div>

<script>
function deleteExpense(id) {
    if (confirm('Are you sure you want to delete this expense record?')) {
        window.location.href = 'delete-expense.php?id=' + id;
    }
}
</script>

<?php include '../footer.php'; ?>