<?php

namespace Drupal\restaurant_menus\Form;

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

class Editmenuitem 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_edit';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    //$modified = date('Y-md- H:i:s');

    $takeaway = $this->conn->query("SELECt value FROM restaurant_settings WHERE name = ?", array('menutakeawayprice'))->fetchField();
    $menu = $this->conn->query("SELECT * FROM restaurant_menu_items WHERE id = ?", array($this->getParam('itemid')))->fetch();


    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#description' => $this->t('Please enter a name for your menu item'),
      '#required' => true,
      '#default_value' => $menu->name
    ];

    $form['markup'] = array(
      '#markup' => '<br>'
    );

    $form['price'] = [
      "#type" => 'textfield',
      '#title' => $this->t('Price'),
      '#size' => 5,
      '#description' => $this->t('Please enter a price for your menu item'),
      '#default_value' => number_format($menu->price, 2)
    ];



    if ($takeaway) {

      $form['markup2'] = array(
        '#markup' => '<br>'
      );

      $takeaway_price = (empty ($menu->price_takeaway)) ? '' : number_format($menu->price_takeaway, 2);

      $form['takewaway'] = [
        "#type" => 'textfield',
        '#title' => $this->t('Take Away Price'),
        '#size' => 5,
        '#description' => $this->t('Please enter a takeaway price for your menu item'),
        //'#default_value' => number_format($menu->price_takeaway, 2)
        '#default_value' => $takeaway_price
      ];

    }

    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Description'),
      '#description' => $this->t('Please enter a description for your menu item')
    ];

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

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $takeaway = $this->conn->query("SELECT value FROM restaurant_settings WHERE name = ?", array('menutakeawayprice'))->fetchField();

    $update = array(
      'name' => $form_state->getValue('name'),
      'price' => number_format($form_state->getValue('price'), 2),
      'description' => $form_state->getValue('description'),
      'modified' => date('Y-m-d H:i:s')
    );

    if ($takeaway) {

      if (! empty ($form_state->getValue('price_takeaway'))) {
        $update['price_takeaway'] = number_format($form_state->getValue('price_takeaway'), 2);
      }

    }

    $id = $this->getParam('itemid');
    $this->conn->update('restaurant_menu_items')->fields($update)->condition('id', $id)->execute();
    //$this->addMessage('Updated menu item ' . $form_state->getValue('name'))->http_redirect('/admin/restaurant/menu/' . $this->getParam('menuid'));
    $this->addMessage('Updated menu item <strong>' . $form_state->getValue('name') . '</strong>')->http_redirect('/admin/restaurant/menu/' . $this->getParam('menuid'));

  }




}
