<?php

namespace Drupal\restaurant_menus\Form;

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

class Editmenu 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_edit_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $menu = $this->conn->query("SELECT * FROM restaurant_menus WHERE id = ?", array($this->getParam('menuid')))->fetch();

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

    $path = $this->getInstalledPath();

    $sections = $this->conn->query("SELECT * FROM restaurant_menu_sections WHERE menu_id = ?", array($menu->id))->fetchAll();

    $html = '<br><div id="tabs">';
    $html .= '<ul>';
    foreach ($sections as $key => $val) {
      $html .= '<li><a href="#tabs-' . $val->id . '">' . $val->name . '</a></li>';
    }
    $html .= '</ul>';

    foreach ($sections as $key => $val) {

      $html .= '<div id="tabs-' . $val->id . '">';

      $items = $this->conn->query("SELECT * FROM restaurant_menu_items WHERE section_id = ?", array($val->id))->fetchAll();

      $takeaway = $this->conn->query("SELECT value FROM restaurant_settings WHERE name = ?", array('menutakeawayprice'))->fetchField();

      $takeawaylable = '';
      $takeawayprice = '';
      if ($takeaway) {
        $takeawaylable = '<th>Take Away Price</th>';
      }

      $takeawayprice = ($takeaway) ? '<th>Take Away Price</th>' : '';

      if (empty ($items)) {
        $html .= '<div id="table-div' . $val->id . '">';
        $html .= '<p>No menu items in this section</p>';
        $html .= '</div>';
      } else {
        $html .= '<div id="table-div' . $val->id . '">';
        $html .= <<<HTML
<table>

    <tr>
      <th>Item Name</ht>
      <th>Item Description</th>
      <th>Price</th>
      $takeawayprice
      <th>Actions</th>
    </tr>
HTML;


foreach ($items as $k => $item) {

  $menuid = $menu->id;
  $itemid = $item->id;

  $takewayvalue = ($takeaway) ? '<td>' . number_format($item->price_takeaway, 2) . '</td>' : '';
  $price = number_format($item->price, 2);

  if ($takeaway) {
    $takeawaylable = '<th>Take Away Price</th>';
    $takeawayprice = '<td>' . number_format($item->price_takeaway, 2) . '</td>';
  }

  $html .= <<<HTML

<tr>
  <td>{$item->name}</td>
  <td>{$item->description}</td>
  <td>{$price}</td>
  {$takeawayprice}
  <td>
    <a title="edit menu item" href="/admin/restaurant/editmenuitem/{$menuid}/{$item->id}"><img alt="edit menu item" src="{$path}restaurant/img/edit-4.png"></a>&nbsp;
    <a title="delete menu item" href="/admin/restaurant/deletemenuitem/{$menuid}/{$item->id}"><img alt="delete menu item" src="{$path}restaurant/img/edit-delete-2.png"></a>
  </td>
</tr>

HTML;
}

      $html .= '</table>';
      //$html .=  '<a class="button" href="/admin/restaurant/menu/additem/' . $menuid . '/' . $val['id'] . '">Add Item</a><br><br>';
      $html .= '</div>';
      }
      $html .=  '<a class="button" href="/admin/restaurant/menu/additem/' . $this->getParam('menuid') . '/' . $val->id . '">Add Item</a><br><br>';
      //$html .= '<a href="/admin/restaurant/addsection/' . $menuid . '" class="button">Add Section</a>';
      $html .= '<a href="/admin/restaurant/deletesection/' . $this->getParam('menuid') . '/' . $val->id . '" class="button">Delete Section</a>';
      $html .= '</div><!-- end sections div -->';
    }
    $html .= '<a href="/admin/restaurant/addsection/' . $this->getParam('menuid') . '" class="button">Add Section</a>';
    //$html .= <a href="/admin/restaurant/deletesection/' . $menuid . '/' . $val['id'] . '" class="button">Delete Section</a>';
    $html .= '</div><div style="clear: both;"></div><br><br>';

    $form['mymarkup'] = array(
      '#markup' => $html
    );


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

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $menuid = $this->getParam('menuid');

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

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

    $this->conn->update('restaurant_menus')->fields($update)->condition('id', $menuid, '=')->execute();
    $this->addMessage('Updated menu ' . $name)->http_redirect('/admin/restaurant/menu/' . $this->getParam('menuid'));
  }




}
