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

// Include step definitions
include 'furniture_steps.php';
include 'civil_steps.php';
include 'aluminium_steps.php';
include 'pvc_steps.php';

// Data Structure for Static Services
$services_data = [
    1 => [
        'title' => 'Retail Outlet (Petrol Pump)',
        'intro' => 'We are a trusted name in the field of petrol pump/retail outlet construction, providing complete turnkey solutions. Our services include civil construction, structural work, and finishing work, which we execute with full responsibility from project planning to final handover. Our work adheres to the safety norms, technical standards, and approved specifications of IOCL, BPCL, HPCL, and other petrochemical companies. We work with quality materials, a trained workforce, and strict safety practices to ensure every project is completed safely, durably, and on time.',
        'scope_title' => 'Scope of Work (Construction Details)',
        'steps' => [
            [
                'number' => 1,
                'title' => 'Site Development & Earth Work',
                'content' => '
                    <ul class="custom-list">
                        <li>Site clearing & leveling</li>
                        <li>Excavation (tank pit, foundation)</li>
                        <li>Soil compaction & PCC work</li>
                    </ul>',
                'image' => 'our servies image/Services php/energy for india.jpeg'
            ],
            [
                'number' => 2,
                'title' => 'Underground Tank & Pipeline Work',
                'content' => '
                    <ul class="custom-list">
                        <li>MS / FRP underground tank installation</li>
                        <li>Product pipeline & vent pipe laying</li>
                        <li>Leak proof testing & safety arrangements</li>
                    </ul>',
                'image' => 'our servies image/Our Services/Petro-Pump-Retail-Outlet.jpeg'
            ],
            [
                'number' => 3,
                'title' => 'Civil Construction Work',
                'content' => '
                    <ul class="custom-list">
                        <li>Canopy foundation & RCC column</li>
                        <li>Sales building (office, store, toilet)</li>
                        <li>Driveway, forecourt & dispensing area RCC</li>
                        <li>Drainage & soak pit construction</li>
                    </ul>',
                'image' => 'our servies image/Our Services/Construction_of_Building.jpeg'
            ],
            [
                'number' => 4,
                'title' => 'Canopy Structure Work',
                'content' => '
                    <ul class="custom-list">
                        <li>Canopy fabrication & erection</li>
                        <li>Roofing sheet / ACP / aluminum panel</li>
                        <li>Proper lighting & electrical conduit</li>
                    </ul>',
                'image' => 'our servies image/Our Services/fabrication_wor.jpeg'
            ]
        ]
    ],
    2 => [
        'title' => 'Furniture Design & Interior Works',
        'intro' => 'Balimeen Private Limited offers a complete range of furniture and interior solutions, designed to meet the diverse needs of residential, commercial, institutional, hospitality, healthcare, and industrial spaces. Our expertise encompasses traditional craftsmanship, modern modular designs, smart furniture, sustainable solutions, and luxury projects, ensuring functionality, aesthetics, and durability in every piece we deliver.',
        'scope_title' => 'Scope of Work – Furniture Services',
        'steps' => $furniture_steps
    ],
    3 => [
        'title' => 'Terminal & Depot Engineering Works',
        'intro' => 'We offer complete engineering, procurement, and construction (EPC) services for fuel terminals and depots. Our work includes civil construction, mechanical erection, piping, electrical and instrumentation, automation, and safety systems. We ensure projects are completed on time, within budget, and according to industry standards.',
        'scope_title' => 'Scope of Work (Construction Details)',
        'steps' => [
            [
                'number' => 1,
                'title' => 'Site Development & Earth Work',
                'content' => '
                    <ul class="custom-list">
                        <li>Site clearing and grading</li>
                        <li>Excavation and backfilling</li>
                        <li>Soil compaction and PCC work</li>
                        <li>Internal roads and paving works</li>
                    </ul>',
                'image' => 'our servies image/Our Services/Development_road.jpeg'
            ],
            [
                'number' => 2,
                'title' => 'Civil & Structural Works',
                'content' => '
                    <ul class="custom-list">
                        <li>RCC foundations and equipment bases</li>
                        <li>Pump house and control room construction</li>
                        <li>Warehouse and storage shed construction</li>
                        <li>Boundary wall and fencing works</li>
                        <li>Drainage and stormwater systems</li>
                    </ul>',
                'image' => 'our servies image/Our Services/Construction_of_Building.jpeg'
            ],
            [
                'number' => 3,
                'title' => 'Storage Tank Works',
                'content' => '
                    <ul class="custom-list">
                        <li>Tank foundation and ring wall construction</li>
                        <li>MS/FRP storage tank installation</li>
                        <li>Tank farm development</li>
                        <li>Hydro testing and safety compliance</li>
                    </ul>',
                'image' => 'our servies image/Services php/d3/Terminal & Depot Engineering Works.jpeg'
            ],
            [
                'number' => 4,
                'title' => 'Pipeline & Mechanical Works',
                'content' => '
                    <ul class="custom-list">
                        <li>Product pipeline laying</li>
                        <li>Firefighting pipeline installation</li>
                        <li>Valve chambers and manifolds</li>
                        <li>Pump installation and mechanical fittings</li>
                        <li>Testing and commissioning</li>
                    </ul>',
                'image' => 'our servies image/Our Services/fabrication_wor.jpeg'
            ],
            [
                'number' => 5,
                'title' => 'Electrical & Utility Works',
                'content' => '
                    <ul class="custom-list">
                        <li>Electrical panels and cabling</li>
                        <li>Lighting systems and earthing</li>
                        <li>DG set foundation and installation</li>
                        <li>Automation and control systems</li>
                    </ul>',
                'image' => 'our servies image/Our Services/Aluminium_work.jpeg'
            ],
            [
                'number' => 6,
                'title' => 'Safety & Fire Protection Systems',
                'content' => '
                    <ul class="custom-list">
                        <li>Fire hydrant and sprinkler systems</li>
                        <li>Foam system installation</li>
                        <li>Safety signages and arrangements</li>
                        <li>Leak-proof and pressure testing</li>
                    </ul>',
                'image' => 'our servies image/Our Services/Petro-Pump-Retail-Outlet.jpeg'
            ]
        ]
    ],
    4 => [
        'title' => 'Civil Engineering & Construction Services',
        'intro' => 'We are a professionally managed civil engineering and construction company providing comprehensive solutions for infrastructure development across Government Departments, Public Sector Undertakings (PSUs), and Private Clients. With strong technical expertise, skilled manpower, and modern equipment, we execute projects with precision, safety, and quality compliance.',
        'scope_title' => 'Our Core Civil Work Services',
        'steps' => $civil_steps
    ],
    5 => [
        'title' => 'Aluminium Fabrication & Installation Works',
        'intro' => 'We offer comprehensive Aluminium Fabrication and Installation Services that combine modern design, structural strength, and long-term durability for residential, commercial, and industrial projects. Our company specializes in delivering high-quality aluminium solutions that are lightweight, corrosion-resistant, and aesthetically appealing.',
        'scope_title' => 'Our Range of Aluminium & Glazing Services',
        'steps' => $aluminium_steps
    ],
    6 => [
        'title' => 'PVC Fabrication & Installation Works',
        'intro' => 'Our company provides complete and professional PVC (Polyvinyl Chloride) work solutions for residential, commercial, institutional, and industrial projects. With years of experience and a skilled workforce, we specialize in designing, fabricating, and installing high-quality PVC products that offer durability, functionality, and modern aesthetics.',
        'scope_title' => 'Our Core PVC Services',
        'steps' => $pvc_steps
    ]
];

