<?php
require_once '../connection.php';

if (!isAdmin()) {
    header('Location: ../login.php');
    exit();
}

$client_id = $_GET['id'] ?? 0;

// Get client details
$stmt = $pdo->prepare("
    SELECT c.*, s.end_date as expiry_date, p.name as plan_name, p.speed_mbps 
    FROM clients c 
    LEFT JOIN subscriptions s ON c.id = s.client_id AND s.is_active = 1 
    LEFT JOIN plans p ON s.plan_id = p.id
    WHERE c.id = ?
");
$stmt->execute([$client_id]);
$client = $stmt->fetch();

if (!$client) {
    header('Location: dashboard.php');
    exit();
}

// Get transaction history
$stmt = $pdo->prepare("SELECT * FROM transactions WHERE client_id = ? ORDER BY created_at DESC");
$stmt->execute([$client_id]);
$transactions = $stmt->fetchAll();

// Get subscription history
$stmt = $pdo->prepare("
    SELECT s.*, p.name, p.speed_mbps, p.price 
    FROM subscriptions s 
    JOIN plans p ON s.plan_id = p.id 
    WHERE s.client_id = ? 
    ORDER BY s.created_at DESC
");
$stmt->execute([$client_id]);
$subscriptions = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <title>View Client - <?php echo htmlspecialchars($client['full_name']); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-dark bg-dark">
        <div class="container-fluid">
            <span class="navbar-brand">Client Details: <?php echo htmlspecialchars($client['full_name']); ?></span>
            <div>
                <a href="dashboard.php" class="btn btn-outline-light btn-sm">Back to Dashboard</a>
                <a href="../logout.php" class="btn btn-outline-light btn-sm">Logout</a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-info text-white">
                        <h5>Client Information</h5>
                    </div>
                    <div class="card-body">
                        <table class="table">
                            <tr>
                                <th>Username:</th>
                                <td><?php echo htmlspecialchars($client['username']); ?></td>
                            </tr>
                            <tr>
                                <th>Full Name:</th>
                                <td><?php echo htmlspecialchars($client['full_name']); ?></td>
                            </tr>
                            <tr>
                                <th>Email:</th>
                                <td><?php echo htmlspecialchars($client['email']); ?></td>
                            </tr>
                            <tr>
                                <th>Phone:</th>
                                <td><?php echo htmlspecialchars($client['phone']); ?></td>
                            </tr>
                            <tr>
                                <th>PPPoE Username:</th>
                                <td><code><?php echo htmlspecialchars($client['pppoe_username']); ?></code></td>
                            </tr>
                            <tr>
                                <th>Wallet Balance:</th>
                                <td><strong>$<?php echo number_format($client['wallet_balance'], 2); ?></strong></td>
                            </tr>
                            <tr>
                                <th>Auto-Debit:</th>
                                <td><?php echo $client['auto_debit'] ? '<span class="badge bg-success">Enabled</span>' : '<span class="badge bg-secondary">Disabled</span>'; ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5>Current Subscription</h5>
                    </div>
                    <div class="card-body">
                        <?php if($client['plan_name']): ?>
                            <table class="table">
                                <tr>
                                    <th>Plan:</th>
                                    <td><?php echo $client['plan_name']; ?> (<?php echo $client['speed_mbps']; ?> Mbps)</td>
                                </tr>
                                <tr>
                                    <th>Expiry Date:</th>
                                    <td class="<?php echo strtotime($client['expiry_date']) < time() ? 'text-danger' : 'text-success'; ?>">
                                        <?php echo date('F j, Y', strtotime($client['expiry_date'])); ?>
                                    </td>
                                </tr>
                                <tr>
                                    <th>Status:</th>
                                    <td>
                                        <?php if(strtotime($client['expiry_date']) < time()): ?>
                                            <span class="badge bg-danger">Expired</span>
                                        <?php else: ?>
                                            <span class="badge bg-success">Active</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            </table>
                        <?php else: ?>
                            <p class="text-danger">No active subscription</p>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="card mt-3">
                    <div class="card-header">
                        <h5>Quick Actions</h5>
                    </div>
                    <div class="card-body">
                        <button class="btn btn-primary" onclick="addFunds(<?php echo $client['id']; ?>)">Add Funds to Wallet</button>
                        <button class="btn btn-warning" onclick="resetPPPoEPassword(<?php echo $client['id']; ?>)">Reset PPPoE Password</button>
                        <button class="btn btn-danger" onclick="disableClient(<?php echo $client['id']; ?>)">Disable Client</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-3">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>Subscription History</h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-bordered">
                            <thead>
                                <tr><th>Plan</th><th>Start Date</th><th>End Date</th><th>Status</th><th>Purchased On</th></tr>
                            </thead>
                            <tbody>
                                <?php foreach($subscriptions as $sub): ?>
                                <tr>
                                    <td><?php echo $sub['name']; ?> (<?php echo $sub['speed_mbps']; ?> Mbps)</td>
                                    <td><?php echo date('M d, Y', strtotime($sub['start_date'])); ?></td>
                                    <td><?php echo date('M d, Y', strtotime($sub['end_date'])); ?></td>
                                    <td><?php echo $sub['is_active'] ? '<span class="badge bg-success">Active</span>' : '<span class="badge bg-secondary">Expired</span>'; ?></td>
                                    <td><?php echo date('M d, Y H:i', strtotime($sub['created_at'])); ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="card mt-3">
                    <div class="card-header">
                        <h5>Transaction History</h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-bordered">
                            <thead>
                                <tr><th>Date</th><th>Type</th><th>Amount</th><th>Reference</th><th>Status</th></tr>
                            </thead>
                            <tbody>
                                <?php foreach($transactions as $trans): ?>
                                <tr>
                                    <td><?php echo date('M d, Y H:i', strtotime($trans['created_at'])); ?></td>
                                    <td><?php echo str_replace('_', ' ', ucfirst($trans['type'])); ?></td>
                                    <td>$<?php echo number_format($trans['amount'], 2); ?></td>
                                    <td><small><?php echo $trans['reference']; ?></small></td>
                                    <td><span class="badge bg-success"><?php echo $trans['status']; ?></span></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
    function addFunds(clientId) {
        let amount = prompt("Enter amount to add:");
        if (amount && !isNaN(amount) && amount > 0) {
            window.location.href = `admin_add_funds.php?id=${clientId}&amount=${amount}`;
        }
    }
    
    function resetPPPoEPassword(clientId) {
        if (confirm("Are you sure you want to reset PPPoE password?")) {
            window.location.href = `reset_pppoe_password.php?id=${clientId}`;
        }
    }
    
    function disableClient(clientId) {
        if (confirm("Are you sure you want to disable this client?")) {
            window.location.href = `disable_client.php?id=${clientId}`;
        }
    }
    </script>
</body>
</html>