<?php
session_start();
require_once 'phpmailer/class.phpmailer.php';
require_once 'phpmailer/class.smtp.php';

function send_email( $email, $reply, $subject, $text, $attachment = null )
{
  $mail = new PHPMailer;
  $mail -> IsSMTP();
  $mail -> SMTPAuth   = true;
  $mail -> Host       = 'mail.ostal.pl';
  $mail -> Port = 25;
  $mail -> Username   = 'konfigurator@ostal.pl';
  $mail -> Password   = 'x6rpeUG5tW';
  $mail -> CharSet = "UTF-8";
  $mail -> SMTPOptions = array(
    'ssl' => array(
      'verify_peer' => false,
      'verify_peer_name' => false,
      'allow_self_signed' => true
    )
  );
  $mail -> From = 'konfigurator@ostal.pl';
  $mail -> FromName = 'Ostal';
  $mail -> addAddress( $email, $email );
  $mail -> addReplyTo( $reply, $reply );
  $mail -> isHTML( true );
  $mail -> Subject = $subject;
  $mail -> Body    = $text;
  
  if ($attachment && isset($attachment['tmp_name']) && file_exists($attachment['tmp_name'])) {
    $mail->addAttachment($attachment['tmp_name'], $attachment['name']);
  }

  return $mail -> send();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $configData = json_decode($_POST['configData'], true);

  $file = isset($_FILES['attachment']) ? $_FILES['attachment'] : null;

  $to = 'kontakt@ostal.pl';
  $subject = 'ostal.pl - Konfigurator';
  $message = '<p>' . $configData['step_1'] . '</p>';

  $message .= '<p>Wymiary</p>';
  $message .= '<ul>';
  if (isset($configData['step_2']['width']) && $configData['step_2']['width']) {
    $message .= '<li>Szerokość X : ' . $configData['step_2']['width'] . 'mm </li>';
  }
  if (isset($configData['step_2']['length']) && $configData['step_2']['length']) {
      $message .= '<li>Głębokość Y : ' . $configData['step_2']['length'] . 'mm </li>';
  }
  if (isset($configData['step_2']['height']) && $configData['step_2']['height']) {
    $message .= '<li>Wysokość Z : ' . $configData['step_2']['height'] . 'mm </li>';
  }
  if (isset($configData['step_2']['entry']) && $configData['step_2']['entry']) {
      $message .= '<li>Wjazd: ' . $configData['step_2']['entry'] . '</li>';
  }
  $message .= '</ul>';

  $message .= '<p>' . $configData['step_3'] . '</p>';

  if (isset($configData['step_3_extra']) && $configData['step_3_extra']) {
    $message .= '<p>' . $configData['step_3_extra'] . '</p>';
  }

  $message .= '<p>Dane kontaktowe</p>';
  $message .= '<ul>';
  $message .= '<li>Miejsce montażu: ' . $configData['step_4']['miejsce_montazu'] . '</li>';
  $message .= '<li>Imię: ' . $configData['step_4']['name'] . '</li>';
  $message .= '<li>E-mail: ' . $configData['step_4']['email'] . '</li>';
  $message .= '<li>Telefon: ' . $configData['step_4']['phone'] . '</li>';
  $message .= '</ul>';

  if (send_email($to, $configData['step_4']['email'], $subject, $message, $file)) {
      $_SESSION["configurator_sended"] = true;
      echo json_encode(['status' => 'ok', 'message' => 'Wiadmość wysłana']);
  } else {
      echo json_encode(['status' => 'error', 'message' => 'Wystąpił błąd podczas wysyłania wiadomości']);
  }
  exit();
} else {
  echo json_encode(['status' => 'error', 'message' => 'Nieprawidłowe żądanie']);
  exit();
}