<?php

namespace Drupal\restaurant_bookings\Form\Ajax;

use Drupal\restaurant_bookings\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\restaurant\Form\AppForm;
use Drupal\restaurant\Bookings\Booking;
use Symfony\Component\HttpFoundation\Session\Session;
use Drupal\restaurant\Form\MultipartForm;

use Drupal\restaurant_bookings\Form\FormElements;
use Drupal\restaurant\Form\Heading;


/**
* BOOKINGS FORM STEP 1
*/
class Data extends AppForm
{
  /**
   * Returns a unique string identifying the form.
   *
   * The returned ID should be a unique string that can be a valid PHP function
   * name, since it's used in hook implementation names such as
   * hook_form_FORM_ID_alter().
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'user_data';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {

    if ($this->getSetting('requirelogin')) {

      if (! \Drupal::currentUser()->isAuthenticated()) {
          header("HTTP/1.1 401 Unauthorized");
          exit;
      }
    }


    $session = $this->getSession();

    if ($this->hasParam('timestamp')) {
    	$session->set('date', $this->getParam('timestamp'));

    }

    $message = array();
    $valid = true;

    if (isset ($_GET['form_id'])) {

        $required = array('firstname', 'lastname', 'phone', 'email');
        $valid = true;


        foreach ($required as $val) {
            if (empty ($_GET[$val])) {
              $valid = false;
              if ($val == 'email') {
                break;
              }
              $message[$val] = $val . ' is required';
            }
        }
        if (filter_var($_GET['email'], FILTER_VALIDATE_EMAIL) === false) {
          $valid = false;
          $message['email'] = 'Valid email is required';
        }

        $session->set('details', $_GET);

        if ($valid) {
          $this->http_redirect('/booking/ajax/requirements/' . $session->get('date'));
        }

	}


    $details = ($session->has('details')) ? $session->get('details') : array();

    $form['top'] = array(
        '#type' => 'markup',
        '#markup' => '<div class="shadow" id="bookings-div-right">'
    );

    $msg = ($valid) ? null : 'Please enter all required fields<br>';
    $form = FormElements::Details($form, $details, array(), $msg);

     $form['linebreak'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="linebreak"></div>'
    );

    $form = FormElements::Buttons($form, $session->get('date'));

    return $form;

  }

  /*public function ajaxCallback(array &$form, FormStateInterface $form_state)
  {
     //$this->http_redirect('/booking/ajax/requirements/' . $this->getSession()->get('date'));
     $response = new AjaxResponse();
     return $response;
    //mydump ('hello world');
  } */

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
      if (! empty ($_POST)) {
          $this->getSession()->set('details', $_POST);
          $this->http_redirect('/booking/ajax/requirements/' . $this->getSession()->get('date'));
      }

  }

}
