<?php
// init.php ko shaamil karein
require_once 'init.php';

// Suraksha jaanch
if (!userHasRole(['Admin', 'Manager'])) {
    header("Location: dashboard.php");
    exit();
}

$output_message = '';

// --- Manually script chalaane ka logic ---
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['generate_reports'])) {
    ob_start(); // Output ko buffer karna shuru karein

    // create_report_tasks.php script ko shaamil karein aur chalaayein
    include 'create_report_tasks.php';

    $output_message = ob_get_clean(); // Buffer se output prapt karein
}

require_once 'header.php';
?>

<title>Report Task Generator - Digify OS</title>
<h2 class="text-xl font-bold text-gray-800">Report Task Generator</h2>

<main class="p-6 md:p-10">
    <div class="max-w-2xl mx-auto bg-white p-8 rounded-lg shadow-lg">
        <h3 class="text-xl font-semibold mb-4">Generate 10-Day Report Tasks</h3>
        <p class="text-gray-600 mb-6">
            Use this tool to manually trigger the 10-day report task generation. The script will check all active clients and create a report task if it is due. It will not create duplicate tasks.
        </p>
        
        <form method="POST" action="report_generator.php">
            <div class="flex justify-center">
                <button type="submit" name="generate_reports" class="px-8 py-3 bg-green-600 text-white font-bold rounded-lg shadow-md hover:bg-green-700">
                    Generate Report Tasks Now
                </button>
            </div>
        </form>

        <?php if (!empty($output_message)): ?>
            <div class="mt-8 p-4 bg-gray-50 border rounded">
                <h4 class="font-semibold mb-2">Execution Log:</h4>
                <pre class="text-sm text-gray-700 whitespace-pre-wrap"><?php echo htmlspecialchars($output_message); ?></pre>
            </div>
        <?php endif; ?>
    </div>
</main>

<?php
// फुटर को शामिल करें
require_once 'footer.php';
?>