// Get Service ID or Title
$service_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$slug = isset($_GET['slug']) ? trim($_GET['slug']) : (isset($_GET['title']) ? trim($_GET['title']) : '');

// Find by Slug (Title) if ID is not provided
if ($service_id === 0 && !empty($slug)) {
    $decoded_slug = urldecode($slug);
    if ($slug === "Furniture image" || $decoded_slug === "Furniture image") {
        $service_id = 2;
    } else {
        foreach ($services_data as $id => $data) {
            if (trim($data['title']) === $slug || trim($data['title']) === $decoded_slug) {
                $service_id = $id;
                break;
            }
        }
    }
}

// Fallback / DB Lookup for Dynamic Services
if (!isset($services_data[$service_id])) {
    include_once 'includes/db.php';
    if ($service_id > 0) {
        $stmt_dyn = $conn->prepare("SELECT * FROM services WHERE id = ?");
        $stmt_dyn->bind_param("i", $service_id);
    } else {
        $decoded_slug = urldecode($slug);
        $stmt_dyn = $conn->prepare("SELECT * FROM services WHERE title = ?");
        $stmt_dyn->bind_param("s", $decoded_slug);
    }
    $stmt_dyn->execute();
    $res_dyn = $stmt_dyn->get_result();
    if ($res_dyn && $res_dyn->num_rows > 0) {
        $dyn_row = $res_dyn->fetch_assoc();
        $service = [
            'title' => $dyn_row['title'],
            'is_dynamic' => true,
            'description' => $dyn_row['description'],
            'image_url' => $dyn_row['image_url']
        ];
    } else {
        $service = [
            'title' => !empty($slug) ? htmlspecialchars(urldecode($slug)) : 'Service Details',
            'intro' => 'Turnkey Engineering & Construction Services by Balimeen Private Limited.',
            'steps' => []
        ];
    }
    if (isset($stmt_dyn)) $stmt_dyn->close();
} else {
    $service = $services_data[$service_id];
}
?>

