package com.eactive.eai.rms.bap.statistics.eaibzwkdstcd.listup;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.bap.tansaction.transactionStatus.BapTransactionStatusService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.IbatisComboService;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.google.gson.Gson;

@Controller
public class BjobBzwkDstcdListupController extends BaseController  {
	
    @Autowired
    @Qualifier("bjobBzwkDstcdListupService")
    private BjobBzwkDstcdListupService service ;
    
	@Autowired
	@Qualifier("bapTransactionStatusService")
	private BapTransactionStatusService statusService;
	
	@Autowired
	private ComboService comboService;
	
    @Autowired
    private IbatisComboService ibatisComboService; 
    
	@RequestMapping(value= "/bap/statistics/eaibzwkdstcd/bjobBzwkDstcdListupMan.view")
	private String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false ) String cmd ) throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())){
			cmd = "";
		}else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0]+cmd;
	}      

	@RequestMapping(value= "/bap/statistics/eaibzwkdstcd/bjobBzwkDstcdListupMan.json",params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap) throws Exception {
		
		HashMap<String, Object> map = service.selectList(paramMap);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("rows", map.get("list"));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bap/statistics/eaibzwkdstcd/bjobBzwkDstcdListupMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initCombo( HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		List<Map<String, Object>> bjobBzwkDstcd  = ibatisComboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");		
		List<Map<String, Object>> osidInstiDstcd  = statusService.selectOsidInstiDstcd("");		
        
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("bjobBzwkDstcd"	, bjobBzwkDstcd);
		resultMap.put("osidInstiDstcd"	, osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    } 
	
	@RequestMapping(value= "/bap/statistics/eaibzwkdstcd/bjobBzwkDstcdListupMan.json",params = "cmd=LIST_ORGN_COMBO")
    public ModelAndView initOrgnCombo( HttpServletRequest request,
            HttpServletResponse response, String bjobBzwkDstcd) throws Exception {
    	
		List<Map<String, Object>> osidInstiDstcd  = statusService.selectOsidInstiDstcd(bjobBzwkDstcd);		
        
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("osidInstiDstcd" , osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    } 
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value= "/bap/statistics/eaibzwkdstcd/bjobBzwkDstcdListupMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
    public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, String gridData, String header, String headerTitle, String fileName, String merge) throws Exception {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
    	Gson gson = new Gson();
		HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);
		
		String[] headers = gson.fromJson(header, String[].class);
		String[] headerTitles = gson.fromJson(headerTitle, String[].class);
		
		HashMap<String, Object>[] lmerge = null; 
		if (merge !=null){
			lmerge = gson.fromJson(merge, HashMap[].class);
		}
    	
    	resultMap.put("headers", headers);
    	resultMap.put("headerTitles", headerTitles);
    	resultMap.put("datas", list);
    	resultMap.put("fileName", fileName);
    	resultMap.put("merge", lmerge);
		ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
        
        return modelAndView;
    }	
}
