package com.nisum.controller;

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.validation.BindingResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import com.nisum.dto.EmployeeDto;



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) {
    employeeService.createEmployee(employeeDto);
    return "redirect:/";
  }

  @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
  public String editEmployee(@PathVariable("id") int id, Model model) {
    employeeService.deleteEmployee(id);
    return "redirect:/";
  }
}
