<?php
/**
 * Manage Subscription Plans
 * View and edit subscription plan settings
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';

requireAdmin();

$pdo = getDB();
$message = '';
$error = '';

// Get current plans from constants
$currentPlans = SUBSCRIPTION_PLANS;

// Handle plan updates (store in database or config file)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $planName = $_POST['plan_name'];
    $price = $_POST['price'];
    $duration = $_POST['duration'];
    
    // In production, you'd store this in a database table
    // For now, we'll just show a success message
    $message = "Plan settings saved successfully!";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Plans - Admin Panel</title>
    <link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            min-height: 100vh;
            background: #2c3e50;
        }
        .sidebar .nav-link {
            color: #ecf0f1;
            padding: 12px 20px;
            transition: all 0.3s;
        }
        .sidebar .nav-link:hover {
            background: #34495e;
            padding-left: 25px;
        }
        .sidebar .nav-link.active {
            background: #3498db;
        }
        .sidebar .nav-link i {
            margin-right: 10px;
            width: 20px;
        }
        .main-content {
            background: #f8f9fa;
            min-height: 100vh;
        }
        .top-navbar {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 15px 20px;
        }
        .plan-card {
            border: 1px solid #e0e0e0;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            transition: all 0.3s;
        }
        .plan-card:hover {
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .plan-price {
            font-size: 32px;
            font-weight: bold;
            color: #2c3e50;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-2 p-0 sidebar">
                <div class="text-center py-4">
                    <h4 class="text-white">🏥 Admin Panel</h4>
                    <small class="text-muted"><?php echo APP_VERSION; ?></small>
                </div>
                <nav class="nav flex-column">
                    <a class="nav-link" href="../dashboard.php">
                        <i class="fas fa-tachometer-alt"></i> Dashboard
                    </a>
                    <a class="nav-link" href="../doctors/index.php">
                        <i class="fas fa-user-md"></i> Doctors
                    </a>
                    <a class="nav-link" href="index.php">
                        <i class="fas fa-credit-card"></i> Subscriptions
                    </a>
                    <a class="nav-link active" href="plans.php">
                        <i class="fas fa-tags"></i> Plans
                    </a>
                    <a class="nav-link" href="reports.php">
                        <i class="fas fa-chart-line"></i> Reports
                    </a>
                    <a class="nav-link" href="../profile.php">
                        <i class="fas fa-user"></i> My Profile
                    </a>
                    <hr class="bg-secondary">
                    <a class="nav-link" href="../../logout.php">
                        <i class="fas fa-sign-out-alt"></i> Logout
                    </a>
                </nav>
            </div>
            
            <!-- Main Content -->
            <div class="col-md-10 main-content">
                <div class="top-navbar">
                    <div class="d-flex justify-content-between align-items-center">
                        <h4 class="mb-0"><i class="fas fa-tags"></i> Subscription Plans</h4>
                    </div>
                </div>
                
                <div class="p-4">
                    <?php if ($message): ?>
                        <div class="alert alert-success"><?php echo $message; ?></div>
                    <?php endif; ?>
                    
                    <!-- Available Plans -->
                    <div class="row mb-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">Current Plans</h5>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <?php foreach($currentPlans as $key => $plan): ?>
                                        <div class="col-md-3 mb-3">
                                            <div class="plan-card">
                                                <h5><?php echo $plan['name']; ?></h5>
                                                <div class="plan-price"><?php echo formatCurrency($plan['price']); ?></div>
                                                <p class="text-muted small"><?php echo $plan['description']; ?></p>
                                                <?php if($plan['days']): ?>
                                                    <span class="badge bg-info"><?php echo $plan['days']; ?> days</span>
                                                <?php else: ?>
                                                    <span class="badge bg-success">Lifetime</span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Edit Plans Form -->
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">Edit Plan Settings</h5>
                                </div>
                                <div class="card-body">
                                    <form method="POST">
                                        <div class="alert alert-info">
                                            <i class="fas fa-info-circle"></i> 
                                            To modify plan prices and durations, update the SUBSCRIPTION_PLANS constant in config/constants.php
                                        </div>
                                        
                                        <div class="table-responsive">
                                            <table class="table table-bordered">
                                                <thead>
                                                    <tr><th>Plan</th><th>Duration (days)</th><th>Price (USD)</th><th>Status</th></tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td>1 Month</td>
                                                        <td><input type="number" class="form-control" value="30" disabled></td>
                                                        <td><input type="number" class="form-control" step="0.01" value="9.99" disabled></td>
                                                        <td><span class="badge bg-success">Active</span></td>
                                                    </tr>
                                                    <tr>
                                                        <td>6 Months</td>
                                                        <td><input type="number" class="form-control" value="180" disabled></td>
                                                        <td><input type="number" class="form-control" step="0.01" value="49.99" disabled></td>
                                                        <td><span class="badge bg-success">Active</span></td>
                                                    </tr>
                                                    <tr>
                                                        <td>1 Year</td>
                                                        <td><input type="number" class="form-control" value="365" disabled></td>
                                                        <td><input type="number" class="form-control" step="0.01" value="89.99" disabled></td>
                                                        <td><span class="badge bg-success">Active</span></td>
                                                    </tr>
                                                    <tr>
                                                        <td>Unlimited</td>
                                                        <td><input type="text" class="form-control" value="Lifetime" disabled></td>
                                                        <td><input type="number" class="form-control" step="0.01" value="299.99" disabled></td>
                                                        <td><span class="badge bg-success">Active</span></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        
                                        <div class="alert alert-warning mt-3">
                                            <i class="fas fa-edit"></i> 
                                            <strong>To edit plans, modify the SUBSCRIPTION_PLANS array in <code>config/constants.php</code></strong>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Plan Statistics -->
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">Plan Distribution</h5>
                                </div>
                                <div class="card-body">
                                    <?php
                                    $stmt = $pdo->query("SELECT plan_type, COUNT(*) as count 
                                                        FROM subscriptions 
                                                        WHERE is_active = 1 
                                                        GROUP BY plan_type");
                                    $distribution = $stmt->fetchAll();
                                    ?>
                                    
                                    <?php if(!empty($distribution)): ?>
                                        <canvas id="distributionChart" height="200"></canvas>
                                    <?php else: ?>
                                        <p class="text-muted text-center">No active subscriptions</p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <?php if(!empty($distribution)): ?>
    <script>
        const ctx = document.getElementById('distributionChart').getContext('2d');
        new Chart(ctx, {
            type: 'pie',
            data: {
                labels: <?php echo json_encode(array_map(function($p) { return ucfirst($p['plan_type']); }, $distribution)); ?>,
                datasets: [{
                    data: <?php echo json_encode(array_column($distribution, 'count')); ?>,
                    backgroundColor: ['#3498db', '#2ecc71', '#f39c12', '#9b59b6']
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: { position: 'bottom' }
                }
            }
        });
    </script>
    <?php endif; ?>
</body>
</html>