<?php
// Atur judul halaman
$page_title = "Business Matching";

// Sertakan file koneksi database
require 'config/db.php';

// Sertakan file header
include 'templates/header.php';

// Inisialisasi variabel pencarian
$search_term = '';
$sql = "SELECT * FROM companies ORDER BY company_name ASC";

// Cek jika ada input pencarian dari form
if (isset($_GET['search']) && !empty(trim($_GET['search']))) {
    $search_term = trim($_GET['search']);
    
    // PERHATIKAN: Query diubah untuk mencari berdasarkan nama, sektor, atau deskripsi
    // Kita menggunakan PREPARED STATEMENT untuk keamanan (mencegah SQL Injection)
    $sql = "SELECT * FROM companies WHERE company_name LIKE ? OR sector LIKE ? OR description LIKE ? ORDER BY company_name ASC";
    
    $stmt = $conn->prepare($sql);
    $search_like = "%" . $search_term . "%";
    $stmt->bind_param("sss", $search_like, $search_like, $search_like);
    $stmt->execute();
    $result = $stmt->get_result();

} else {
    // Jika tidak ada pencarian, jalankan query biasa
    $result = $conn->query($sql);
}

?>

<main class="bg-gray-50 pt-16">
    <div class="bg-gradient-to-r from-blue-500 to-purple-500 text-white py-16">
        <div class="container mx-auto px-4 text-center">
            <h1 class="text-4xl font-bold">Direktori Perusahaan</h1>
            <p class="mt-2 text-lg">Temukan Mitra Strategis Potensial untuk Bisnis Anda.</p>
        </div>
    </div>

    <div class="container mx-auto px-4 py-12">
        
        <div class="mb-8 p-6 bg-white rounded-lg shadow-md">
            <form action="business_matching.php" method="GET" class="flex flex-col sm:flex-row gap-4">
                <input 
                    type="text" 
                    name="search" 
                    placeholder="Cari nama perusahaan, sektor, atau deskripsi..." 
                    class="w-full px-4 py-3 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"
                    value="<?php echo htmlspecialchars($search_term); ?>"
                >
                <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-6 rounded-md transition duration-300">
                    <i class="fas fa-search mr-2"></i>Cari
                </button>
            </form>
        </div>

        <?php if (!empty($search_term)): ?>
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">Hasil pencarian untuk: "<?php echo htmlspecialchars($search_term); ?>"</h2>
        <?php endif; ?>

        <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
            
            <?php if ($result && $result->num_rows > 0): ?>
                <?php while($company = $result->fetch_assoc()): ?>
                    <div class="bg-white rounded-lg shadow-lg overflow-hidden transform hover:-translate-y-2 transition-transform duration-300">
                        <div class="p-6">
                            <div class="flex items-center space-x-4 mb-4">
                                <img src="<?php echo htmlspecialchars($company['logo_url']); ?>" alt="Logo <?php echo htmlspecialchars($company['company_name']); ?>" class="w-16 h-16 rounded-full border-2 border-gray-100">
                                <div>
                                    <h3 class="text-xl font-bold text-gray-900"><?php echo htmlspecialchars($company['company_name']); ?></h3>
                                    <p class="text-sm text-blue-600 font-medium"><?php echo htmlspecialchars($company['sector']); ?></p>
                                </div>
                            </div>
                            <p class="text-gray-600 text-sm mb-4">
                                <?php echo htmlspecialchars($company['description']); ?>
                            </p>
                            <a href="#" class="w-full text-center block bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white font-bold py-2 px-4 rounded-md transition duration-300">
                                <i class="fas fa-paper-plane mr-2"></i>Hubungi
                            </a>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="text-center text-gray-500 col-span-full bg-yellow-50 p-8 rounded-lg">
                    <i class="fas fa-exclamation-triangle fa-3x text-yellow-400 mb-4"></i>
                    <p class="text-xl">Data tidak ditemukan.</p>
                    <?php if (!empty($search_term)): ?>
                        <p>Tidak ada perusahaan yang cocok dengan kata kunci "<?php echo htmlspecialchars($search_term); ?>".</p>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
            
        </div>
    </div>
</main>


<?php
// Sertakan file footer
include 'templates/footer.php';
if (isset($stmt)) $stmt->close(); // Tutup statement jika ada
$conn->close(); // Tutup koneksi database
?>