<?php
$currentPage = 'contact';
$pageTitle = 'Contact Us | Vivora Tools';
$pageDescription = 'Get in touch with the Vivora Tools team to request new utilities or share feedback.';
$canonicalPath = 'contact-us';
$bodyClass = 'contact-page';
$breadcrumbs = [
    ['name' => 'Home', 'url' => ''],
    ['name' => 'Contact Us', 'url' => 'contact-us'],
];

$contactStatus = null;
$contactErrors = [];
$contactValues = [
    'name' => '',
    'email' => '',
    'reason' => '',
    'message' => '',
];
$reasonLabels = [
    'tool-request' => 'Request a new tool',
    'bug-report' => 'Report a bug',
    'general-feedback' => 'General feedback',
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $contactValues = [
        'name' => trim((string) ($_POST['name'] ?? '')),
        'email' => trim((string) ($_POST['email'] ?? '')),
        'reason' => trim((string) ($_POST['reason'] ?? '')),
        'message' => trim((string) ($_POST['message'] ?? '')),
    ];

    if ($contactValues['name'] === '') {
        $contactErrors[] = 'Please enter your full name.';
    }

    if ($contactValues['email'] === '' || !filter_var($contactValues['email'], FILTER_VALIDATE_EMAIL)) {
        $contactErrors[] = 'Please enter a valid email address.';
    }

    if ($contactValues['reason'] === '' || !isset($reasonLabels[$contactValues['reason']])) {
        $contactErrors[] = 'Please select a valid reason.';
    }

    if ($contactValues['message'] === '') {
        $contactErrors[] = 'Please enter your message.';
    }

    if (!$contactErrors) {
        $reasonLabel = $reasonLabels[$contactValues['reason']];
        $submittedAt = date('Y-m-d H:i:s T');
        $safeEmail = str_replace(["\r", "\n"], '', $contactValues['email']);
        $safeName = str_replace(["\r", "\n"], '', $contactValues['name']);
        $subject = 'New Vivora Tools Contact Message - ' . $reasonLabel;
        $body = implode("\n", [
            'Hello Vivora Tools Team,',
            '',
            'A new message was submitted from the Vivora Tools contact form.',
            '',
            'Name: ' . $contactValues['name'],
            'Email: ' . $contactValues['email'],
            'Reason: ' . $reasonLabel,
            'Submitted At: ' . $submittedAt,
            '',
            'Message:',
            $contactValues['message'],
            '',
            '---',
            'This email was generated automatically from https://vivoratools.in/contact-us',
        ]);
        $headers = [
            'From: Vivora Tools <no-reply@vivoratools.in>',
            'Reply-To: ' . $safeName . ' <' . $safeEmail . '>',
            'MIME-Version: 1.0',
            'Content-Type: text/plain; charset=UTF-8',
            'X-Mailer: PHP/' . phpversion(),
        ];

        if (@mail('vivoratools@gmail.com', $subject, $body, implode("\r\n", $headers))) {
            $contactStatus = 'success';
            $contactValues = [
                'name' => '',
                'email' => '',
                'reason' => '',
                'message' => '',
            ];
        } else {
            $contactStatus = 'error';
            $contactErrors[] = 'We could not send your message right now. Please try again later.';
        }
    } else {
        $contactStatus = 'error';
    }
}
require __DIR__ . '/includes/head.php';
require __DIR__ . '/includes/header.php';
?>

<section class="page-hero pt-16 pb-6">
  <div class="container">
    <div class="text-center">
      <ul class="breadcrumb inline-flex h-8 items-center justify-center space-x-2 rounded-3xl bg-theme-light px-4 py-2">
        <li class="leading-none text-dark">
          <a class="inline-flex items-center text-primary" href="/">
            <i class="fa-solid fa-house breadcrumb-home-icon" aria-hidden="true"></i>
            <span class="text-sm leading-none">Home</span>
          </a>
        </li>
        <li class="leading-none text-dark">
          <span class="text-sm leading-none">/ Contact Us</span>
        </li>
      </ul>
    </div>
    <div class="page-hero-content mx-auto max-w-[768px] text-center">
      <h1 class="mb-5 mt-8">Contact our team</h1>
      <p>
        Have a tool idea or feedback on the site? Send us a note and we will review it.
      </p>
    </div>
  </div>
</section>

<section class="section pt-0">
  <div class="container">
    <div class="row items-start">
      <div class="mb-10 md:col-6 md:mb-0 md:pt-9">
        <div class="contact-img relative inline-flex pl-5 pb-5">
          <img src="images/contact-img.png" alt="Contact illustration" />
          <img class="absolute bottom-0 left-0 -z-[1] h-14 w-14" src="images/shape-2.svg" alt="" />
        </div>
      </div>
      <div class="md:col-6">
        <?php if ($contactStatus === 'success'): ?>
        <div class="contact-alert contact-alert-success mb-5" role="status">
          Thank you. Your message has been sent to the Vivora Tools team.
        </div>
        <?php elseif ($contactErrors): ?>
        <div class="contact-alert contact-alert-error mb-5" role="alert">
          <strong>Please fix the following:</strong>
          <ul class="mt-2">
            <?php foreach ($contactErrors as $error): ?>
            <li><?php echo htmlspecialchars($error, ENT_QUOTES); ?></li>
            <?php endforeach; ?>
          </ul>
        </div>
        <?php endif; ?>

        <form class="lg:max-w-[484px]" action="/contact-us" method="POST">
          <div class="form-group mb-5">
            <label class="form-label" for="name">Full Name</label>
            <input class="form-control" type="text" id="name" name="name" placeholder="Your full name" value="<?php echo htmlspecialchars($contactValues['name'], ENT_QUOTES); ?>" required />
          </div>
          <div class="form-group mb-5">
            <label class="form-label" for="email">Email Address</label>
            <input class="form-control" type="email" id="email" name="email" placeholder="Your email address" value="<?php echo htmlspecialchars($contactValues['email'], ENT_QUOTES); ?>" required />
          </div>
          <div class="form-group mb-5">
            <label class="form-label" for="reason">Reason / purpose</label>
            <select name="reason" id="reason" class="form-select" required>
              <option value="">Select a reason</option>
              <?php foreach ($reasonLabels as $reasonValue => $reasonLabel): ?>
              <option value="<?php echo htmlspecialchars($reasonValue, ENT_QUOTES); ?>" <?php echo $contactValues['reason'] === $reasonValue ? 'selected' : ''; ?>>
                <?php echo htmlspecialchars($reasonLabel, ENT_QUOTES); ?>
              </option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group mb-5">
            <label class="form-label" for="message">Message</label>
            <textarea class="form-control h-[150px]" id="message" name="message" cols="30" rows="10" placeholder="Tell us how we can help" required><?php echo htmlspecialchars($contactValues['message'], ENT_QUOTES); ?></textarea>
          </div>
          <input class="btn btn-primary block w-full" type="submit" value="Send Message" />
        </form>
      </div>
    </div>
  </div>
</section>

<?php require __DIR__ . '/includes/footer.php'; ?>
