<?php
// admin/accounts/view-income.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: income.php');
    exit;
}

// Get income details
$stmt = $pdo->prepare("SELECT * FROM income WHERE id = ?");
$stmt->execute([$id]);
$income = $stmt->fetch();

if (!$income) {
    header('Location: income.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-success { color: #22c55e; }
.info-item .value.text-danger { color: #ef4444; }

.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    display: inline-block;
}

.status-badge.completed { background: #86efac; color: #166534; }
.status-badge.pending { background: #fcd34d; color: #92400e; }
.status-badge.failed { background: #fca5a5; color: #991b1b; }

.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-down me-2"></i> Income Details</h2>
        <div>
            <a href="income.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>
            Income #<?php echo $income['income_no']; ?>
            <span class="status-badge <?php echo $income['status'] ?? 'completed'; ?>">
                <?php echo ucfirst($income['status'] ?? 'Completed'); ?>
            </span>
        </h5>

        <div class="info-grid">
            <div class="info-item">
                <span class="label">Income Number</span>
                <span class="value"><code><?php echo $income['income_no']; ?></code></span>
            </div>
            <div class="info-item">
                <span class="label">Customer Name</span>
                <span class="value"><?php echo htmlspecialchars($income['customer_name']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Amount</span>
                <span class="value text-success"><?php echo number_format($income['amount']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Payment Method</span>
                <span class="value"><?php echo ucfirst($income['payment_method']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Payment Date</span>
                <span class="value"><?php echo date('d M Y', strtotime($income['payment_date'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Reference No</span>
                <span class="value"><?php echo $income['reference_no'] ?: 'N/A'; ?></span>
            </div>
            <?php if ($income['invoice_id']): ?>
            <div class="info-item">
                <span class="label">Invoice</span>
                <span class="value">
                    <a href="view-invoice.php?id=<?php echo $income['invoice_id']; ?>">
                        View Invoice
                    </a>
                </span>
            </div>
            <?php endif; ?>
            <?php if ($income['order_id']): ?>
            <div class="info-item">
                <span class="label">Order</span>
                <span class="value">
                    <a href="order-details.php?id=<?php echo $income['order_id']; ?>">
                        View Order
                    </a>
                </span>
            </div>
            <?php endif; ?>
            <?php if ($income['note']): ?>
            <div class="info-item" style="grid-column: 1 / -1;">
                <span class="label">Note</span>
                <span class="value"><?php echo nl2br(htmlspecialchars($income['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($income['created_at'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Received By</span>
                <span class="value"><?php echo $income['received_by'] ? 'Admin' : 'System'; ?></span>
            </div>
        </div>

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

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

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