<?= $this->extend('layouts/admin') ?>

<?= $this->section('content') ?>

<div>
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px;">
        <div>
            <h1>Service Offerings</h1>
            <p style="color: var(--text-muted);">Manage the core services displayed on the frontend.</p>
        </div>
        <a href="<?= base_url('admin/service_add') ?>" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Add Service</a>
    </div>

    <div class="admin-card">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Service Title</th>
                    <th>Type</th>
                    <th>Thumbnail</th>
                    <th>Description Snippet</th>
                    <th style="text-align: right;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($services as $service): ?>
                    <tr>
                        <td style="font-weight: 600;"><?= $service['title'] ?></td>
                        <td>
                            <span style="padding: 4px 10px; border-radius: 6px; font-size: 0.75rem; font-weight: 700; background: <?= $service['type'] == 'Expertise' ? 'var(--primary-soft)' : 'rgba(255,255,255,0.05)' ?>; color: <?= $service['type'] == 'Expertise' ? 'var(--primary)' : 'var(--text-muted)' ?>; border: 1px solid <?= $service['type'] == 'Expertise' ? 'var(--primary)' : 'var(--border)' ?>;">
                                <?= $service['type'] ?>
                            </span>
                        </td>
                        <td>
                            <?php 
                                $serviceImg = $service['image'];
                                if (empty($serviceImg) || (strpos($serviceImg, 'http') !== 0 && !file_exists(FCPATH . str_replace(base_url(), '', $serviceImg)))) {
                                    $title = strtolower($service['title']);
                                    if (strpos($title, 'railway bridge') !== false) {
                                        $serviceImg = base_url('assets/images/services/railway_bridge.png');
                                    } elseif (strpos($title, 'foot over') !== false || strpos($title, 'fob') !== false) {
                                        $serviceImg = base_url('assets/images/services/foot_over_bridge.png');
                                    } elseif (strpos($title, 'track') !== false || strpos($title, 'mast') !== false) {
                                        $serviceImg = base_url('assets/images/services/track_works.png');
                                    } elseif (strpos($title, 'gated') !== false || strpos($title, 'villas') !== false) {
                                        $serviceImg = base_url('assets/images/services/gated_community.png');
                                    } elseif (strpos($title, 'high-rise') !== false || strpos($title, 'tower') !== false) {
                                        $serviceImg = base_url('assets/images/services/high_rise.png');
                                    } else {
                                        $serviceImg = base_url('assets/images/services/general_construction.png');
                                    }
                                }
                            ?>
                            <img src="<?= $serviceImg ?>" alt="" style="width: 45px; height: 45px; border-radius: 8px; object-fit: cover; border: 1px solid var(--border-admin);">
                        </td>
                        <td style="color: var(--text-muted); font-size: 0.9rem; max-width: 400px;"><?= substr($service['description'], 0, 80) ?>...</td>
                        <td style="text-align: right;">
                            <div style="display: flex; gap: 8px; justify-content: flex-end;">
                                <a href="#" class="btn-action edit" title="Edit"><i class="bi bi-pencil-square"></i></a>
                                <a href="#" class="btn-action delete" title="Delete"><i class="bi bi-trash-fill"></i></a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?= $this->endSection() ?>