<!-- Link Dedicated Design System Stylesheet -->
<link rel="stylesheet" href="/bm/assets/css/blog.css">

<div class="bm-single-post-wrapper">

    <!-- 1. BREADCRUMBS -->
    <nav class="bm-breadcrumb-nav" aria-label="breadcrumb">
        <div class="container">
            <ul class="bm-breadcrumb-list">
                <li><a href="/bm/"><i class="fas fa-home me-1"></i> Home</a></li>
                <li class="bm-breadcrumb-sep"><i class="fas fa-chevron-right"></i></li>
                <li><a href="/bm/services">Services</a></li>
                <li class="bm-breadcrumb-sep"><i class="fas fa-chevron-right"></i></li>
                <li><span class="bm-breadcrumb-current"><?php echo htmlspecialchars(mb_substr($service['title'], 0, 45)) . (mb_strlen($service['title']) > 45 ? '...' : ''); ?></span></li>
            </ul>
        </div>
    </nav>

    <!-- 2. MAIN FULL-WIDTH LAPTOP CONTAINER (NO SIDEBAR, NO BOTTOM BANNER) -->
    <div class="bm-single-post-layout">
        <main>
            <article class="bm-article-card">

                <?php if(isset($service['is_dynamic']) && $service['is_dynamic']): ?>
                    <!-- ===== DYNAMIC SERVICE CONTENT ===== -->
                    <?php if (!empty($service['image_url'])): ?>
                    <div class="singleBlogImg">
                        <img src="<?php echo htmlspecialchars($service['image_url']); ?>"
                             alt="<?php echo htmlspecialchars($service['title']); ?>"
                             loading="eager"
                             onerror="this.src='/bm/our servies image/Our Services/Construction_of_Building.jpeg'">
                    </div>
                    <?php endif; ?>

                    <div class="blogSingle">
                        <h1 class="heading"><?php echo htmlspecialchars($service['title']); ?></h1>
                        <div class="iconWrap">
                            <p class="iconBox">
                                <i class="fas fa-shield-alt"></i>
                                <span>Balimeen Certified Engineering Service</span>
                            </p>
                        </div>
                    </div>

                    <div class="webContent singleBlogContent">
                    <?php
                        $content = $service['description'];
                        $json_data = json_decode($content, true);

                        if ($json_data && isset($json_data['blocks'])) {
                            foreach ($json_data['blocks'] as $block) {
                                switch ($block['type']) {
                                    case 'header':
                                        $level = $block['data']['level'] ?? 2;
                                        echo "<h{$level}>" . ($block['data']['text'] ?? '') . "</h{$level}>";
                                        break;
                                    case 'paragraph':
                                        echo "<p>" . ($block['data']['text'] ?? '') . "</p>";
                                        break;
                                    case 'list':
                                        $tag = ($block['data']['style'] ?? 'unordered') === 'ordered' ? 'ol' : 'ul';
                                        echo "<{$tag}>";
                                        foreach ($block['data']['items'] ?? [] as $item) {
                                            $item_text = is_array($item) ? ($item['content'] ?? '') : $item;
                                            echo "<li>" . $item_text . "</li>";
                                        }
                                        echo "</{$tag}>";
                                        break;
                                    case 'image':
                                        $url = $block['data']['file']['url'] ?? '';
                                        $caption = $block['data']['caption'] ?? '';
                                        echo "<figure><img src='{$url}' alt='" . htmlspecialchars($caption) . "'>";
                                        if ($caption) echo "<figcaption>" . htmlspecialchars($caption) . "</figcaption>";
                                        echo "</figure>";
                                        break;
                                    case 'quote':
                                        $qtext = $block['data']['text'] ?? '';
                                        $qcap = $block['data']['caption'] ?? '';
                                        echo "<blockquote><p style='margin-bottom:0;'>{$qtext}</p>";
                                        if ($qcap) echo "<footer style='font-size:14px;font-weight:600;margin-top:8px;color:var(--bm-blue-primary);'>— {$qcap}</footer>";
                                        echo "</blockquote>";
                                        break;
                                    case 'table':
                                        if (!empty($block['data']['content']) && is_array($block['data']['content'])) {
                                            echo "<div style='overflow-x:auto; margin: 1.8em 0;'><table>";
                                            foreach ($block['data']['content'] as $rowIdx => $row) {
                                                echo "<tr>";
                                                $tag2 = ($rowIdx === 0 && !empty($block['data']['withHeadings'])) ? 'th' : 'td';
                                                foreach ($row as $cell) {
                                                    echo "<{$tag2}>" . htmlspecialchars_decode(strip_tags($cell, '<b><i><a><strong><em>')) . "</{$tag2}>";
                                                }
                                                echo "</tr>";
                                            }
                                            echo "</table></div>";
                                        }
                                        break;
                                    case 'code':
                                        $code_text = htmlspecialchars($block['data']['code'] ?? '');
                                        echo "<pre><code>{$code_text}</code></pre>";
                                        break;
                                    case 'preformatted':
                                        $pre_text = htmlspecialchars($block['data']['text'] ?? '');
                                        echo "<pre>{$pre_text}</pre>";
                                        break;
                                    case 'details':
                                        $summary = htmlspecialchars($block['data']['summary'] ?? 'Click to Read More');
                                        $dcontent = $block['data']['content'] ?? '';
                                        echo "<details><summary>{$summary}</summary><p style='margin-top:12px;margin-bottom:0;'>{$dcontent}</p></details>";
                                        break;
                                    case 'pullquote':
                                        $pq_text = $block['data']['text'] ?? '';
                                        $pq_cit = $block['data']['citation'] ?? '';
                                        echo "<div style='padding:22px 26px; border-left:4px solid var(--bm-blue-primary); border-right:4px solid var(--bm-blue-primary); background:var(--bm-blue-tint); text-align:center; margin:32px 0; border-radius:var(--bm-radius-md);'>";
                                        echo "<h3 style='font-style:italic; color:var(--bm-blue-dark); margin:0 0 8px; font-size:1.25rem;'>“{$pq_text}”</h3>";
                                        if ($pq_cit) echo "<p style='text-transform:uppercase;font-size:12px;font-weight:700;color:var(--bm-blue-primary);margin:0;'>{$pq_cit}</p>";
                                        echo "</div>";
                                        break;
                                    case 'button':
                                        $burl = htmlspecialchars($block['data']['url'] ?? '#');
                                        $btext = htmlspecialchars($block['data']['text'] ?? 'Learn More');
                                        echo "<div style='text-align:center;margin:26px 0;'><a href='{$burl}' class='btn-cta-white' style='background:var(--bm-blue-primary);color:#fff;display:inline-block;'>{$btext} <i class='fas fa-arrow-right ms-2'></i></a></div>";
                                        break;
                                    case 'delimiter':
                                        echo "<div class='ce-delimiter'>* * *</div>";
                                        break;
                                    case 'separator':
                                        echo "<hr style='width:80px;margin:32px auto;border:none;border-top:3px solid var(--bm-blue-primary);border-radius:3px;'>";
                                        break;
                                    case 'spacer':
                                        $height = (int)($block['data']['height'] ?? 30);
                                        echo "<div style='height:{$height}px;'></div>";
                                        break;
                                    case 'raw':
                                        echo ($block['data']['html'] ?? '');
                                        break;
                                    case 'embed-youtube':
                                        if (!empty($block['data']['url'])) {
                                            preg_match('/(?:v=|youtu\.be\/)([a-zA-Z0-9_-]+)/', $block['data']['url'], $m);
                                            if (!empty($m[1])) {
                                                echo "<div style='position:relative;padding-bottom:56.25%;height:0;overflow:hidden;margin:26px 0;border-radius:var(--bm-radius-md);box-shadow:var(--bm-shadow-md);'><iframe src='https://www.youtube.com/embed/{$m[1]}' style='position:absolute;top:0;left:0;width:100%;height:100%;' frameborder='0' allowfullscreen></iframe></div>";
                                            }
                                        }
                                        break;
                                    default:
                                        if (!empty($block['data']['text'])) {
                                            echo "<p>" . $block['data']['text'] . "</p>";
                                        }
                                        break;
                                }
                            }
                        } else {
                            echo $content;
                        }
                    ?>
                    </div>

                <?php else: ?>
                    <!-- ===== STATIC DETAILED SERVICE CONTENT ===== -->
                    <div class="blogSingle">
                        <h1 class="heading">
                            <?php echo htmlspecialchars($service['title']); ?>
                        </h1>
                        <div class="iconWrap">
                            <p class="iconBox">
                                <i class="fas fa-certificate"></i>
                                <span>Turnkey Execution by Balimeen Private Limited</span>
                            </p>
                            <p class="iconBox">
                                <i class="fas fa-check-circle" style="color: var(--bm-blue-primary);"></i>
                                <span>Quality &amp; Safety Assured</span>
                            </p>
                        </div>
                    </div>

                    <?php if(isset($service['intro'])): ?>
                        <div class="service-intro-text">
                            <?php echo $service['intro']; ?>
                        </div>
                    <?php endif; ?>

                    <?php if(isset($service['scope_title'])): ?>
                        <h3 class="service-scope-header">
                            <i class="fas fa-tasks"></i> <?php echo htmlspecialchars($service['scope_title']); ?>
                        </h3>
                    <?php endif; ?>

                    <!-- Steps Execution Loop -->
                    <?php 
                    if(isset($service['steps']) && is_array($service['steps'])): 
                        $count = 0;
                        foreach($service['steps'] as $step): 
                            $count++;
                            $is_reverse = ($count % 2 == 0) ? 'reverse' : '';
                            if (isset($step['layout']) && $step['layout'] == 'normal') { $is_reverse = ''; }
                    ?>
                        <div class="service-step-row <?php echo $is_reverse; ?>">
                            <div class="service-step-content">
                                <div class="service-step-title">
                                    <span class="service-step-num"><?php echo $count; ?></span>
                                    <?php echo htmlspecialchars($step['title'] ?? 'Phase ' . $count); ?>
                                </div>
                                <div class="service-step-desc">
                                    <?php echo $step['content'] ?? ''; ?>
                                </div>
                            </div>
                            
                            <div class="service-step-image">
                                <?php 
                                $display_image = '';
                                if ($service_id == 2 && isset($furniture_images[$step['title']])) {
                                    $display_image = $furniture_images[$step['title']];
                                } elseif (!empty($step['image'])) {
                                    $display_image = $step['image'];
                                } elseif (isset($furniture_images['default'])) {
                                    $display_image = $furniture_images['default'];
                                }
                                if (!empty($display_image) && !preg_match('#^https?://#', $display_image) && strpos($display_image, '/') !== 0) {
                                    $display_image = '/bm/' . str_replace('\\', '/', $display_image);
                                }
                                if(!empty($display_image)): ?>
                                    <img src="<?php echo htmlspecialchars($display_image); ?>" 
                                         alt="<?php echo htmlspecialchars($step['title'] ?? 'Service Step'); ?>"
                                         loading="lazy"
                                         onerror="this.src='/bm/our servies image/Our Services/Construction_of_Building.jpeg'">
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php 
                        endforeach; 
                    endif; 
                    ?>

                <?php endif; ?>

                <!-- Back to Services Button -->
                <div style="margin-top: 36px;">
                    <a href="/bm/services" class="article-back-link">
                        <i class="fas fa-arrow-left"></i> Back to All Services
                    </a>
                </div>

            </article>
        </main>
    </div><!-- /.bm-single-post-layout -->

</div><!-- /.bm-single-post-wrapper -->

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