<?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 Symfony\Component\HttpFoundation\Session\Session;
use Drupal\restaurant\Form\MultipartForm;
use Drupal\restaurant\View\View;

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 'show_bookings';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
  	
	 $form_state->setMethod('GET');
    $html = '<br><a href="/admin/restaurant/bookings/add">+ Add Booking</a><p></p>';
    $form['link'] = array(
      '#markup' => $html
    );
    
    $date = (isset ($_GET['calendar'])) ? $_GET['calendar'] : date('Y-m-d');

    $form['calendar'] = array(
      '#type' => 'date',
      '#default_value' => $date
    );
 	 
    $path = get_restaurant_path(false) . DIRECTORY_SEPARATOR;

    $times = $this->conn->query("SELECT DISTINCT bookingtime FROM restaurant_bookings WHERE '$date' = DATE(bookingtime) ORDER BY bookingtime ASC")->fetchCol();
    
    
    $html = '<br><table id="bookings-table">';
    $bookings = array();
    foreach ($times as $key => $val) {
      $time = date('H:i', strtotime($val));

      $html .= '<tr>';
        $html .= '<th>';
        $html .= date('g:i a', strtotime($time));
        $html .= '</th>';
        $bookings = $this->conn->query("SELECT * FROM restaurant_bookings WHERE '$date' = DATE(bookingtime) AND '$time' = TIME_FORMAT(bookingtime, '%H:%i')");
        $html .= '<td>';
        foreach ($bookings as $v) {
          if (! empty ($v->lastname)) {
            $name = $v->lastname;
          } else if (! empty ($v->firstname)) {
            $name = $v->firstname;
          } else {
            $name = $v->email;
          }
          $html .= '<div class="booking-link">';

            $html .= '<div class="booking-link-left">';
            $html .= '<a href="/admin/restaurant/booking/edit/' . $v->id . '">' . $name . '</a>';
            $html .= '</div>';

            $html .= '<div class="booking-link-right">';
            $html .= '<a href="/admin/restaurant/booking/edit/' . $v->id . '">' . '<img src="' . $path . 'img' . DIRECTORY_SEPARATOR . 'edit-4.png">' . '</a>&nbsp;';
            $html .= '<a onclick="return confirm(\'Delete this booking?\')" class="deletebooking" href="/admin/restaurant/booking/delete/' . $v->id . '">' . '<img src="' . $path . 'img' . DIRECTORY_SEPARATOR . 'edit-delete-2.png">' . '</a>';
            $html .= '</div>';

          $html .= '</div><div style="clear: both;"></div>';
        }
        $html .= '</td>';
      $html .= '</tr>';
    }
    $html .= '</table>';
  
    $form['table'] = array(
      '#markup' => $this->t($html)
    );

    return $form;

  }

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




}
