<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace AdminModule;
use \Nette\Application\UI\Form;
use \Nette\Utils\Image;

 
class DkkPresenter extends BasePresenter {
    
    private $dkkModel;
    private $strankyModel;
    
    private $urlList = "dkk/list";
   
    
    //public $rokyk;
   
   /** @persistent */
   // public $rok;
 
    public function __construct(\DkkModel $dkkModel,\StrankyModel $strankyModel) {
    $this->dkkModel = $dkkModel;
    $this->strankyModel = $strankyModel;
    }
    
  
    
    public function renderList() {
      $this->template->text = $this->strankyModel->getStrankaByUrl($this->urlList);
       
    }
    
    public function renderFeny() {
      
       $this->template->psi = $this->dkkModel->getFeny(); 
    }
    
    public function renderPsi() {
      
       $this->template->psi = $this->dkkModel->getPsi(); 
    }
    
    public function renderAdd() {
      
       // if ($this->rok == null) { $this->rok=date('Y'); } // aktualni rok
     
        
    }
    
    public function renderEdit() {
      
       // if ($this->rok == null) { $this->rok=date('Y'); } // aktualni rok
     
       
    }
    
    public function renderEditText() {
        
    }
 


protected function createComponentAddPesForm() {
   
    $form = new Form();
    $form->addText('jmeno', 'Jméno+info:*');
    $sex = array('pes' => 'pes','fena' => 'fena');
    $form->addRadioList('pohl', 'Pohlaví*:', $sex)
    ->getSeparatorPrototype()->setName(NULL);
    $form->addText('otec', 'Otec:*');
    $form->addText('matka','Matka*:');
    $form->addText('otec_o', 'Otec otce:*');
    $form->addText('matka_o','Matka otce*:');
    $form->addText('otec_m', 'Otec matky:*');
    $form->addText('matka_m','Matka matky*:');
    $form->addSubmit("send", "Uložit");
    $form->onSuccess[] = $this->addPesFormSucceeded;
    return $form;
}

public function addPesFormSucceeded(Form $form) {
    $values = $form->getValues();
    
     if ($values->pohl == 'fena') {
        $values->fena = 1;
    } else { $values->fena = 0;}
    unset($values['pohl']);
    $this->dkkModel->addPes($values);
     $this->flashMessage('Pes byl přidán', 'info');
     
     if ($values->fena == 1) {
     $this->redirect('Dkk:feny'); } 
     else { $this->redirect('Dkk:psi');
     }
}

protected function createComponentEditTextForm() {
    $form = new Form();
    $str = $this->strankyModel->getStrankaByUrl($this->urlList);
    $form->addTextArea('text', 'Text:')->setDefaultValue($str->text);
    $form->addHidden('id')->setDefaultValue($str->id);
    $form->addSubmit("send", "Uložit");
    $form->onSuccess[] = $this->editTextFormSucceeded;
    return $form;
}

public function editTextFormSucceeded(Form $form) {
    $values = $form->getValues();
    $this->strankyModel->updateStranka($values->id, $values);
    $this->flashMessage('Text byl editován', 'info');
    $this->redirect('Dkk:list');
}

protected function createComponentEditPesForm() {
    $pes = $this->dkkModel->getPes($this->getParam('id'));
    $form = new Form();
    $form->addText('jmeno', 'Jméno+info:*')->setDefaultValue($pes->jmeno)->setRequired('Zadejte jméno!');
    $sex = array('pes' => 'pes','fena' => 'fena');
    $form->addRadioList('pohl', 'Pohlaví*:', $sex)
    ->getSeparatorPrototype()->setName(NULL);
    $form->addText('otec', 'Otec:*')->setDefaultValue($pes->otec)->setRequired('Zadejte jméno otce!');
    $form->addText('matka','Matka*:')->setDefaultValue($pes->matka)->setRequired('Zadejte jméno matky!');
    $form->addText('otec_o', 'Otec otce:*')->setDefaultValue($pes->otec_o)->setRequired('Zadejte jméno!');
    $form->addText('matka_o','Matka otce*:')->setDefaultValue($pes->matka_o)->setRequired('Zadejte jméno!');
    $form->addText('otec_m', 'Otec matky:*')->setDefaultValue($pes->otec_m)->setRequired('Zadejte jméno!');
    $form->addText('matka_m','Matka matky*:')->setDefaultValue($pes->matka_m)->setRequired('Zadejte jméno!');
    $form->setDefaults(array('pohl' => $pes->fena==0?'pes':'fena'));
    
    $form->addHidden("id")
        ->setDefaultValue($this->getParam('id'));
    $form->addSubmit("send", "Uložit");
    $form->onSuccess[] = $this->editPesFormSucceeded;
    return $form;
}

public function editPesFormSucceeded(Form $form) {
   $values = $form->getValues();
    if ($values->pohl == 'fena') {
        $values->fena = 1;
    } else { $values->fena = 0;}
  
   unset($values['pohl']);
    $this->dkkModel->updatePes($values->id,$values);
    
    $this->flashMessage('Pes byl editován', 'info');
   
     
     if ($values->fena == 1) {
     $this->redirect('Dkk:feny'); } 
     else { $this->redirect('Dkk:psi');
     }
}



public function handleDeletePes($id) {
    $pes = $this->dkkModel->getPes($id);
  
    $this->dkkModel->deletePes($id);
    $this->flashMessage('Pes byl smazán', 'info');
     if ($pes->fena == 1) {
     $this->redirect('Dkk:feny'); } 
     else { $this->redirect('Dkk:psi');
     }
    
}

    
}
