<?php

namespace Drupal\restaurant_settings\Form;

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

class Editnotes 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_notes';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    
    $table = $this->conn->query("SELECT * FROM restaurant_requirements WHERE id = ?", array($this->getParam('noteid')))->fetch();
    
    $form['requirement'] = array(
        '#type' => 'textfield',
        '#required' => true,
        '#title' => $this->t('Requirements'),
        '#default_value' => $table->requirement
    );
    
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Edit requirements')
    );

    return $form;
  }

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

    $data = array(
      'requirement' => $this->getParam('requirement'),
    );
    
    $this->conn->update('restaurant_requirements')->fields($data)->condition('id', $this->getParam('noteid'), "=")->execute();

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

}
