<?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 Summary 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_summary';
  }

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

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

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

    $form['#cache']['max-age'] = 0;

	$config = \Drupal::config('system.site');
	$sitename = $config->get('name');

	if (! $this->hasParam('timestamp')) {
		throw new Exception("No timestamp supplied");
	}

	$session = $this->getSession();

	$date = date('d/m/Y', $this->getParam('timestamp'));
	$time = date('h:ia', $this->getParam('timestamp'));
	$details = $session->get('details');


	$phone = (! empty ($details['phone'])) ? '<dt>Phone</dt><dd>' . $details['phone'] . '</dd>' : '';
	$company = (! empty ($details['company'])) ? '<dt>Company</dt><dd>' . $details['company'] . '</dd>' : '';

	$db = \Drupal::database();
	$requirements = array();
	if (! empty ($session->get('checkrequirements'))) {
		foreach ($session->get('checkrequirements') as $val) {
			$requirements[] = $db->query("SELECT requirement FROM restaurant_requirements WHERE id = ?", array($val))->fetchField();
		}
	}
	if (! empty ($session->get('requirements'))) {
		$requirements[] = $session->get('requirements');
	}
	$req = (! empty ($requirements)) ? '<dt>Requirements</dt><dd> ' . implode(', ', $requirements) . '</dd>' : '';


$str = <<<HTML

<dl class="summary">

 <dt>Venue</dt>
 <dd>{$sitename}</dd>

 <dt>Name</dt>
 <dd>{$details['firstname']} {$details['lastname']}</dd>

 <dt>Date</dt>
 <dd>{$date}</dd>

 <dt>Time</dt>
 <dd>{$time}</dd>

 <dt>Guests</dt>
 <dd>{$session->get('guests')}</dd>

 {$phone}

 {$company}

 <dt>Email</dt>
 <dd>{$details['email']}</dd>

 {$req}


</dl>

HTML;


	$form['text'] = array(
		'#type' => 'markup',
		'#markup' => $str
	);

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

	$form = FormElements::Buttons($form, $this->getParam('timestamp'));

    return $form;

  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {



		//mydump ($_POST);
  }

}
