<?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;

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

class EditBooking 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 'edit_booking';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $booking = (array)$this->conn->query("SELECT * FROM restaurant_bookings WHERE id = ?", array($this->getParam('bookingid')))->fetch();
    
    $descriptions = array(
        'firstname' => 'Customer first name',
        'lastname'  => 'Customer last name',
        'email'     => 'Valid email address',
        'phone'     => 'Customer phone number',
        'title'     => 'Enter a title',
        'Company'   => 'Company name'
    );
    
     $form = FormElements::Details($form, $booking, $descriptions);
	 $form = FormElements::Guests($form,  $booking['guests']);
	 $form = FormElements::Calendar($form, date('Y-m-d', strtotime($booking['bookingtime'])));
	 $form = FormElements::TimesSelect($form, $booking['bookingtime']);
	 $form = FormElements::TableSelect($form, $booking['id']);
	 $form = FormElements::Notes($form, $booking['id']);
	 $form = FormElements::Submit($form);
    

    return $form;
  }

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

  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $data = array(
      'firstname' => $_POST['firstname'],
      'lastname' => $_POST['lastname'],
      'title' => $_POST['title'],
      'email' => $_POST['email'],
      'phone' => $_POST['phone'],
      'modified' => date('Y-m-d H:i:s'),
      'bookingtime' => $_POST['date'] . ' ' . $_POST['time'] . ':00',
      'guests' => $_POST['guests'],
      'requirements' => $_POST['requirements']
    );
    $this->conn->update('restaurant_bookings')->fields($data)->condition('id', $this->getParam('bookingid'), '=')->execute();


	 $req = array();
      if (isset ($_POST['checkrequirements'])) {
          foreach ($_POST['checkrequirements'] as $key => $val) {
              $req[] = $key;
          }
          unset ($_POST['checkrequirements']);
      }
     unset ($data['checkrequirements']);
	 $this->conn->delete('restaurant_bookings_requirements')
		           ->condition('booking_id', $this->getParam('bookingid'))->execute();
	 foreach ($req as $key => $val) {

		 $insert = array(
		     'booking_id' => $this->getParam('bookingid'),
		     'requirement_id' => $val
		 );
		 $this->conn->insert('restaurant_bookings_requirements')->fields($insert)->execute();
	 }
		
		
    if (! empty ($_POST['tables'])) {
		//mydump ($_POST['tables']);
		$this->conn->delete('restaurant_bookings_tables')->condition('booking_id', $this->getParam('bookingid'), '=')->execute();
      foreach ($_POST['tables'] as $key => $val) {
        
        $bookinglength = $this->conn->query("SELECT value FROM restaurant_settings WHERE name = ?", array('bookinglength'))->fetchField();
        $table = array(
          'table_id' => $val,
          'booking_id' => $this->getParam('bookingid'),
          'start' => $data['bookingtime'],
          'end' => date('Y-m-d H:i:s', strtotime("+ " . $bookinglength, strtotime($data['bookingtime'])))
        );
        
        $this->conn->insert('restaurant_bookings_tables')->fields($table)->execute();
      }
     
    }

    $this->addMessage("Booking saved successfully");
    \Drupal\restaurant\Service\Http::Redirect('/admin/restaurant/booking/edit/' . $this->getParam('bookingid'));
  }




}
