<?php

namespace Drupal\restaurant_settings\Form;

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

class Edittable 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_table';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $table = $this->conn->query("SELECT * FROM restaurant_tables WHERE id = ?", array($this->getParam('tableid')))->fetch();
    $form['name'] = array(
      '#type' => 'textfield',
      '#required' => true,
      '#title' => $this->t('Table name'),
      '#size' => 20,
      '#placeholder' => 'eg Table 1',
      '#default_value' => $table->name
    );

    $form['guests'] = array(
      '#type' => 'number',
      '#required' => true,
      '#title' => $this->t('Guests'),
      '#default_value' => $table->number
    );

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

    return $form;
  }

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

    $data = array(
      'name' => $this->getParam('name'),
      'number' => $this->getParam('guests')
    );
    $this->conn->update('restaurant_tables')->fields($data)->condition('id', $this->getParam('tableid'), "=")->execute();

    $this->addMessage("Table saved")->http_redirect('/admin/restaurant/settings/tables');
  }

}
