package kr.co.jinwoosi.clfd.framework.pgm.web;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import kr.co.jinwoosi.clfd.framework.base.web.BaseController;
import kr.co.jinwoosi.clfd.framework.menu.service.MenuService;
import kr.co.jinwoosi.clfd.framework.pgm.model.PgmSearch;
import kr.co.jinwoosi.clfd.framework.pgm.service.PgmService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import kr.co.jinwoosi.clfd.framework.pgm.model.Pgm;

@Controller
public class PgmController extends BaseController {
	
	@Resource (name="menuService")
	private MenuService menuService;
	
	@Resource (name="pgmService")
	private PgmService pgmService;
	
	@RequestMapping (value="/system/pgm/list.mng", method=RequestMethod.GET)
	public String list(Model model) {
		return "system/pgm/list";
	}
	
	@RequestMapping (value="/system/pgm/list.json", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request, PgmSearch pgmSearch) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {	
			String sort = request.getParameter("columns[" + request.getParameter("order[0][column]") + "][name]");
			String sortOder = request.getParameter("order[0][dir]");
			pgmSearch.setSort(sort);
			pgmSearch.setSortOrd(sortOder);
			pgmSearch.setPagingYn(true);
			
			int totalCount = pgmService.count(pgmSearch);
			result.put("recordsTotal", totalCount);
			result.put("recordsFiltered", totalCount);
			
			result.put("data", pgmService.selectList(pgmSearch));
			
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
	
	@RequestMapping (value="/system/pgm/view.json", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> detail(HttpServletRequest request, PgmSearch pgmSearch) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {	
			result.put("data", pgmService.select(pgmSearch));
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
	
	@RequestMapping (value="/system/pgm/insert.json", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insert(HttpServletRequest request, Pgm pgm) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {	
			pgm.setRegId(getUserId());
			pgmService.insert(pgm);
			
			//pgmService.initJson();
			//menuService.initJson();
			menuService.initRoleMenuList();;
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
	
	@RequestMapping (value="/system/pgm/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request, Pgm pgm) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {	
			pgm.setUpdId(getUserId());
			pgmService.update(pgm);
			
			//pgmService.initJson();
			//menuService.initJson();
			menuService.initRoleMenuList();
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}

	@RequestMapping (value="/system/pgm/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request, Pgm pgm) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {	
			pgm.setDelId(getUserId());
			pgmService.delete(pgm);
			
			//pgmService.initJson();
			//menuService.initJson();
			menuService.initRoleMenuList();
			result.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
}
