<?php
include 'includes/db.php';

$msg = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars(trim($_POST['name'] ?? ''));
    $email = htmlspecialchars(trim($_POST['email'] ?? ''));
    $message = htmlspecialchars(trim($_POST['message'] ?? ''));

    if(!empty($name) && !empty($email) && !empty($message)){
        $stmt = $conn->prepare("INSERT INTO contact_messages (name, email, message) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $name, $email, $message);
        if ($stmt->execute()) {
            $msg = "<div class='alert alert-success'>Message sent successfully!</div>";
        } else {
            $msg = "<div class='alert alert-danger'>Error sending message.</div>";
        }
    } else {
        $msg = "<div class='alert alert-warning'>All fields are required.</div>";
    }
}

// Fetch Published Companies
$companies_query = $conn->query("SELECT * FROM companies WHERE status='Published' ORDER BY display_order ASC, created_at DESC");

// Fallback to legacy customers if companies table is empty
$legacy_customers = null;
if (!$companies_query || $companies_query->num_rows == 0) {
    $legacy_customers = $conn->query("SELECT * FROM customers WHERE status='active' ORDER BY display_order ASC, created_at DESC");
}

include 'includes/header.php';
?>

<!-- Include specialized company profile stylesheet -->
<link rel="stylesheet" href="/bm/assets/css/company-profile.css">



<!-- 1. Original Contact Us Form Section (100% Intact) -->
<div class="container" style="padding: 60px 20px 40px; max-width: 800px;">
    <h2 class="section-title" style="text-align: center; color: var(--dark-blue, #003366); font-size: 32px; font-weight: 700; margin-bottom: 25px;">Contact Us</h2>
    <?php echo $msg; ?>
    <form method="POST" action="" style="background: #fff; padding: 35px; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); border: 1px solid #eef2f6;">
        <div style="margin-bottom: 20px;">
            <label style="display:block; margin-bottom: 8px; font-weight: 600; color: #1e293b;">Name <span style="color: #ef4444;">*</span></label>
            <input type="text" name="name" style="width: 100%; padding: 12px 14px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 15px;" required placeholder="Your full name">
        </div>
        <div style="margin-bottom: 20px;">
            <label style="display:block; margin-bottom: 8px; font-weight: 600; color: #1e293b;">Email <span style="color: #ef4444;">*</span></label>
            <input type="email" name="email" style="width: 100%; padding: 12px 14px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 15px;" required placeholder="name@example.com">
        </div>
        <div style="margin-bottom: 25px;">
            <label style="display:block; margin-bottom: 8px; font-weight: 600; color: #1e293b;">Message <span style="color: #ef4444;">*</span></label>
            <textarea name="message" rows="5" style="width: 100%; padding: 12px 14px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 15px;" required placeholder="How can we assist you?"></textarea>
        </div>
        <button type="submit" style="background: var(--accent-orange, #ff6600); color: white; padding: 12px 32px; border: none; border-radius: 25px; cursor: pointer; font-size: 16px; font-weight: 700; transition: all 0.3s ease; box-shadow: 0 4px 12px rgba(255, 102, 0, 0.3);">
            <i class="fas fa-paper-plane" style="margin-right: 6px;"></i> Send Message
        </button>
    </form>
    
    <script>
        // Auto-hide alert after 3 seconds
        setTimeout(function() {
            let alert = document.querySelector('.alert');
            if (alert) {
                alert.style.transition = "opacity 0.5s ease";
                alert.style.opacity = "0";
                setTimeout(() => alert.style.display = "none", 500);
            }
        }, 3000);
    </script>
</div>

