<?php

namespace App\Controller;

use App\Controller\AppController;
use Cake\Mailer\Email;

/**
 * Examinations Controller
 *
 * @property \App\Model\Table\ExaminationsTable $Examinations
 */
class ExaminationsController extends AppController {

    public function sendresult() {
        $email = new Email('default');
        $email->from(['asd.trinkl@schulen.bayern.de' => 'ASD Trinkl'])
            ->to('asd.kraupner@schulen.bayern.de')
            ->subject('Test')
            ->send('Hallo Martina!');
    }

    public function listImports() {
        $this->loadModel('Imports');
        //$import = $this->Imports->newEntity();
        $list = $this->Examinations->find('all', [
            'contain' => ['Schooltypes','Imports']
        ]);
        //$this->set('import',$import);
        $this->set('list', $list);
        //foreach($list as $item) {
        //    debug($item);
        //}
        //die;
    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index() {
        $this->paginate = [
            'contain' => ['Schooltypes']
        ];
        $examinations = $this->paginate($this->Examinations);
        
        //debug($examinations);die;

        $this->set(compact('examinations'));
        $this->set('_serialize', ['examinations']);
    }

    /**
     * View method
     *
     * @param string|null $id Examination id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null) {
        $examination = $this->Examinations->get($id, [
            'contain' => ['Schooltypes', 'Imports']
        ]);

        $this->set('examination', $examination);
        $this->set('_serialize', ['examination']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add() {
        $examination = $this->Examinations->newEntity();
        if ($this->request->is('post')) {
            $examination = $this->Examinations->patchEntity($examination, $this->request->data);
            if ($this->Examinations->save($examination)) {
                $this->Flash->success(__('The examination has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The examination could not be saved. Please, try again.'));
        }
        $schooltypes = $this->Examinations->Schooltypes->find('list', ['limit' => 200]);
        $this->set(compact('examination', 'schooltypes'));
        $this->set('_serialize', ['examination']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Examination id.
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null) {
        $examination = $this->Examinations->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $examination = $this->Examinations->patchEntity($examination, $this->request->data);
            if ($this->Examinations->save($examination)) {
                $this->Flash->success(__('The examination has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The examination could not be saved. Please, try again.'));
        }
        $schooltypes = $this->Examinations->Schooltypes->find('list', ['limit' => 200]);
        $this->set(compact('examination', 'schooltypes'));
        $this->set('_serialize', ['examination']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Examination id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null) {
        $this->request->allowMethod(['post', 'delete']);
        $examination = $this->Examinations->get($id);
        if ($this->Examinations->delete($examination)) {
            $this->Flash->success(__('The examination has been deleted.'));
        } else {
            $this->Flash->error(__('The examination could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }

}
