<?php

namespace Drupal\restaurant_settings\Form;

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

class Deletetable 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 'delete_table';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $bookings = $this->conn->query("SELECT * FROM restaurant_bookings_tables WHERE table_id = ?", array($this->getParam('tableid')))->fetchAll();

    if (! empty ($bookings)) {
      //TODO display what bookings are associated with table and attempt to assiged those bookings to another table
      \Drupal::messenger()->addError($this->t("Cannot delete this table as there are bookings assigned to it"));
    } else {

      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Confirm Delete')
      );

    }

    $form['markup'] = array(
      "#markup" => $message
    );

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $this->conn->delete('restaurant_tables')->condition('id', $this->getParam('tableid'))->execute();
    $this->addMessage("Deleted table")->http_redirect('/admin/restaurant/settings/tables');
  }

}