<!-- 2. MEET OUR CUSTOMERS Section (Below Contact Us) -->
<section class="section" style="padding: 60px 0 90px; background-color: #f8fafc; border-top: 1px solid #eef2f6;">
    <div class="container" style="max-width: 1200px; margin: 0 auto; padding: 0 20px;">
        <div style="text-align: center; max-width: 760px; margin: 0 auto 50px;">
            <h2 style="color: var(--dark-blue, #003366); font-size: 34px; font-weight: 800; margin-bottom: 12px; text-transform: uppercase; letter-spacing: 0.5px;">
                MEET OUR CUSTOMERS
            </h2>
            <div style="width: 70px; height: 3px; background: var(--accent-orange, #ff6600); margin: 0 auto 16px; border-radius: 2px;"></div>
            <p style="color: #64748b; font-size: 16px; line-height: 1.6;">
                We take immense pride in our corporate collaborations and valued customers. Explore our full partner company profiles, founders, directors and services below.
            </p>
        </div>

        <div class="customer-showcase-grid">
            <?php if ($companies_query && $companies_query->num_rows > 0): ?>
                <?php while($row = $companies_query->fetch_assoc()): ?>
                    <?php 
                        $full_desc = $row['description'] ? $row['description'] : ($row['about_company'] ?? '');
                        $short_desc = strlen($full_desc) > 120 ? substr($full_desc, 0, 120) . '...' : $full_desc;
                    ?>
                    <div class="showcase-card">
                        <div class="showcase-logo-box">
                            <?php if(!empty($row['logo'])): ?>
                                <img src="/bm/<?php echo htmlspecialchars($row['logo']); ?>" alt="<?php echo htmlspecialchars($row['company_name']); ?> Logo">
                            <?php else: ?>
                                <div style="width: 75px; height: 75px; background: #eef2ff; color: #003366; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 800; border: 1px solid #cbd5e1;">
                                    <?php echo strtoupper(substr($row['company_name'], 0, 1)); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        
                        <h3 class="showcase-title"><?php echo htmlspecialchars($row['company_name']); ?></h3>
                        
                        <div style="display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin-bottom: 15px;">
                            <?php if(!empty($row['industry'])): ?>
                                <span style="background: #eef2ff; color: #003366; padding: 4px 12px; border-radius: 20px; font-size: 12.5px; font-weight: 600;">
                                    <i class="fas fa-industry" style="margin-right: 4px;"></i> <?php echo htmlspecialchars($row['industry']); ?>
                                </span>
                            <?php endif; ?>
                            <?php if(!empty($row['year_established'])): ?>
                                <span style="background: #fff3eb; color: #ff6600; padding: 4px 12px; border-radius: 20px; font-size: 12.5px; font-weight: 600;">
                                    <i class="fas fa-calendar-alt" style="margin-right: 4px;"></i> Estd. <?php echo htmlspecialchars($row['year_established']); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                        
                        <p class="showcase-desc">
                            <?php echo htmlspecialchars($short_desc ?: 'Explore complete corporate profile, leadership, services and turnkey project information.'); ?>
                        </p>
                        
                        <a href="/bm/company.php?id=<?php echo $row['id']; ?>" class="showcase-btn">
                            <span>View Company</span> <i class="fas fa-arrow-right"></i>
                        </a>
                    </div>
                <?php endwhile; ?>

            <?php elseif ($legacy_customers && $legacy_customers->num_rows > 0): ?>
                <!-- Fallback to legacy customers if companies table is not populated yet -->
                <?php while($row = $legacy_customers->fetch_assoc()): ?>
                    <?php 
                        $full_desc = $row['description'] ? $row['description'] : '';
                        $short_desc = strlen($full_desc) > 120 ? substr($full_desc, 0, 120) . '...' : $full_desc;
                    ?>
                    <div class="showcase-card">
                        <div class="showcase-logo-box">
                            <?php if(!empty($row['logo_url'])): ?>
                                <img src="<?php echo htmlspecialchars($row['logo_url']); ?>" alt="<?php echo htmlspecialchars($row['company_name']); ?> Logo">
                            <?php else: ?>
                                <div style="width: 75px; height: 75px; background: #eef2ff; color: #003366; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 800; border: 1px solid #cbd5e1;">
                                    <?php echo strtoupper(substr($row['company_name'], 0, 1)); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        
                        <h3 class="showcase-title"><?php echo htmlspecialchars($row['company_name']); ?></h3>
                        
                        <?php if(!empty($row['industry'])): ?>
                            <div style="text-align: center; margin-bottom: 14px;">
                                <span style="background: #eef2ff; color: #003366; padding: 4px 12px; border-radius: 20px; font-size: 12.5px; font-weight: 600;">
                                    <?php echo htmlspecialchars($row['industry']); ?>
                                </span>
                            </div>
                        <?php endif; ?>
                        
                        <p class="showcase-desc">
                            <?php echo htmlspecialchars($short_desc ?: 'Explore partner details and service collaborations.'); ?>
                        </p>
                        
                        <?php if(!empty($row['website_url'])): ?>
                            <a href="<?php echo htmlspecialchars($row['website_url']); ?>" target="_blank" class="showcase-btn">
                                <span>Visit Website</span> <i class="fas fa-external-link-alt"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                <?php endwhile; ?>

            <?php else: ?>
                <div style="grid-column: 1 / -1; text-align: center; padding: 50px 20px; background: #fff; border-radius: 16px; border: 1px solid #eef2f6;">
                    <i class="fas fa-building" style="font-size: 48px; color: #cbd5e1; margin-bottom: 16px;"></i>
                    <h4 style="color: var(--dark-blue, #003366); font-weight: 700; margin-bottom: 8px;">No Companies Published Yet</h4>
                    <p style="color: #64748b; margin-bottom: 0;">Our customer showcases are being updated. Check back soon!</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<?php include 'includes/footer.php'; ?>
