<?php
/**
 * AlixDeal Admin Notification - Restarted Edition
 */

// --- 1. DATABASE CONNECTION ---
$db_host = 'localhost';
$db_name = 'alixdeal_ads1'; 
$db_user = 'alixdeal_ads1'; 
$db_pass = 'alixdeal_ads1'; 

try {
    $conn_admin = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8mb4", $db_user, $db_pass);
    $conn_admin->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    error_log("Admin Notify DB Error: " . $e->getMessage());
}

// --- 2. THE ADMIN NOTIFICATION FUNCTION ---
function send_admin_order_alert($order_id) {
    global $conn_admin;
    
    // MPWA Settings
    $api_key   = "o6YBBpdG7dnLQgT5ZQX7YDtcyggBto"; 
    $sender_no = "1234567890"; 
    $api_url   = "https://ads.alixdeal.info/send-message"; 
    $admin_no  = "918273147063"; // Lucky Bhai

    try {
        // Database se fresh detail uthana
        $stmt = $conn_admin->prepare("SELECT o.order_id, o.order_charge, o.order_quantity, o.order_profit, c.username, s.service_name 
                                FROM orders o 
                                JOIN clients c ON o.client_id = c.client_id 
                                JOIN services s ON o.service_id = s.service_id 
                                WHERE o.order_id = :id");
        $stmt->execute(['id' => $order_id]);
        $order = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$order) return "Order not found";

        // Message Format (Pro Style)
        $text = "🔔 *NEW ORDER ALERT (ADMIN)* 🔔\n";
        $text .= "━━━━━━━━━━━━━━━\n";
        $text .= "🆔 *Order ID:* #" . $order['order_id'] . "\n";
        $text .= "👤 *Client:* " . $order['username'] . "\n";
        $text .= "📦 *Service:* " . $order['service_name'] . "\n";
        $text .= "🔢 *Qty:* " . $order['order_quantity'] . "\n";
        $text .= "━━━━━━━━━━━━━━━\n";
        $text .= "💸 *Charge:* ₹" . round($order['order_charge'], 2) . "\n";
        $text .= "💰 *Profit:* ₹" . round($order['order_profit'], 2) . "\n";
        $text .= "━━━━━━━━━━━━━━━\n";
        $text .= "🔥 Lucky bhai, panel check kar lo!";

        // Send API Request
        $data = ["api_key" => $api_key, "sender" => $sender_no, "number" => $admin_no, "message" => $text];
        $ch = curl_init($api_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;

    } catch (Exception $e) { 
        return "Error: " . $e->getMessage(); 
    }
}

// --- 3. DASHBOARD / MANUAL TRIGGER ---
if (basename($_SERVER['PHP_SELF']) == 'notify_admin.php') {
    $test_sent = false;
    if (isset($_POST['send_test'])) {
        $stmt = $conn_admin->query("SELECT order_id FROM orders ORDER BY order_id DESC LIMIT 1");
        $last = $stmt->fetch();
        if ($last) { send_admin_order_alert($last['order_id']); $test_sent = true; }
    }
?>
<!DOCTYPE html>
<html>
<head><title>Admin Alert</title><style>body{background:#0f172a;color:#fff;text-align:center;padding:50px;font-family:sans-serif;}.card{background:#1e293b;padding:20px;border-radius:10px;display:inline-block;}.btn{background:#3b82f6;color:#fff;padding:10px 20px;border:none;border-radius:5px;cursor:pointer;}</style></head>
<body>
    <div class="card">
        <h2>🛠️ Admin Alert System</h2>
        <form method="POST"><button type="submit" name="send_test" class="btn">🚀 Refresh & Test</button></form>
        <?php if ($test_sent) echo "<p style='color:#4ade80'>Alert Sent to Admin!</p>"; ?>
    </div>
</body>
</html>
<?php } ?>