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

// Get PO payment details
$stmt = $pdo->prepare("SELECT pp.*, v.name as vendor_name, po.po_no 
                       FROM po_payments pp 
                       LEFT JOIN vendors v ON pp.vendor_id = v.id 
                       LEFT JOIN purchase_orders po ON pp.po_id = po.id 
                       WHERE pp.id = ?");
$stmt->execute([$id]);
$payment = $stmt->fetch();

if (!$payment) {
    header('Location: po-payments.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-warning { color: #f59e0b; }

.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-hand-holding-dollar me-2"></i> PO Payment Details</h2>
        <div>
            <a href="po-payments.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>
            PO Payment #<?php echo $payment['payment_no']; ?>
        </h5>

        <div class="info-grid">
            <div class="info-item">
                <span class="label">Payment Number</span>
                <span class="value"><code><?php echo $payment['payment_no']; ?></code></span>
            </div>
            <div class="info-item">
                <span class="label">PO Number</span>
                <span class="value">
                    <?php if ($payment['po_no']): ?>
                    <a href="view-po.php?id=<?php echo $payment['po_id']; ?>"><?php echo $payment['po_no']; ?></a>
                    <?php else: ?>
                    N/A
                    <?php endif; ?>
                </span>
            </div>
            <div class="info-item">
                <span class="label">Vendor</span>
                <span class="value"><?php echo htmlspecialchars($payment['vendor_name'] ?? 'N/A'); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Amount</span>
                <span class="value text-warning"><?php echo number_format($payment['amount']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Payment Method</span>
                <span class="value"><?php echo ucfirst($payment['payment_method']); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Payment Date</span>
                <span class="value"><?php echo date('d M Y', strtotime($payment['payment_date'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Reference No</span>
                <span class="value"><?php echo $payment['reference_no'] ?: 'N/A'; ?></span>
            </div>
            <?php if ($payment['note']): ?>
            <div class="info-item" style="grid-column: 1 / -1;">
                <span class="label">Note</span>
                <span class="value"><?php echo nl2br(htmlspecialchars($payment['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($payment['created_at'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Created By</span>
                <span class="value"><?php echo $payment['created_by'] ? 'Admin' : 'System'; ?></span>
            </div>
        </div>

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

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

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