<?php
ob_start();
include_once('include/header.php');

$segments = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
$categorySlug = $segments[count($segments) - 1] ?? null;

$category = null;
$products = [];

if ($categorySlug) {

    // Fetch category
    $stmt = $con->prepare("
        SELECT *
        FROM top_categories
        WHERE slug = ?
        LIMIT 1
    ");
    $stmt->bind_param("s", $categorySlug);
    $stmt->execute();
    $category = $stmt->get_result()->fetch_assoc();

    if ($category) {
        // Fetch products under this category
        $stmt = $con->prepare("
            SELECT id, product_name, slug, product_main_image, product_short_desc
            FROM products
            WHERE top_category_id = ?
            ORDER BY id DESC
        ");
        $stmt->bind_param("i", $category['id']);
        $stmt->execute();
        $products = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    }
}

// 404 if not found
if (!$category) {
    ob_clean();
    include '404.php';
    exit;
}

ob_end_flush();
?>


<style>
.consult-card {
    border: none;
    margin: auto;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}

.toggle-btns .btn {
    border-radius: 30px;
    border: 1px solid #cbcbcbff;
}

.toggle-btns .btn.active {
    background-color: #cbcbcbff;
    border: none;
    /* color: #000;
      font-weight: 600; */
}

.submit-btn {

    border: none;
    border-radius: 30px;
    padding: 12px;
    font-weight: 600;
}

.form-check-input:checked {
    background-color: #4a6cf7;
    border-color: #4a6cf7;
}

.form-check-label {
    font-size: 14px;
}

.category-img {
    width: 100%;
    height: 320px;
    object-fit: cover;
    border-radius: 12px;
}

.product-card-img {
    width: 100%;
    height: 220px;
    object-fit: cover;
    border-radius: 8px;
}
</style>


<!-- Page Header Start -->
<div class="page-header parallaxie">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="page-header-box">
                    <h1 class="text-anime">
                        <?php echo htmlspecialchars($category['name']); ?>
                    </h1>

                    <nav class="wow fadeInUp" data-wow-delay="0.25s">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="<?php path() ?>">Home /</a></li>
                            <li class="breadcrumb-item">
                                <a href="<?php echo htmlspecialchars($category['slug']); ?>">
                                    <?php echo htmlspecialchars($category['name']); ?>
                                </a>
                            </li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="container py-5">
    <div class="row ">
        <div class="col-md-4">
            <img src="<?php path(); ?>admin/assets/images/top_categories/<?php echo $category['image']; ?>"
                class="img-fluid category-img" alt="<?php echo htmlspecialchars($category['name']); ?>">
        </div>

        <div class="col-md-8">
            <?php echo html_entity_decode($category['description']); ?>
        </div>
    </div>
</div>

 <?php if (!empty($products)): ?>
<div class="container py-5">
    <h2 class="mb-4">Available Products</h2>

    <div class="row">
       
        <?php foreach ($products as $product): ?>
        <div class="col-lg-4 col-md-6 mb-4">
            <div class="consult-card p-3 h-100">

                <img src="<?php path(); ?>admin/assets/images/products/<?php echo $product['product_main_image']; ?>"
                    class="product-card-img mb-3" alt="<?php echo htmlspecialchars($product['product_name']); ?>">

                <h5><?php echo htmlspecialchars($product['product_name']); ?></h5>

                <p>
                    <?php  echo htmlspecialchars(  mb_strimwidth(strip_tags($product['product_short_desc']), 0, 120, '...')  ); ?>
                </p>

                <a href="<?php path() ?>product/<?php echo $product['slug']; ?>" class="small text-decoration-none btn-custom">
                    View Details
                </a>
            </div>
        </div>
        <?php endforeach; ?>
       
    </div>
</div>

<?php endif; ?>



<!-- Single Service Page Start -->
 <div class="container">
    <div class="row">
        <div class="col-md-12 mb-4">
            <!-- Service Benefits Start -->
            <div class="service-benefits">
                <div class="row">
                    <div class="col-md-12">
                        <div class="service-benefits-title">
                            <h2 class="text-anime">Benefits of Solar Energy</h2>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-lg-4 col-md-6">
                        <div class="benefits-item">
                            <div class="icon-box">
                                <img src="<?php path() ?>images/icon-benefits-1.svg" alt="Renewable Solar Energy">
                            </div>
                            <h3>Renewable Energy</h3>
                            <p>Solar energy is a clean and renewable power source that reduces carbon emissions and supports a sustainable environment.</p>
                        </div>
                    </div>

                    <div class="col-lg-4 col-md-6">
                        <div class="benefits-item">
                            <div class="icon-box">
                                <img src="<?php path() ?>images/icon-benefits-2.svg" alt="Solar Energy Saving">
                            </div>
                            <h3>Energy Saving</h3>
                            <p>Generate free electricity from sunlight and reduce your power bills by up to 70–90% over time.</p>
                        </div>
                    </div>

                    <div class="col-lg-4 col-md-6">
                        <div class="benefits-item">
                            <div class="icon-box">
                                <img src="<?php path() ?>images/icon-benefits-3.svg" alt="Easy Solar Installation">
                            </div>
                            <h3>Easy Installation</h3>
                            <p>Modern solar systems are easy to install with minimal disruption and quick setup by experts.</p>
                        </div>
                    </div>

                    <div class="col-lg-4 col-md-6">
                        <div class="benefits-item">
                            <div class="icon-box">
                                <img src="<?php path() ?>images/icon-benefits-4.svg" alt="Smart Solar Solution">
                            </div>
                            <h3>Smart Energy Solution</h3>
                            <p>Solar provides a long-term energy solution for homes, businesses, and industries with reliable output.</p>
                        </div>
                    </div>

                    <div class="col-lg-4 col-md-6">
                        <div class="benefits-item">
                            <div class="icon-box">
                                <img src="<?php path() ?>images/icon-benefits-5.svg" alt="Solar Technical Support">
                            </div>
                            <h3>Technical Support</h3>
                            <p>Get complete support including system monitoring, troubleshooting, and performance optimization.</p>
                        </div>
                    </div>

                    <div class="col-lg-4 col-md-6">
                        <div class="benefits-item">
                            <div class="icon-box">
                                <img src="<?php path() ?>images/icon-benefits-6.svg" alt="Solar Maintenance Service">
                            </div>
                            <h3>Solar Maintenance</h3>
                            <p>Regular cleaning and inspection ensure maximum efficiency and long life of your solar system.</p>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Service Benefits End -->
        </div>
    </div>
</div>
 
<div class="container">
    <div class="row py-5"> 
        <div class="col-md-6 faq-box">
            <h2 class="text-anime">Frequently Asked Questions</h2>

            <div class="faq-accordion">
                <div class="accordion" id="faq_accordion">

                    <div class="accordion-item">
                        <h2 class="accordion-header" id="heading1">
                            <button class="accordion-button" type="button" data-bs-toggle="collapse"
                                data-bs-target="#collapse1" aria-expanded="true" aria-controls="collapse1">
                                What is Renewable Energy and How Does Solar Work?
                            </button>
                        </h2>
                        <div id="collapse1" class="accordion-collapse collapse show" aria-labelledby="heading1"
                            data-bs-parent="#faq_accordion">
                            <div class="accordion-body">
                                <p>Renewable energy comes from natural sources like sunlight. Solar panels convert sunlight into electricity, reducing dependency on fossil fuels.</p>
                            </div>
                        </div>
                    </div>

                    <div class="accordion-item">
                        <h2 class="accordion-header" id="heading2">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                                data-bs-target="#collapse2" aria-expanded="false" aria-controls="collapse2">
                                Is Solar Energy Suitable for Homes and Businesses?
                            </button>
                        </h2>
                        <div id="collapse2" class="accordion-collapse collapse" aria-labelledby="heading2"
                            data-bs-parent="#faq_accordion">
                            <div class="accordion-body">
                                <p>Yes, solar energy is ideal for homes, shops, offices, factories, hospitals, and institutions.</p>
                            </div>
                        </div>
                    </div>

                    <div class="accordion-item">
                        <h2 class="accordion-header" id="heading3">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                                data-bs-target="#collapse3" aria-expanded="false" aria-controls="collapse3">
                                How Long Does a Solar Power System Last?
                            </button>
                        </h2>
                        <div id="collapse3" class="accordion-collapse collapse" aria-labelledby="heading3"
                            data-bs-parent="#faq_accordion">
                            <div class="accordion-body">
                                <p>Solar panels typically last 20–25 years with minimal maintenance and consistent performance.</p>
                            </div>
                        </div>
                    </div>

                    <div class="accordion-item">
                        <h2 class="accordion-header" id="heading4">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                                data-bs-target="#collapse4" aria-expanded="false" aria-controls="collapse4">
                                Does a Solar System Require Regular Maintenance?
                            </button>
                        </h2>
                        <div id="collapse4" class="accordion-collapse collapse" aria-labelledby="heading4"
                            data-bs-parent="#faq_accordion">
                            <div class="accordion-body">
                                <p>Solar systems need very little maintenance, mainly panel cleaning and periodic system checks.</p>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="col-md-6 mx-auto mb-4">
            <h2 class="text-anime">Book a FREE Solar Consultation</h2>

            <div class="service-sidebar">
                <div class="card consult-card p-4">
                    
                    <form id="contactForm" action="#" method="POST" data-toggle="validator">
                                                    <div class="row">
                                                
                                                        <!-- Name -->
                                                        <div class="form-group col-md-6 mb-4">
                                                            <input type="text" name="name" class="form-control" id="name"  placeholder="Full Name" onkeydown="return nameKeyValidation(event)"
                                                               onpaste="return false"  required>
                    
                                                            <div class="help-block with-errors"></div>
                                                        </div>
                                                
                                                        <!-- Email -->
                                                        <div class="form-group col-md-6 mb-4">
                                                            <input type="email" name="email" class="form-control" id="email" placeholder="Email Address" required>
                                                            <div class="help-block with-errors"></div>
                                                        </div>
                                                
                                                        <!-- Phone -->
                                                        <div class="form-group col-md-6 mb-4">
                                                           <input type="text" name="phone" id="phone"
                                                               class="form-control"
                                                               placeholder="Mobile Number"
                                                               maxlength="10"
                                                               onkeypress="return /[0-9]/.test(event.key)"
                                                               required>
                    
                                                            <div class="help-block with-errors"></div>
                                                        </div>
                                                
                                                        <!-- Service / Product Selection -->
                                                        <div class="form-group col-md-6 mb-4">
                                                            <select name="service_interest" class="form-control" id="service_interest" required>
                                                                <option value="" disabled selected>Select Service / Product</option>
                                                
                                                                <optgroup label="Solar Solutions">
                                                                    <option value="On Grid Solar">On-Grid Solar System</option>
                                                                    <option value="Off Grid Solar">Off-Grid Solar System</option>
                                                                    <option value="Industrial Solar">Industrial Solar</option>
                                                                </optgroup>
                                                
                                                                <optgroup label="Products">
                                                                    <option value="Solar Water Heater">Solar Water Heater / Geyser</option>
                                                                    <option value="Solar Water Pump">Solar Water Pump</option>
                                                                    <option value="Solar Street Light">Solar Street Light</option>
                                                                </optgroup>
                                                
                                                                <optgroup label="Services">
                                                                    <option value="Installation & Commissioning">Installation & Commissioning</option>
                                                                    <option value="AMC">AMC (Annual Maintenance Contract)</option>
                                                                    <option value="Repair & Maintenance">Repair & Maintenance</option>
                                                                    <option value="Subsidy Help">Subsidy Documentation Help</option>
                                                                </optgroup>
                                                            </select>
                                                            <div class="help-block with-errors"></div>
                                                        </div>
                                                
                                                        <!-- Message -->
                                                        <div class="form-group col-md-12 mb-4">
                                                            <textarea name="msg" class="form-control" id="msg" rows="4"
                                                                placeholder="Site details, load requirement, location, or any specific question"></textarea>
                                                            <div class="help-block with-errors"></div>
                                                        </div>
                                                
                                                        <!-- Submit -->
                                                        <div class="col-md-12 text-center">
                                                            <button type="submit" class="btn-default">Get Free Consultation</button>
                                                            <div id="msgSubmit" class="h3 text-left hidden"></div>
                                                        </div>
                                                
                                                    </div>
                                                </form>

                </div>
            </div>
        </div>
    </div>
</div>
 
 <!-- Footer Ticker End -->
<?php include_once('include/footer.php') ?>