// Refill Status Check logic (Optimized)
foreach ($refills as $refill) {
    if ($refill["check_refill_status"] == 2) {
        $task_id = $refill["task_id"];
        $refill_id = $refill["refill_orderid"];

        // Ek hi query kaafi hai
        $order_query = $conn->prepare("SELECT * FROM orders 
            INNER JOIN services ON services.service_id = orders.service_id 
            INNER JOIN service_api ON services.service_api = service_api.id 
            WHERE orders.order_id=:id");
        $order_query->execute(array("id" => $refill['order_id']));
        $order = $order_query->fetch(PDO::FETCH_ASSOC);

        if ($order) {
            $api_url = $order["api_url"];
            
            // Standard API Refill Check
            if (get_domain($api_url) != "smqmteamindia.com") {
                $response = $smmapi->action(array(
                    'key' => $order["api_key"],
                    'action' => 'refill_status',
                    'refill' => $refill_id
                ), $api_url);
                $status = $response->status;
            }

            // Status Update Logic
            if ($status == "Rejected") {
                $task_status = "rejected"; $check = 1;
            } elseif ($status == "Completed" || $status == "Success") {
                $task_status = "completed"; $check = 1;
            } else {
                $task_status = "inprogress"; $check = 2;
            }

            $update = $conn->prepare("UPDATE tasks SET task_status=:status, task_updated_at=:updated_at, check_refill_status=:check WHERE task_id=:id");
            $update->execute(array(
                "status" => $task_status,
                "updated_at" => date('Y.m.d H:i:s'),
                "check" => $check,
                "id" => $task_id
            ));
        }
    }
}