package net.sevecek.zakladniwebapp;

import javax.validation.*;
import org.springframework.stereotype.*;
import org.springframework.validation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.*;

@Controller
public class HlavniController {

    @RequestMapping(value = "/ukazka.html", method = RequestMethod.GET)
    public ModelAndView zobrazUkazku() {
        ModelAndView modelAndView = new ModelAndView(
                "/WEB-INF/view/ukazka.jsp");
        modelAndView.addObject("ukazkaForm", new UkazkaForm());
        return modelAndView;
    }

    @RequestMapping(value = "/ukazka.html", method = RequestMethod.POST)
    public ModelAndView zpracujUkazku(
                @Valid @ModelAttribute("ukazkaForm") UkazkaForm ukazkaForm,
                Errors validacniChyby) {
        if (validacniChyby.hasErrors()) {
            ModelAndView modelAndView;
            modelAndView = new ModelAndView("/WEB-INF/view/ukazka.jsp");
//            modelAndView.addObject("ukazkaForm", ukazkaForm);
            return modelAndView;
        }


        ModelAndView modelAndView;
        modelAndView = new ModelAndView("/WEB-INF/view/vysledek.jsp");
        modelAndView.addObject("celeJmeno",
                ukazkaForm.getKrestniJmeno() + " "
                        + ukazkaForm.getPrijmeni());
        modelAndView.addObject("vek", ukazkaForm.getVek());
        return modelAndView;
    }

}
