<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Client & Feedback';
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$clientsDir = BASE_PATH . '/uploads/clients/';
$feedbackDir = BASE_PATH . '/uploads/feedback/';
if (!is_dir($feedbackDir)) @mkdir($feedbackDir, 0755, true);

// Clients: logo and name (from our_clients)
$clientsTitle = getSetting('our_clients_title', 'Our Clients');
$clientsSubtext = getSetting('our_clients_subtext', '');
$clients = [];
try {
    $stmt = $pdo->query("SELECT id, name, logo, website_url FROM our_clients WHERE is_active = 1 ORDER BY sort_order, id");
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $c) {
        if (empty($c['logo']) || !is_file($clientsDir . $c['logo'])) continue;
        $c['logo_url'] = $assetBase . 'uploads/clients/' . rawurlencode($c['logo']);
        $clients[] = $c;
    }
} catch (Exception $e) { /* */ }

// Feedback / testimonials: name, designation, message, image
$feedbackTitle = getSetting('client_feedback_title', 'What Our Clients Say');
$feedbackList = [];
try {
    $stmt = $pdo->query("SELECT customer_name, designation, message, image FROM client_feedback WHERE is_active = 1 ORDER BY sort_order, id");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $imgSrc = null;
        if (!empty($row['image']) && is_file($feedbackDir . $row['image'])) {
            $imgSrc = $assetBase . 'uploads/feedback/' . rawurlencode($row['image']);
        }
        $feedbackList[] = [
            'customer_name' => $row['customer_name'],
            'designation' => $row['designation'] ?? '',
            'message' => $row['message'],
            'image_src' => $imgSrc,
        ];
    }
} catch (Exception $e) { /* */ }

require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content clients-feedback-page">
    <div class="container">
        <h1 class="clients-feedback-page-title">Client &amp; Feedback</h1>

        <!-- Clients: logo and name -->
        <section class="clients-feedback-section clients-section" aria-labelledby="clients-heading">
            <h2 id="clients-heading" class="clients-feedback-heading"><?php echo htmlspecialchars($clientsTitle); ?></h2>
            <?php if ($clientsSubtext): ?>
            <p class="clients-feedback-sub"><?php echo htmlspecialchars($clientsSubtext); ?></p>
            <?php endif; ?>
            <?php if (!empty($clients)): ?>
            <div class="clients-logo-grid">
                <?php foreach ($clients as $c): ?>
                <div class="client-logo-card">
                    <div class="client-logo-img-wrap">
                        <img src="<?php echo htmlspecialchars($c['logo_url']); ?>" alt="<?php echo htmlspecialchars($c['name']); ?>" loading="lazy">
                    </div>
                    <p class="client-logo-name"><?php echo htmlspecialchars($c['name']); ?></p>
                </div>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p class="clients-feedback-empty">No clients added yet. Manage client logos in <strong>Admin → Our clients</strong>.</p>
            <?php endif; ?>
        </section>

        <!-- What clients say: name, designation, message, image -->
        <section class="clients-feedback-section feedback-section" aria-labelledby="feedback-heading">
            <h2 id="feedback-heading" class="clients-feedback-heading"><?php echo htmlspecialchars($feedbackTitle); ?></h2>
            <?php if (!empty($feedbackList)): ?>
            <div class="feedback-grid">
                <?php foreach ($feedbackList as $fb): ?>
                <div class="feedback-card">
                    <div class="feedback-card-inner">
                        <?php if (!empty($fb['image_src'])): ?>
                        <div class="feedback-avatar-wrap">
                            <img src="<?php echo htmlspecialchars($fb['image_src']); ?>" alt="<?php echo htmlspecialchars($fb['customer_name']); ?>" loading="lazy">
                        </div>
                        <?php else: ?>
                        <div class="feedback-avatar-wrap feedback-avatar-placeholder">
                            <span aria-hidden="true"><?php echo htmlspecialchars(mb_substr($fb['customer_name'], 0, 1)); ?></span>
                        </div>
                        <?php endif; ?>
                        <blockquote class="feedback-message"><?php echo nl2br(htmlspecialchars($fb['message'])); ?></blockquote>
                        <div class="feedback-meta">
                            <strong class="feedback-name"><?php echo htmlspecialchars($fb['customer_name']); ?></strong>
                            <?php if (!empty($fb['designation'])): ?>
                            <span class="feedback-designation"><?php echo htmlspecialchars($fb['designation']); ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p class="clients-feedback-empty">No feedback yet. Add testimonials in <strong>Admin → Client Feedback</strong>.</p>
            <?php endif; ?>
        </section>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
