<?php

namespace Drupal\restaurant_settings\Form;

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

class Smtp 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 'smtp_settings';
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
  	 $form['heading'] = array(
  		'#type' => 'markup',
  		'#markup' => '<h2>SMTP (email) Settings</h2>'
	);
  	 
  	 $form['smtp_host'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('SMTP Hostname'),
      '#description' => $this->t('Please enter your STMP hostname here'),
      '#required' => false,
      '#placeholder' => 'eg smtp.yourdomain.com',
      '#size' => 30,
      '#default_value' => $this->service->getSetting('SMTPHost')
    );
    
	$form['smtp_secure'] = array(
      '#type' => 'select',
      '#title' => $this->t('Security type'),
      '#options' => [
        'none' => $this->t('none'),
        'ssl' => $this->t('ssl'),
        'tls' => $this->t('tls')
    	],
      '#required' => true,
      '#placeholder' => 'eg smtp.yourdomain.com',
      '#default_value' => "{$this->service->getSetting('SMTPSecure')}"
    );
    
    $form['smtp_username'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('SMTP Username'),
      '#description' => $this->t('Please enter your STMP username'),
      '#required' => false,
      '#placeholder' => 'eg user@yourdomain.com',
      '#size' => 30,
      '#default_value' => $this->service->getSetting('SMTPUsername')
    );
    
	$form['smtp_password'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('SMTP Password'),
      '#description' => $this->t('Please enter your STMP password'),
      '#required' => false,
      '#placeholder' => 'your password',
      '#size' => 30,
      '#default_value' => $this->service->getSetting('SMTPPassword')
   );
   
   $form['smtp_port'] = array(
      '#type' => 'number',
      '#title' => $this->t('SMTP Port'),//25 or 587
      '#description' => $this->t('Please enter your STMP port number. Usually port 25 or 587'),
      '#required' => true,
      '#placeholder' => 'your SMTP server port. Defaults to port 25',
      '#size' => 10,
      '#default_value' => $this->service->getSetting('SMTPPort')
   );
   
   $form['submit'] = array(
   	'#type' => 'submit',
   	'#value' => 'Save'
	);
  	 
    return $form;
  }


  public function submitForm(array &$form, FormStateInterface $form_state)
  {
	
	
	if (isset ($_POST['op'])) {
		
		try {
			$this->service->setSetting('SMTPHost', $_POST['smtp_host']);
			$this->service->setSetting('SMTPUsername', $_POST['smtp_username']);
			$this->service->setSetting('SMTPPassword', $_POST['smtp_password']);
			$this->service->setSetting('SMTPSecure', $_POST['smtp_secure']);
			$this->service->setSetting('SMTPPort', $_POST['smtp_port']);
			$this->addMessage("SMTP settings saved successfully");
		} catch (\Exception $e) {
			
			$this->addMessage("There was an error saving your settings. Please contact your system adminstrator", "error");
			
		}
		
		
	}
				
		
  }

}
