<?php
session_start();

// التحقق من تسجيل الدخول
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

require_once 'db_connect.php';

$item = null;
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['barcode'])) {
    $barcode = $_POST['barcode'] ?? '';

    try {
        $stmt = $conn->prepare("SELECT name, retail_price FROM items WHERE barcode = :barcode LIMIT 1");
        $stmt->execute([':barcode' => $barcode]);
        $item = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$item) {
            $error_message = "لم يتم العثور على الصنف!";
        }
    } catch (PDOException $e) {
        error_log("Error searching item: " . $e->getMessage());
        $error_message = "حدث خطأ أثناء البحث: " . $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Astra - بحث عن الصنف</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            direction: rtl;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #2ECC71, #3498DB, #E74C3C);
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            overflow-x: hidden;
        }
        .container-fluid {
            padding: 0;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: stretch; /* لملء العرض الكامل */
        }
        .content {
            width: 100%; /* ملء العرض الكامل */
            padding: 20px;
        }
        h2 {
            color: #2ECC71;
            font-weight: bold;
            text-align: center;
            margin-bottom: 30px;
            text-shadow: 0 0 8px rgba(46, 204, 113, 0.7);
            font-size: 2.5rem;
        }
        .form-label {
            color: #3498DB;
            font-weight: bold;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            font-size: 1.5rem;
        }
        .form-label i {
            margin-left: 15px;
            color: #2ECC71;
        }
        .form-control {
            border-radius: 15px;
            border: 3px solid #2ECC71;
            padding: 12px 20px;
            background: rgba(255, 255, 255, 0.9);
            transition: all 0.3s ease;
            box-shadow: inset 0 5px 10px rgba(0, 0, 0, 0.1);
            font-size: 1.5rem;
            width: 100%; /* ملء العرض الكامل */
        }
        .form-control:focus {
            border-color: #3498DB;
            box-shadow: 0 0 15px rgba(52, 152, 219, 0.7), inset 0 5px 10px rgba(0, 0, 0, 0.1);
            background: #fff;
        }
        .btn-primary {
            background: linear-gradient(45deg, #2ECC71, #27AE60);
            border: none;
            color: #fff;
            border-radius: 15px;
            padding: 12px 25px;
            font-weight: bold;
            transition: all 0.4s ease;
            font-size: 1.5rem;
            width: 100%; /* ملء العرض الكامل */
            margin-top: 15px;
        }
        .btn-primary:hover {
            background: linear-gradient(45deg, #27AE60, #219653);
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(46, 204, 113, 0.7);
        }
        .result {
            margin-top: 30px;
            text-align: center;
            color: #2ECC71;
            font-weight: bold;
            font-size: 2rem;
            padding: 20px;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
            animation: fadeInOut 5s ease-in-out;
            display: none;
            width: 100%; /* ملء العرض الكامل */
        }
        @keyframes fadeInOut {
            0% { opacity: 0; }
            20% { opacity: 1; }
            80% { opacity: 1; }
            100% { opacity: 0; display: none; }
        }
        .error-message {
            color: #E74C3C;
            font-weight: bold;
            text-align: center;
            margin-top: 30px;
            font-size: 1.8rem;
            animation: fadeIn 0.5s ease-in-out;
            width: 100%; /* ملء العرض الكامل */
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="content">
            <h2><i class="fas fa-search"></i> بحث عن الصنف</h2>
            <form method="POST">
                <div class="mb-4">
                    <label class="form-label"><i class="fas fa-barcode"></i> الباركود</label>
                    <input type="text" class="form-control" name="barcode" id="barcodeInput" placeholder="ادخل الباركود" autofocus required>
                </div>
                <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> بحث</button>
            </form>

            <?php if (isset($item)): ?>
                <div class="result" id="result">
                    اسم الصنف: <?php echo htmlspecialchars($item['name']); ?><br>
                    السعر القطاعي: <?php echo number_format($item['retail_price'], 2); ?> ج.م
                </div>
            <?php elseif (isset($error_message)): ?>
                <div class="error-message"><?php echo $error_message; ?></div>
            <?php endif; ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const result = document.getElementById('result');
            if (result) {
                result.style.display = 'block';
                setTimeout(() => {
                    result.style.display = 'none';
                }, 5000); // اختفاء بعد 5 ثانيات
            }

            document.getElementById('barcodeInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    e.preventDefault();
                    document.querySelector('form').submit();
                }
            });
        });
    </script>
</body>
</html>