<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

require_once __DIR__.'/db_connect.php';

$code = trim($_GET['code'] ?? '');

if ($code === '') {
    echo json_encode(['success'=>false,'message'=>'code manquant'], JSON_UNESCAPED_UNICODE);
    exit;
}

try {
    $sql = "SELECT arcodebarre, ardesignation 
            FROM tblarticles 
            WHERE arcode = :code 
            LIMIT 1";

    $stmt = $pdo->prepare($sql);
    $stmt->bindValue(':code', $code);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($row) {
        echo json_encode([
            'success' => true,
            'data' => [
                'arcodebarre' => $row['arcodebarre'],
                'ardesignation' => $row['ardesignation']
            ]
        ], JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Article introuvable'
        ], JSON_UNESCAPED_UNICODE);
    }

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success'=>false,'message'=>$e->getMessage()], JSON_UNESCAPED_UNICODE);
}