<?php
include("admin/connection.php");
include('include/header.php');

$slug = $_GET['slug'];
$ser = mysqli_query($con, "SELECT * FROM `tbl_top_category` where slug='$slug'");
while ($row = mysqli_fetch_array($ser)) {
    $cat_name = $row['cat_name'];
    $slug = $row['slug'];
    $Image = $row['Image'];
    $t_id = $row['t_id'];
    $description = $row['description'];
}
?>
<?php
  
require_once 'mail-function.php';

if (isset($_POST['submit'])) {
 
    $name    = mysqli_real_escape_string($con, $_POST['name'] ?? '');
    $email   = mysqli_real_escape_string($con, $_POST['email'] ?? '');
    $phone   = mysqli_real_escape_string($con, $_POST['phone'] ?? '');
    $subject = mysqli_real_escape_string($con, $_POST['subject'] ?? '');
    $message = mysqli_real_escape_string($con, $_POST['message'] ?? '');
 
    $body = "
    <h3>New Contact Enquiry Received</h3>

    <table border='1' cellpadding='10' cellspacing='0' style='border-collapse: collapse; width: 100%;'>
        <tr>
            <th align='left' style='background:#f5f5f5;'>Name</th>
            <td>{$name}</td>
        </tr>
        <tr>
            <th align='left' style='background:#f5f5f5;'>Email</th>
            <td>{$email}</td>
        </tr>
        <tr>
            <th align='left' style='background:#f5f5f5;'>Phone</th>
            <td>{$phone}</td>
        </tr>
        <tr>
            <th align='left' style='background:#f5f5f5;'>Subject</th>
            <td>{$subject}</td>
        </tr>
        <tr>
            <th align='left' style='background:#f5f5f5;'>Message</th>
            <td>" . nl2br(htmlspecialchars($message)) . "</td>
        </tr>
    </table>
    ";
 
    $result = sendWebsiteMail(
        'New Contact Enquiry - ' . $subject, 
        $body,                               
        $email,                               
        $name                                
    );

    
    if ($result['status']) {
        echo "<script>
                alert('Your enquiry has been sent successfully.');
                window.location.href = window.location.href;
              </script>";
    } else {
        echo "<script>
                alert('Mail Error: " . addslashes($result['message']) . "');
              </script>";
    }
}

?>

<style>
    .blog-details__img img {
        aspect-ratio: 3 / 2;
    }

    img.floating {
        animation-name: floating;
        animation-duration: 2s;
        animation-iteration-count: infinite;
        animation-timing-function: ease-in-out;
        margin-left: 0px;
        margin-top: 0px;
    }

    @keyframes floating {
        0% {
            transform: translate(0, 0px);
        }

        50% {
            transform: translate(0, 15px);
        }

        100% {
            transform: translate(0, -0px);
        }
    }

    p {
        text-align: justify;
    }
</style>
<!--Page Header Start-->
<section class="page-header">
    <div class="page-header__bg" style="background-image: url(<?php path() ?>assets/images/backgrounds/page-header-bg.jpg);">
    </div>
    <div class="page-header__shape-1">
        <img src="<?php path() ?>assets/images/shapes/page-header-shape-1.png" alt="">
    </div>
    <div class="container">
        <div class="page-header__inner">
            <h3><?php echo $cat_name; ?></h3>
            <div class="thm-breadcrumb__inner">
                <ul class="thm-breadcrumb list-unstyled">
                    <li><a href="<?php path() ?>index.php">Home</a></li>
                    <li><span class="fas fa-angle-right"></span></li>
                    <li><?php echo $cat_name; ?></li>
                </ul>
            </div>
        </div>
    </div>
</section>
<!--Page Header End-->

<!--Blog Details Start -->
<section class="blog-details">
    <div class="container">
        <div class="row">
            <div class="col-xl-6 col-lg-6">
                <div class="blog-details__left">
                    <div class="blog-details__img-box-1">
                        <div class="blog-details__img">
                            <img class="floating" src="<?php path(); ?>/admin/assets/images/service/<?php echo htmlspecialchars($Image); ?>" alt="">
                        </div>

                    </div>

                </div>
            </div>

            <!--Start Sidebar-->
            <div class="col-xl-6 col-lg-6">
                <div class="blog-details__content">

                    <h4 class="blog-details__title"><?php echo htmlspecialchars($cat_name); ?></h4>

                    <p class="blog-details__text-1"><?php echo $description; ?></p>
                </div>
            </div>
            <!--End Sidebar-->
        </div>
    </div>
</section>
<!--Blog Details End-->
<!--Feature One Start-->
<!-- product image -->
<?php
$slug = isset($_GET['slug']) ? mysqli_real_escape_string($con, $_GET['slug']) : '';

