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

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$payment_id = $_GET['id'] ?? '';

if (empty($payment_id)) {
    header('Location: list_payments.php');
    exit();
}

// Get payment details
$sql = "SELECT rp.*, l.lease_code, u.unit_number, p.property_name,
               t.first_name, t.last_name, t.email, t.phone,
               i.invoice_number
        FROM rent_payments rp
        JOIN leases l ON rp.lease_id = l.lease_id
        JOIN units u ON l.unit_id = u.unit_id
        JOIN properties p ON u.property_id = p.property_id
        JOIN lease_tenants lt ON l.lease_id = lt.lease_id AND lt.is_primary = 1
        JOIN tenants t ON lt.tenant_id = t.tenant_id
        LEFT JOIN invoices i ON rp.invoice_id = i.invoice_id
        WHERE rp.payment_id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $payment_id);
$stmt->execute();
$result = $stmt->get_result();
$payment = $result->fetch_assoc();

if (!$payment) {
    header('Location: list_payments.php');
    exit();
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Payment Details')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Payments', 'title' => 'Payment Details')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="list_payments.php" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Payments
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Payment Details</h4>
                                </div>
                                <div class="card-body">
                                    <div class="row mb-4">
                                        <div class="col-md-6">
                                            <h6 class="text-muted mb-3">Payment Information</h6>
                                            <p class="mb-2"><strong>Reference:</strong> <?php echo htmlspecialchars($payment['payment_reference']); ?></p>
                                            <p class="mb-2"><strong>Date:</strong> <?php echo date('M d, Y', strtotime($payment['payment_date'])); ?></p>
                                            <p class="mb-2"><strong>Amount:</strong> 
                                                <span class="fw-semibold text-success">KES <?php echo number_format($payment['amount'], 2); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Method:</strong> 
                                                <span class="badge bg-soft-info"><?php echo htmlspecialchars($payment['payment_method']); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Status:</strong> 
                                                <span class="badge bg-soft-<?php echo $payment['payment_status'] == 'COMPLETED' ? 'success' : 'warning'; ?>">
                                                    <?php echo htmlspecialchars($payment['payment_status']); ?>
                                                </span>
                                            </p>
                                        </div>
                                        <div class="col-md-6">
                                            <h6 class="text-muted mb-3">Related Information</h6>
                                            <p class="mb-2"><strong>Property:</strong> <?php echo htmlspecialchars($payment['property_name']); ?></p>
                                            <p class="mb-2"><strong>Unit:</strong> <?php echo htmlspecialchars($payment['unit_number']); ?></p>
                                            <p class="mb-2"><strong>Lease:</strong> 
                                                <a href="lease_details.php?id=<?php echo $payment['lease_id']; ?>">
                                                    <?php echo htmlspecialchars($payment['lease_code']); ?>
                                                </a>
                                            </p>
                                            <?php if ($payment['invoice_number']): ?>
                                                <p class="mb-2"><strong>Invoice:</strong> 
                                                    <a href="invoice_details.php?id=<?php echo $payment['invoice_id']; ?>">
                                                        <?php echo htmlspecialchars($payment['invoice_number']); ?>
                                                    </a>
                                                </p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <div class="mb-4">
                                        <h6 class="text-muted mb-3">Payer Information</h6>
                                        <p class="mb-1"><strong><?php echo htmlspecialchars($payment['first_name'] . ' ' . $payment['last_name']); ?></strong></p>
                                        <p class="mb-1"><?php echo htmlspecialchars($payment['email']); ?></p>
                                        <p class="mb-1"><?php echo htmlspecialchars($payment['phone']); ?></p>
                                    </div>

                                    <?php if ($payment['notes']): ?>
                                        <div class="mb-4">
                                            <h6 class="text-muted mb-2">Notes</h6>
                                            <p class="text-muted"><?php echo nl2br(htmlspecialchars($payment['notes'])); ?></p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Quick Actions</h4>
                                </div>
                                <div class="card-body">
                                    <?php if ($payment['invoice_id']): ?>
                                        <a href="invoice_details.php?id=<?php echo $payment['invoice_id']; ?>" class="btn btn-info w-100 mb-2">
                                            <i class="ri-receipt-line me-1"></i>View Invoice
                                        </a>
                                    <?php endif; ?>
                                    <a href="lease_details.php?id=<?php echo $payment['lease_id']; ?>" class="btn btn-secondary w-100 mb-2">
                                        <i class="ri-file-text-line me-1"></i>View Lease
                                    </a>
                                    <a href="tenant_details.php?id=<?php echo $payment['tenant_id'] ?? ''; ?>" class="btn btn-primary w-100">
                                        <i class="ri-user-line me-1"></i>View Tenant
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

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

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
