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

if (!isset($_GET['slug'])) {
    die("Invalid Request");
}

$slug = mysqli_real_escape_string($con, $_GET['slug']);

$ser = mysqli_query($con, "SELECT * FROM product WHERE slug='$slug'");
$row = mysqli_fetch_assoc($ser);

if (!$row) {
    die("Product not found");
}

$title       = $row['title'];
$slug        = $row['slug'];
$Image       = $row['Image'];
$p_id        = $row['p_id'];
$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>
    .service-details__left p {
        margin-bottom: 15px;
        text-align: justify;
    }

    .service-details__img img {
        aspect-ratio: 2 / 1;
    }

    h3 {
        margin-bottom: 15px;
        margin-top: 15px;
    }
</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 $title; ?></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 $title; ?></li>
                </ul>
            </div>
        </div>
    </div>
</section>
<!--Page Header End-->

<!--Service Details Start-->
<section class="service-details">
    <div class="container">
        <div class="row d-flex flex-row-reverse">

            <div class="col-xl-8 col-lg-7">
                <div class="service-details__left">
                    <div class="service-details__img">
                        <img src="<?php path(); ?>/admin/assets/images/service/<?php echo htmlspecialchars($Image); ?>" alt="">
                    </div>
                    <!-- <h3 class="service-details__title-1"><php echo htmlspecialchars($title); ?></h3> -->
                    <p class="service-details__text-1"><?php echo $description; ?></p>


                </div>
            </div>
            <div class="col-xl-4 col-lg-5">
                <div class="service-details__sidebar">
                    <div class="service-details__services-box">
                        <h3 class="service-details__services-title">Our Services</h3>
                        <ul class="service-details__services-list list-unstyled">
                            <?php

                            $prods = mysqli_query($con, "SELECT slug, title FROM product WHERE t_id = '$t_id' ORDER BY p_id ASC");

                            $currentSlug = $slug;

                            if ($prods && mysqli_num_rows($prods) > 0) {

                                while ($s = mysqli_fetch_assoc($prods)) {

                                    $prodSlug  = htmlspecialchars($s['slug'], ENT_QUOTES, 'UTF-8');
                                    $prodTitle = htmlspecialchars($s['title'], ENT_QUOTES, 'UTF-8');

                                    $activeClass = ($currentSlug === $s['slug']) ? 'active' : '';

                                    // Proper URL
                                    $url =  $prodSlug;
                            ?>

                                    <li class="<?php echo $activeClass; ?>">
                                        <a href="<?php echo $url; ?>">
                                            <?php echo $prodTitle; ?>
                                            <span class="icon-next"></span>
                                        </a>
                                    </li>

                            <?php
                                }
                            } else {
                                echo "<li>No products found</li>";
                            }
                            ?>
                        </ul>
                    </div>

                    <div class="comment-form text-center">
                        <h4 class="comment-form__title">GET IN TOUCH</h4>
                        <p class="comment-form__text">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-12">
                                    <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-12">
                                    <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-12">
                                    <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-12">
                                    <div class="comment-form__input-box">
                                        <input type="text" placeholder="Your Subject..." name="subject" value="<?php echo $title; ?>" 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>
    </div>
</section>
<!--Service Details End-->



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