<?php

namespace Drupal\restaurant_settings\Form;

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

class Bookings 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 'bookings_settings';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    /*
     * Length of a booking. This will close off the table booked for the period specified.
     * Front end is at /admin/restaurant/settings/bookings under 'Maximum booking length'
     */
    $len = array('15 minutes','30 minutes','45 minutes','1 hour','2 hours','3 hours','4 hours');
    $bookinglength = array();
    foreach ($len as $val) {
      $bookinglength[$val] = $val;
    }
    $bookinglength['entire'] = 'entire service period';

    /**
     * Booking offset.
     * Take bookings until $bookingoffset before booking. Front end is at /admin/restaurant/settings/bookings 'Take online bookings until'
     * default is 'any (allow bookings right up until current time)'
     */
    $bookingoffset = array(
      '1 hour' => '1 hour before booking',
      '2 hours' => '2 hours before booking',
      '4 hours' => '4 hours before booking',
      '6 hours' => '6 hours before booking',
      '12 hours' => '12 hours before booking',
      '1 day' => '1 day before booking',
      'any' => 'allow bookings right up until current time'
    );

    $form['bookinglength'] = [
      '#type' => 'select',
      '#null' => false,
      '#default_value' => $this->getSetting('bookinglength'),
      '#title' => $this->t('Maximum booking length'),
      '#description' => $this->t('Choose a maximum time a table may be booked for in any 1 service period. Choose \'entire service period\' if a table may only be booked once per service time'),
      '#options' => $bookinglength,
      '#required' => true
    ];

    $form['bookuntil'] = array(
        '#type' => 'radios',
        '#title' => t('Choose the period to take bookings for'),
        '#options' => array(
          'T' => $this->t('Take bookings until Maximum booking length (above) from end of service time'),
          'E' => $this->t('Take bookings up to (and including) the end of service time')
      ),
      '#required' => true,
      '#default_value' => $this->getSetting('bookuntil')
    );



    $form['maxbookings'] = array(
      '#type' => 'number',
      '#title' => 'Maximum guests for online bookings',
      '#required' => true,
      '#default_value' => $this->getSetting('maxbookings')
    );

    $form['takebookingsuntil'] = [
      '#type' => 'select',
      '#null' => false,
      '#default_value' => $this->getSetting('takebookingsuntil'),
      '#title' => $this->t('Take online bookings until'),
      '#options' => $bookingoffset,
      '#required' => true
    ];

    /*$form['multipletables'] = [
        '#type' => 'radios',
        '#title' => 'Allow multiple table bookings',
        '#options' => array(
          0 => 'No',
          1 => 'Yes'
        ),
        '#required' => true,
        '#default_value' => $this->getSetting('multipletables')
    ]; */

    $form['requirelogin'] = [
        '#type' => 'radios',
        '#title' => 'Require login',
        '#options' => array(
          0 => 'No',
          1 => 'Yes'
        ),
        '#required' => true,
        '#default_value' => $this->getSetting('requirelogin')
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
      '#attributes' => array('class' => array('use-ajax-submit6'))
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $this->setSetting('bookinglength', $form_state->getValue('bookinglength'));
    $this->setSetting('bookuntil', $form_state->getValue('bookuntil'));
    $this->setSetting('maxbookings', $form_state->getValue('maxbookings'));
    $this->setSetting('takebookingsuntil', $form_state->getValue('takebookingsuntil'));
    //$this->setSetting('multipletables', $form_state->getValue('multipletables'));
    $this->setSetting('requirelogin', $form_state->getValue('requirelogin'));

    $this->addMessage("Saved bookings settings")->http_redirect('/admin/restaurant/settings');
  }

}
