<?php
/**
 * @package SP Page Builder
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2016 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */

//no direct accees
defined('_JEXEC') or die('restricted aceess');

class SppagebuilderAddonAppointment_form extends SppagebuilderAddons {
    public function render() {
      $title            = (isset($this->addon->settings->title) && $this->addon->settings->title) ? $this->addon->settings->title : '';
      $heading_selector = (isset($this->addon->settings->heading_selector) && $this->addon->settings->heading_selector) ? $this->addon->settings->heading_selector : '';
      $class            = (isset($this->addon->settings->class) && $this->addon->settings->class) ? $this->addon->settings->class : '';
      $variation        = (isset($this->addon->settings->variation) && $this->addon->settings->variation) ? $this->addon->settings->variation : '';
      $recipient_email  = (isset($this->addon->settings->recipient_email) && $this->addon->settings->recipient_email) ? $this->addon->settings->recipient_email : '';
      $formcaptcha      = (isset($this->addon->settings->formcaptcha) && $this->addon->settings->formcaptcha) ? $this->addon->settings->formcaptcha : '';
      $message_box      = (isset($this->addon->settings->message_box) && $this->addon->settings->message_box) ? $this->addon->settings->message_box : '';
      $select_subject   = (isset($this->addon->settings->select_subject) && $this->addon->settings->select_subject) ? $this->addon->settings->select_subject : '';
      $captcha_question = (isset($this->addon->settings->captcha_question) && $this->addon->settings->captcha_question) ? $this->addon->settings->captcha_question : '';
      $captcha_answer   = (isset($this->addon->settings->captcha_answer) && $this->addon->settings->captcha_answer) ? $this->addon->settings->captcha_answer : '';
      $show_placeholder = (isset($this->addon->settings->show_placeholder) && $this->addon->settings->show_placeholder) ? $this->addon->settings->show_placeholder : '';
      $show_label       = (isset($this->addon->settings->show_label) && $this->addon->settings->show_label) ? $this->addon->settings->show_label : '';
      $show_tac       = (isset($this->addon->settings->show_tac) && $this->addon->settings->show_tac) ? $this->addon->settings->show_tac : '';
      $tac_text       = (isset($this->addon->settings->tac_text) && $this->addon->settings->tac_text) ? $this->addon->settings->tac_text : '';


      //output start
      $output = '';
      $output .= '<div class="sppb-addon sppb-addon-appointment-form ' . $class . '">'; //sppb-addon-appointment-form
      if ($title) {
          $output .= '<' . $heading_selector . ' class="sppb-addon-title">' . $title . '</' . $heading_selector . '>';
      }
      //For Placeholder
      $name_ph = '';
      $number_ph = '';
      $date_ph = '';
      $email_ph = '';
      $message_ph = '';
      if ($show_placeholder) {
          $name_ph = JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_NAME');
          $number_ph = JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_NUMBER');
          $date_ph = JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_DATE');
          $email_ph = JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_EMAIL');
          $message_ph = JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_MESSAGE');
      }

      $output .= '<div class="sppb-addon-content">'; //sppb-addon-content
        $output .= '<form class="sppb-appointment-form ' . $variation . '">'; //Apointment Form

        if ($select_subject) {
            $output .= '<div class="sppb-form-group">'; //sppb-form-group
            if ($show_label) {
                $output .= '<label for="subject">' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT') . '</label>';
            }
            $output .= '<select name="subject" id="subject" class="form-control sp-select">
    						<option value="' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_1') . '" selected="selected">
    						' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_1') . '</option>
    						<option value="' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_2') . '"> ' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_2') . '</option>
    						<option value="' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_3') . '"> ' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_3') . '</option>
    						<option value="' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_4') . '"> ' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUBJECT_4') . '</option>
    					</select>';
            $output .= '</div>'; //end sppb-form-group
        }

        $output .= '<div class="sppb-form-group">'; //sppb-form-group
        if ($show_label) {
            $output .= '<label for="name">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_NAME') . '</label>';
        }
        $output .= '<input type="text" name="name" id="name" class="sppb-form-control" placeholder="' . $name_ph . '" required="required">';
        $output .= '</div>'; //end sppb-form-group

        $output .= '<div class="sppb-form-group">'; //sppb-form-group
        if ($show_label) {
            $output .= '<label for="number">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_NUMBER') . '</label>';
        }
        $output .= '<input type="tel" name="number" id="number" class="sppb-form-control" placeholder="' . $number_ph . '" required="required">';
        $output .= '</div>'; //end sppb-form-group

        $output .= '<div class="sppb-form-group">'; //sppb-form-group
        if ($show_label) {
            $output .= '<label for="date">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_DATE') . '</label>';
        }
        $output .= '<input type="text" name="date" id="date" class="sppb-form-control" placeholder="' . $date_ph . '" required="required">';
        $output .= '</div>'; //end sppb-form-group

        $output .= '<div class="sppb-form-group">'; //sppb-form-group
        if ($show_label) {
            $output .= '<label for="email">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_EMAIL') . '</label>';
        }
        $output .= '<input type="email" name="email" id="email" class="sppb-form-control" placeholder="' . $email_ph . '" required="required">';
        $output .= '</div>'; //end sppb-form-group


        if ($formcaptcha) {
            $output .= '<div class="sppb-form-group">'; //sppb-form-group
            if ($show_label) {
                $output .= '<label for="captcha_question">' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_CAPTCHA') . '</label>';
            }
            $output .= '<input type="text" name="captcha_question" id="captcha_question" class="sppb-form-control" placeholder="' . $captcha_question . '" required="required">';
            $output .= '</div>'; //end sppb-form-group
        }
        if ($message_box) {
            $output .= '<div class="sppb-form-group textarea">'; //sppb-form-group
            if ($show_label) {
                $output .= '<label for="message">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_PH_MESSAGE') . '</label>';
            }
            $output .= '<textarea type="text" name="message" id="message" rows="5" class="sppb-form-control" placeholder="' . $message_ph . '" required="required"></textarea>';
            $output .= '</div>'; //end sppb-form-group
        }

        $output .= '<input type="hidden" name="recipient" value="' . base64_encode($recipient_email) . '">';

        if ($formcaptcha) {
            $output .= '<input type="hidden" name="captcha_answer" value="' . md5($captcha_answer) . '">';
        }

        if($show_tac) {
            $output .= '<div class="sppb-form-group tac-wrapper">';
                $output .= '<div class="input-field">';
                    $output .= '<label class="custom-checkbox">';
                        $output .= '<input type="checkbox" id="tac" name="tac" value="tac" required="true" data-apptac="true">';
                        $output .= '<span>' . $tac_text . '</span>';
                    $output .= '</label>';
                $output .= '</div>';

        }

        $output .= '<div class="sppb-form-btn">'; //sppb-form-btn
        $output .= '<button type="submit" class="sppb-btn sppb-btn-primary"><i class="fa"></i> ' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SEND') . '</button>';
        $output .= '</div>'; //end sppb-form-btn
        $output .= '</form>'; //End Apointment Form
        $output .= '<div style="display:none;margin-top:10px;" class="sppb-ajax-contact-status"></div>';
      $output .= '</div>'; //end sppb-addon-content
      $output .= '</div>'; //end sppb-addon-appointment-form

      return $output;
    }

    public static function getAjax() {
        $input = JFactory::getApplication()->input;
        $mail = JFactory::getMailer();
        $showcaptcha = false;

        //inputs
        $inputs = $input->get('data', array(), 'ARRAY');
        foreach ($inputs as $input) {

            if ($input['name'] == 'recipient') {
                $recipient = base64_decode($input['value']);
            }

            if ($input['name'] == 'number') {
                $number = $input['value'];
            }

            if ($input['name'] == 'subject') {
                $subject = nl2br($input['value']);
            }

            if ($input['name'] == 'date') {
                $date = $input['value'];
            }

            if ($input['name'] == 'email') {
                $email = $input['value'];
            }

            if ($input['name'] == 'name') {
                $name = $input['value'];
            }

            if ($input['name'] == 'message') {
                $message = nl2br($input['value']);
            }

            if ($input['name'] == 'captcha_question') {
                $captcha_question = $input['value'];
                $showcaptcha = true;
            }

            if ($input['name'] == 'captcha_answer') {
                $captcha_answer = $input['value'];
                $showcaptcha = true;
            }
        }

        if ($showcaptcha) {
            if (md5($captcha_question) != $captcha_answer) {
                return '<span class="sppb-text-danger">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_WRONG_CAPTCHA') . '</span>';
            }
        }

        // generate message
        $message_body = '';
        if (isset($message) && $message) {
            $message_body .= '<p>';
            $message_body .= $message;
            $message_body .= '</p>';
        }
        $message_body .= '<p>';
        $message_body .= nl2br(JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_APPNT_NAME') . ': ' . $name) . '<br>';
        if (isset($subject) && $subject) {
            $message_body .= nl2br(JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_APPNT_SUBJECT') . ': ' . $subject) . '<br>';
        }
        $message_body .= nl2br(JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_APPNT_PHONE_NO') . ': ' . $number) . '<br>';
        $message_body .= nl2br(JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_APPNT_DATE') . ': ' . $date) . '<br>';
        $message_body .= '</p>';

        $sender = array($email, $name);
        $mail->setSender($sender);
        $mail->addRecipient($recipient);
        $mail->isHTML(true);
        $mail->Encoding = 'base64';
        $mail->setBody($message_body);

        if ($mail->Send()) {
            return '<span class="sppb-text-success">' . JText::_('COM_SPPAGEBUILDER_ADDON_APPNT_FORM_SUCCESS') . '</span>';
        } else {
            return '<span class="sppb-text-danger">' . JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_FAILED') . '</span>';
        }
    }
}
