package tw.edu.ncu.cc.manage.controller.godmode;

import java.util.List;

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 tw.edu.ncu.cc.manage.domain.Manager;
import tw.edu.ncu.cc.manage.service.IManagerService;

/**
 * 系統管理員管理
 * @author yyc1217
 *
 */
@Controller
@RequestMapping("/godmode/manager")
public class ManagerController {
	
	@Autowired
	private IManagerService managerService;
	
	/**
	 * 首頁
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) {
		List<Manager> managerList = this.managerService.findAll();
		model.addAttribute("managerList", managerList);
		return "manager/index";
	}
	
	/**
	 * 新增頁面
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute("manager", new Manager());
		return "manager/create";
	}
	
	/**
	 * 在新增頁面按下「新增」
	 * @param manager
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String createSubmit(Model model, @ModelAttribute Manager manager, BindingResult result) {
		
		//TODO validator
		if (result.hasErrors()) {
			model.addAttribute("manager", manager);
			return "manager/create";
		}
		
		this.managerService.create(manager);
		return "redirect:../manager";
	}
	
	// 刪除
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String delete(@PathVariable("id") String id) {
		
		Manager manager = this.managerService.find(id).get();
		this.managerService.delete(manager);
		
		return "redirect:../";
	}
	
}

@@ -530,16 +530,70 @@
 Method;%0D
+%0Aimport org.springframework.web.servlet.ModelAndView;%0D
 %0A%0D%0Aimpor
@@ -988,32 +988,27 @@
 lic 
-String index(Model model
+ModelAndView index(
 ) %7B%0D
@@ -1072,34 +1072,39 @@
 l();%0D%0A%09%09
-model.addAttribute
+return new ModelAndView
 (%22manage
@@ -1108,15 +1108,18 @@
 ager
-List
+/index
 %22, 
+%22
 mana
@@ -1129,36 +1129,23 @@
 List
-);%0D%0A%09%09return %22manager/index%22
+%22, managerList)
 ;%0D%0A%09
@@ -1262,112 +1262,102 @@
 lic 
-String create(Model model) %7B%0D%0A%09%09model.addAttribute(%22manager%22, new Manager());%0D%0A%09%09return %22manager/create%22
+ModelAndView create() %7B%0D%0A%09%09return new ModelAndView(%22manager/create%22, %22manager%22, new Manager())
 ;%0D%0A%09

use ModelAndView to handle single model attribute