package com.nisum.controller;

import com.nisum.dto.EmployeeDto;
import com.nisum.model.impl.EmployeeImp;
import com.nisum.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;

@Controller
public class EmployeeController {

  @Autowired
  EmployeeService employeeService;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String index(Model model) {
    Collection<EmployeeImp> employeeList = employeeService.getAllEmployees();
    model.addAttribute("employees", employeeList);
    return "index";
  }

  @RequestMapping(value = "/new", method = RequestMethod.GET)
  public String newEmployee(Model model){
    model.addAttribute("employee", new EmployeeDto());
    return "new";
  }

  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public String createEmployee(@ModelAttribute("employee") EmployeeDto employeeDto,
                                  BindingResult bindingResult){
    if (bindingResult.hasErrors()){
      return "redirect:/new";
    }
    employeeService.createEmployee(employeeDto);
    return "redirect:/";
  }

//  @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
//  public String editEmployee(@PathVariable("id") int id, Model model){
//
//  }
}