<?php

namespace Drupal\restaurant_bookings\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\restaurant\Form\AppForm;
use Drupal\restaurant\Bookings\Booking;

/**
* BOOKINGS FORM STEP 1
*/
class Guests extends FormBase {

  /**
   * 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 'add_booking';
  }
  
  public function getSetting()
  {
  	
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
  	 $db = \Drupal::database();
  	 
    if ($this->getSetting('requirelogin')) {

      if (! \Drupal::currentUser()->isAuthenticated()) {
        $this->addMessage($this->t('Login required to make a booking'), 'error')->http_redirect('/user/login');
      }
    }

    $services = $db->query("SELECT DISTINCT name FROM restaurant_opening_times")->fetchCol();

    $options = array();
    foreach ($services as $val) {
      $options[$val] = $val;
    }

    //$form = parent::buildForm($form, $form_state);
    
    $form['#action'] = '/nojs/times';

    $form['date'] = array(
      '#type' => 'date',
      '#title' => $this->t('Booking Date'),
      '#required' => true,
      '#default_value' => date('Y-m-d'),
    );

    $form['guests'] = array(
      '#width' => 100,
      '#type' => 'number',
      '#title' => $this->t('Guests'),
      '#min' => 1,
      '#max' => $db->query("SELECT value FROM restaurant_settings WHERE name = ?", array('maxbookings'))->fetchField(),
      '#attributes' => array('class' => array('edit-guests')),
      '#required' => true
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Select Time'),
      '#attributes' => array('class' => array('edit-button'))
    );

    return $form;
  }


  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    /*$bookings = new Booking($this->conn);
    $times = $bookings->getTimes($_POST['date'], true);
    $bookings->filterTimes($times, $_POST['date'], $_POST['guests']);

    if (! empty ($times)) {
      $this->store->set('times', $times[$_POST['service']]['Times']);
      $this->store->set('guests', $form_state->getValue('guests'));
      $this->store->set('date', $form_state->getValue('date'));
      $this->store->set('service', $form_state->getValue('service'));

      if ($this->inAdminPath()) {
        $this->http_redirect('/admin/restaurant/bookings/times');
      } else {
        $this->http_redirect('/bookings/selecttime');
      }
    } else {
      $this->addMessage($this->t("No times are available for the date you selected"), "error");
    }
    
    */
  }




}
