<?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 Requirements 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_requirements';
  }

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

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

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

    //mydump (date('Y-m-d H:i', $this->getSession()->get('date')));

    if (isset ($_GET['form_id'])) {
        $requirements_options = (isset ($_GET['checkrequirements'])) ? array_keys($_GET['checkrequirements']) : array();
        $this->getSession()->set('checkrequirements', $requirements_options);
        $this->getSession()->set('requirements', $_GET['requirements']);
        $this->http_redirect('/booking/ajax/summary/' . $this->getSession()->get('date'));
    }
    
    $text = $this->getSession()->has('requirements') ? $this->getSession()->get('requirements') : '';
    $req = $this->getSession()->has('checkrequirements') ? $this->getSession()->get('checkrequirements') : '';
    $form = FormElements::Notes($form, '', array('textarea' => $text, 'selected' => $req));
    $form = FormElements::Buttons($form, $this->getSession()->get('date', $this->getParam('timestamp')));
    
    return $form;

  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
        if (isset ($_POST)) {
			$requirements_options = (isset ($_POST['checkrequirements'])) ? array_keys($_POST['checkrequirements']) : array();
			
			$this->getSession()->set('checkrequirements', $requirements_options);
			$this->getSession()->set('requirements', $_POST['requirements']);
			
			$this->http_redirect('/booking/ajax/summary/' . $this->getSession()->get('date'));	
		}
      	
  }

}
