<?php
// alixdeal.shop/check_status.php (FULL CODE)
error_reporting(0);
ini_set('display_errors', 0);

// 1. Config लोड करना (return array format)
if (file_exists("app/config.php")) {
    $config_data = include "app/config.php"; 
} else {
    die("Error: Config file missing");
}

// 2. Database Connection
try {
    $db_info = $config_data['db'];
    $conn = new PDO("mysql:host=".$db_info['host'].";dbname=".$db_info['name'].";charset=utf8", $db_info['user'], $db_info['pass']);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database Connection Failed");
}

// 3. Domain का नाम साफ़ करना
$domain = isset($_GET['domain']) ? trim($_GET['domain']) : '';
$domain = str_replace(["http://", "https://", "www."], "", $domain);

if(!empty($domain)){
    // 4. डेटाबेस में स्टेटस और एक्सपायरी ढूंढना
    $query = $conn->prepare("SELECT child_panel_status, renewal_date FROM childpanels WHERE domain LIKE :domain LIMIT 1");
    $query->execute(['domain' => "%$domain%"]);
    $result = $query->fetch(PDO::FETCH_ASSOC);

    if($result) {
        // समय की गणना (Days Left)
        $expiry = new DateTime($result['renewal_date']);
        $now = new DateTime("now");
        $diff = $now->diff($expiry);
        $days = (int)$diff->format('%R%a');

        // अगर 'info=all' माँगा है (सैम के पैनल के लिए) तो JSON भेजें
        if(isset($_GET['info']) && $_GET['info'] == 'all') {
            header('Content-Type: application/json');
            echo json_encode([
                'status' => $result['child_panel_status'],
                'expiry_date' => date("d M Y", strtotime($result['renewal_date'])),
                'days_left' => ($days > 0) ? $days : 0
            ]);
        } else {
            // पुराने सिस्टम या सिंपल चेक के लिए सिर्फ स्टेटस
            echo trim($result['child_panel_status']); 
        }
    } else {
        echo "NotFound";
    }
} else {
    echo "NoDomainProvided";
}
?>