$currentCat = null;

if ($slug != '') {
    $catQuery = mysqli_query($con, "SELECT * FROM tbl_top_category WHERE slug = '$slug' LIMIT 1");
    $currentCat = mysqli_fetch_assoc($catQuery);
}
?>

<section class="feature-one">
    <div class="container">

        <div class="row g-4">

            <?php
            if ($currentCat) {

                // Category ID
                $catId = $currentCat['t_id'];

                // Category slug (IMPORTANT for URL)
                $service_slug = $currentCat['slug'];

                // Fetch products
                $products = mysqli_query($con, "SELECT * FROM product WHERE t_id = '$catId'");

                if (mysqli_num_rows($products) > 0) {

                    while ($row = mysqli_fetch_assoc($products)) {
            ?>

                        <div class="col-12 col-md-3 col-lg-3">
                            <div class="tech-boxs">
                                <a href="<?php echo path() . $service_slug . '/' . $row['slug']; ?>">
                                    <img src="<?php echo path(); ?>admin/assets/images/service/<?php echo $row['Image']; ?>" alt="">
                                </a>

                                <!-- SEO Friendly URL -->
                                <a href="<?php echo path() . $service_slug . '/' . $row['slug']; ?>">
                                    <h5><?php echo $row['title']; ?></h5>
                                </a>

                            </div>
                        </div>

            <?php
                    }
                } else {
                    echo "<div class='col-12 mb-5'><h4 class='text-center'>No products found in this category.</h4></div>";
                }
            } else {
                echo "<div class='col-12'><h4 class='text-center'>Please select a category.</h4></div>";
            }
            ?>

        </div>
    </div>
</section>

<?php
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', '1');
// error_reporting(E_ALL);
$serviceQuery = mysqli_query(
    $con,
    "SELECT *
     FROM tbl_top_category
     WHERE slug='$slug'"
);

$service = mysqli_fetch_assoc($serviceQuery);

$category_id = $service['t_id'];
$locationQuery = mysqli_query(
    $con,
    "SELECT *
     FROM tbl_location_pages
     WHERE category_id='$category_id'
     AND status='1'
     ORDER BY location_name ASC"
);
?> 

<section class="location-section text-center">
    <div class="container">
        
    <h2>
        <?= $service['cat_name']; ?> Locations
    </h2>

    <div class="row">

    <?php
    while($location=mysqli_fetch_assoc($locationQuery))
    {
        
    ?>

        <div class="col-md-3 my-3">

            <a href="<?php echo path() . 'services/' . $slug . '/' . $location['slug']; ?>" class="location-card">

                <?= $service['cat_name']; ?>
                in
                <?= $location['location_name']; ?>

            </a>

        </div>

    <?php
    }
    ?>

    </div>
    
    </div>


</section>

<!--Feature One End-->
<section class="mb-5">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="comment-form text-center">
                    <h3 class="comment-form__title">GET IN TOUCH</h3>
                    <p class="comment-form__text text-center">By using form u agree with the message sorage, you can
                        contact us directly now</p>
                    <form action="" class="comment-one__form contact-form-validated" novalidate="novalidate">
                        <div class="row">
                            <div class="col-xl-6">
                                <div class="comment-form__input-box">
                                    <input type="text" placeholder="Your Name..." name="name" required=""
                                        oninput="this.value = this.value.replace(/[^a-zA-Z\s]/g, '')">
                                </div>
                            </div>
                            <div class="col-xl-6">
                                <div class="comment-form__input-box">
                                    <input type="email" placeholder="Your Email..." name="email" required="">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xl-6">
                                <div class="comment-form__input-box">
                                    <input type="text" placeholder="Your Phone..." name="phone"
                                        maxlength="10" oninput="this.value = this.value.replace(/[^0-9]/g, '')" required="">
                                </div>
                            </div>
                            <div class="col-xl-6">
                                <div class="comment-form__input-box">
                                    <input type="text" placeholder="Your Subject..." name="subject" value="<?php echo $cat_name; ?>" required="">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xl-12">
                                <div class="comment-form__input-box text-message-box">
                                    <textarea name="message" placeholder="Write your messege" required=""></textarea>
                                </div>
                                <div class="comment-form__btn-box">
                                    <button type="submit" name="submit" class="thm-btn comment-form__btn">submit
                                        now <span></span>
                                        <span></span>
                                        <span></span>
                                        <span></span>
                                        <span></span> </button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="result"></div>
                </div>
            </div>
        </div>
    </div>
</section>


<?php
include("include/footer.php");
?>