<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "partner") {
    header("Location: ../auth/login.php");
    exit;
}

$stmt = $pdo->prepare("SELECT * FROM partner_shops WHERE user_id = ? LIMIT 1");
$stmt->execute([$_SESSION["user_id"]]);
$shop = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$shop) {
    die("No partner shop connected.");
}

$pdo->prepare("
    UPDATE transactions
    SET partner_read_at = NOW()
    WHERE to_type = 'partner'
    AND to_id = ?
    AND from_type = 'customer'
    AND partner_read_at IS NULL
")->execute([$shop["id"]]);

$stmt = $pdo->prepare("
    SELECT t.*, c.full_name, c.phone
    FROM transactions t
    LEFT JOIN customers c ON c.id = t.from_id
    WHERE t.to_type = 'partner'
    AND t.to_id = ?
    AND t.from_type = 'customer'
    ORDER BY t.created_at DESC
");
$stmt->execute([$shop["id"]]);
$payments = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Customer Payments</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f8;
            margin: 0;
            padding: 30px;
        }

        .container {
            max-width: 1000px;
            margin: auto;
        }

        .header {
            background: #111827;
            color: white;
            padding: 28px;
            border-radius: 18px;
            margin-bottom: 24px;
        }

        .header h1 {
            margin: 0;
        }

        .card {
            background: white;
            border-radius: 16px;
            padding: 22px;
            box-shadow: 0 4px 14px rgba(0,0,0,0.08);
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 14px;
            border-bottom: 1px solid #e5e7eb;
            text-align: left;
        }

        th {
            color: #374151;
            font-size: 14px;
        }

        .amount {
            color: #16a34a;
            font-weight: bold;
        }

        .btn {
            display: inline-block;
            margin-top: 18px;
            background: #2563eb;
            color: white;
            padding: 12px 16px;
            border-radius: 10px;
            text-decoration: none;
        }

        .empty {
            color: #6b7280;
            text-align: center;
            padding: 30px;
        }
    </style>
</head>
<body>

<div class="container">

    <div class="header">
        <h1>Customer Payments</h1>
        <p><?php echo htmlspecialchars($shop["shop_name"]); ?></p>
    </div>

    <div class="card">
        <?php if (count($payments) > 0): ?>
            <table>
                <tr>
                    <th>Date</th>
                    <th>Customer</th>
                    <th>Phone</th>
                    <th>Amount</th>
                    <th>Transaction</th>
                </tr>

                <?php foreach ($payments as $payment): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($payment["created_at"]); ?></td>
                        <td><?php echo htmlspecialchars($payment["full_name"] ?? "Customer"); ?></td>
                        <td><?php echo htmlspecialchars($payment["phone"] ?? "-"); ?></td>
                        <td class="amount"><?php echo number_format($payment["amount"], 2); ?></td>
                        <td><?php echo htmlspecialchars($payment["transaction_code"]); ?></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        <?php else: ?>
            <div class="empty">No customer payments yet.</div>
        <?php endif; ?>
    </div>

    <a class="btn" href="dashboard.php">Back to Dashboard</a>

</div>

</body>
</html>