<?php

namespace Drupal\restaurant_menus\Form;

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

class Deletemenuitem 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 'menu_item_delete';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $name = $this->conn->query("SELECT name FROM restaurant_menu_items WHERE id = ?", array($this->getParam('itemid')))->fetchField();

    $form['markup'] = array(
      '#markup' => '<p>Delete menu item <i>' . $name . '</i>? This cannot be undone.</p>'
    );

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

    return $form;
  }

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

    $item = $this->getParam('itemid');
    $menu = $this->getParam('menuid');

    $name = $this->conn->query("SELECT name FROM restaurant_menu_items WHERE id = ?", array($item))->fetchField();

    $this->conn->delete('restaurant_menu_items')->condition('id', $item)->execute();

    $this->addMessage('Deleted item ' . $form_state->getValue('name'))->http_redirect('/admin/restaurant/menu/' . $menu);

  }




}
