<?php

/*

namespace Drupal\restaurant_settings\Form;

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

class General extends AppForm {

  public function getFormId() {
    return 'site_info_settings';
  }

  private function getValue($field)
  {
    return $this->db->fetchOne("SELECT {$field} FROM restaurant_sites");
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {

    $data = $this->db->fetchRow("SELECT * FROM restaurant_sites");

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

    $form['phone'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Phone number'),
      '#description' => $this->t('Please your restaurant phone number'),
      '#required' => false,
      '#default_value' => $this->getValue('phone')
    ];

    $form['address'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Address'),
      '#description' => $this->t('Please enter your restaurant address'),
      '#required' => false,
      '#default_value' => $this->getValue('address')
    ];

    $form['suburb'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Suburb'),
      '#description' => $this->t('Please enter your restaurant suburb'),
      '#required' => false,
      '#default_value' => $this->getValue('suburb')
    ];

    $form['state'] = [
      '#type' => 'textfield',
      '#title' => $this->t('State'),
      '#description' => $this->t('Please enter your restaurant state'),
      '#required' => false,
      '#default_value' => $this->getValue('state')
    ];

    $form['postcode'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Postcode'),
      '#description' => $this->t('Please enter your restaurant postcode'),
      '#required' => false,
      '#default_value' => $this->getValue('postcode')
    ];

    $countries = $this->db->fetchAll("SELECT id, name FROM restaurant_countries");
    $options = array();
    foreach ($countries as $key => $val) {
      $options[$val['id']] = $this->t($val['name']);
    }

    $form['country_id'] = [
      '#type' => 'select',
      '#title' => $this->t('Your country'),
      '#options' => [
        $options
      ],
    ];

    if (empty ($data['country_id'])) {
      $form['country_id']['#default_value'] = '14';
    } else {
      $form['country_id']['#default_value'] = $data['country_id'];
    }

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

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state)
  {
    $name = $this->db->fetchOne("SELECT name FROM restaurant_sites WHERE name = ?", array($form_state->getValue('name')));

    $date = date('Y-m-d H:i:s');

    $insert = array(
      'name' => $form_state->getValue('name'),
      'address' => $form_state->getValue('address'),
      'suburb' => $form_state->getValue('suburb'),
      'postcode' => $form_state->getValue('postcode'),
      'country_id' => $form_state->getValue('country_id'),
      'phone' => $form_state->getValue('phone'),
      'state' => $form_state->getValue('state'),
      'created' => $date,
      'modified' => $date
    );

    $this->db->delete('restaurant_sites');
    $this->db->insert('restaurant_sites', $insert);
    $this->addMessage($this->t("Saved restaurant site settings"))->http_redirect('/admin/restaurant/settings');
  }

  */

}
