<?php

namespace Drupal\restaurant_menus\Controller;
use Drupal\restaurant\Controller\AppController;

#[\AllowDynamicProperties]
class RestaurantMenusController extends AppController
{

  public function __construct()
  {
    parent::__construct();
    $this->setPath('restaurant_menus' . DIRECTORY_SEPARATOR . 'HtmlScripts' . DIRECTORY_SEPARATOR);
  }

  public function menus()
  {
    $this->view->menus = $this->conn->query("SELECT * FROM restaurant_menus")->fetchAll();
    return $this->render('menus.phtml');
  }

  public function menu()
  {

  }

  public function toggle()
  {
    if ($this->hasParam('menuid')) {
      $active = ($this->conn->query("SELECT active FROM restaurant_menus WHERE id = ?", array($this->getParam('menuid'))))->fetchfield() ? 0 : 1;
      $options = array('modified' => date('Y-m-d H:i:s'), 'active' => $active);
      $this->conn->update('restaurant_menus')->fields($options)->condition('id', $this->getParam('menuid'))->execute();
      $this->service->http_redirect('/admin/restaurant/menus');

    }
  }

  public function delete()
  {
    if ($this->hasParam('menuid')) {
      $this->conn->delete('restaurant_menus')->condition('id', $this->getParam('menuid'))->execute();
      $this->service->http_redirect('/admin/restaurant/menus');
    }
  }
}
