<?php

namespace Drupal\restaurant_menus\Form;

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

class Deletesection 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_section_delete';
  }

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

    $form['markup'] = array(
      '#markup' => '<p>Delete section <i>' . $name . '</i>? This will delete all menu items in section and cannot be undone.</p>'
    );

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

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $sectionid = $this->getParam('sectionid');
    $name = $this->conn->query("SELECT name FROM restaurant_menu_sections WHERE id = ?", array($sectionid))->fetchField();
    $this->conn->delete('restaurant_menu_items')->condition('section_id', $sectionid)->execute();
    $this->conn->delete('restaurant_menu_sections')->condition('id', $sectionid)->execute();
    $this->addMessage('Deleted section ' . $name)->http_redirect('/admin/restaurant/menu/' . $this->getParam('menuid'));
  }




}
