<?php
/**
 * Special API for QR Code Generator
 * This file connects directly to the database and returns bus data
 * 
 * Upload this file to: public_html/api.transub.dz/apiboumerdes/apipointage/
 * 
 * Database: transubd_PointageBus
 * Table: buses
 */

// Database configuration - UPDATE THESE VALUES
$dbHost = 'localhost'; // Usually 'localhost' for cPanel
$dbName = 'transubd_PointageBus';
$dbUser = 'transubd_transub_user';
$dbPass = 'Tr@nsub2@24';

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Content-Type: application/json; charset=UTF-8");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

try {
    // Connect to database
    $conn = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
     // Get all active buses with qr_code
     $query = "SELECT id, matricule, qr_code, status
               FROM buses
               WHERE status = 'Active'
               AND qr_code IS NOT NULL
               AND qr_code != ''
               ORDER BY matricule ASC";
    
    $stmt = $conn->prepare($query);
    $stmt->execute();
    
    $buses = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'data' => $buses,
        'count' => count($buses)
    ]);
    
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'Database error: ' . $e->getMessage()
    ]);
}
?>
