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

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

// Handle payment verification
if (isset($_GET['verify']) && isset($_GET['order_id'])) {
    $order_id = (int)$_GET['order_id'];
    $admin_id = 1; // hardcoded admin id, or use session
    $conn->query("UPDATE orders SET payment_status = 'paid', payment_verified_at = NOW(), payment_verified_by = $admin_id WHERE id = $order_id");
    
    // Fetch order and customer details
    $order = $conn->query("SELECT * FROM orders WHERE id = $order_id")->fetch_assoc();
    if ($order) {
        $customer_phone = $order['customer_phone'];
        $order_number = $order['order_number'];
        $amount = number_format($order['total_amount'], 2);
        $msg = "✅ আপনার অর্ডার #$order_number এর পেমেন্ট নিশ্চিত করা হয়েছে। ধন্যবাদ – Dr. Ferdous";
        if (function_exists('sendSMSViaBulkSMSBD')) {
            sendSMSViaBulkSMSBD($customer_phone, $msg);
        }
        // Also notify admin
        sendSMSViaBulkSMSBD('01681519799', "✅ Payment verified for order $order_number");
    }
    header('Location: orders.php');
    exit;
}

// Fetch all orders
$orders = $conn->query("SELECT * FROM orders ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Orders | Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { background: #f0f2f5; }
        .container { margin-top: 30px; }
        .card { border-radius: 15px; box-shadow: 0 5px 15px rgba(0,0,0,0.08); }
        .badge-paid { background: #28a745; color: #fff; }
        .badge-unpaid { background: #dc3545; color: #fff; }
        .badge-pending { background: #ffc107; color: #000; }
    </style>
</head>
<body>
<div class="container">
    <div class="card">
        <div class="card-header bg-primary text-white d-flex justify-content-between">
            <h4 class="mb-0"><i class="fas fa-box"></i> Orders</h4>
            <a href="admin.php" class="btn btn-light">Back to Dashboard</a>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Order #</th>
                            <th>Customer</th>
                            <th>Phone</th>
                            <th>Amount</th>
                            <th>Payment Method</th>
                            <th>Transaction ID</th>
                            <th>Status</th>
                            <th>Payment Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php while ($row = $orders->fetch_assoc()): ?>
                        <tr>
                            <td><?= $row['id'] ?></td>
                            <td><strong><?= $row['order_number'] ?></strong></td>
                            <td><?= htmlspecialchars($row['customer_name']) ?></td>
                            <td><?= $row['customer_phone'] ?></td>
                            <td>৳<?= number_format($row['total_amount'], 2) ?></td>
                            <td><?= strtoupper($row['payment_method']) ?></td>
                            <td><?= htmlspecialchars($row['transaction_id'] ?: '—') ?></td>
                            <td>
                                <?php
                                $status_class = 'badge-pending';
                                if ($row['status'] == 'approved') $status_class = 'badge-paid';
                                elseif ($row['status'] == 'delivered') $status_class = 'badge-paid';
                                ?>
                                <span class="badge <?= $status_class ?>"><?= ucfirst($row['status']) ?></span>
                            </td>
                            <td>
                                <?php if ($row['payment_status'] == 'paid'): ?>
                                    <span class="badge badge-paid">Paid</span>
                                <?php else: ?>
                                    <span class="badge badge-unpaid">Unpaid</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($row['payment_status'] == 'unpaid' && $row['payment_method'] != 'cod' && !empty($row['transaction_id'])): ?>
                                    <a href="?verify=1&order_id=<?= $row['id'] ?>" class="btn btn-success btn-sm" onclick="return confirm('Verify payment for order <?= $row['order_number'] ?>?')">
                                        <i class="fas fa-check"></i> Verify Payment
                                    </a>
                                <?php else: ?>
                                    <span class="text-muted">—</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>