package nlib.sample.web;

import java.util.Map;

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import nlib.cmm.NlibCommonController;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.restful.service.DataApiVO;
import nlib.sample.service.SampleDataApiService;
import nlib.user.service.NlibLoginVO;
import nlib.util.StringUtil;

/**
 * <pre>
 * @Class Name : SampleDataApiController.java
 * 
 * @Description : RESTful API 호출 샘플
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 6. 22.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 6. 22.
 * @version 1.0
 *
 */
@Controller
public class SampleDataApiController extends NlibCommonController {
	
	/** SampleDataApiService */
	@Resource(name = "sampleDataApiService")
	private SampleDataApiService sampleDataApiService;
	

	/* DataApi 접속 서버 정보 */ 
	@Value("#{properties['dataapi.server.ip']}") 
	private String dataapiServerIp;
	
	@Value("#{properties['dataapi.server.port']}") 
	private String dataapiServerPort;
	
	@Value("#{properties['dataapi.server.protocol']}") 
	private String dataapiServerProtocol;
	
	
    @RequestMapping("/sample/getSampleInfoForm.do")
    public String getSampleInfoForm(ModelMap model) throws Exception {

		model.addAttribute("serverProtocol", dataapiServerIp);
		model.addAttribute("serverIp", dataapiServerPort);
		model.addAttribute("serverPort", dataapiServerProtocol);
		return "nlib/sample/getSampleInfoForm";
    }

    @RequestMapping("/sample/getSampleInfo.do")
    public String getSampleInfo(@RequestParam Map<String, Object> commandMap, ModelMap model) throws Exception {
		
    	String page = (String) commandMap.get("page");
    	String rows = (String) commandMap.get("rows");
    	
    	DataApiReqVO reqVO = new DataApiReqVO();
    	reqVO.setPageIndex(page);
    	reqVO.setPageSize(rows);
    	
    	DataApiResVO resVO = sampleDataApiService.getSampleInfo(reqVO);
    	model.addAttribute("result", resVO);
    	
		return "nlib/sample/getSampleInfo";
    }

    @RequestMapping("/sample/getItemInfo.do")
    public String getItemInfo(HttpServletRequest request, @RequestParam Map<String, String> commandMap, ModelMap model) throws Exception {
		
    	String message = null;
    	
    	DataApiReqVO reqVO = new DataApiReqVO("/uac/service/nlib/col/getItemInfo", HttpMethod.GET);
    	
    	String mngOrgCd = commandMap.get("mngOrgCd");
    	String masterId = commandMap.get("masterId");
    	String typeDivCd = commandMap.get("typeDivCd");
    	
    	if(StringUtil.isEmpty(mngOrgCd) || StringUtil.isEmpty(masterId) || StringUtil.isEmpty(typeDivCd)) {
    		message = "필수정보값이 입력되지 않았습니다.";
    		model.addAttribute("message", message);
    		return "nlib/sample/getItemInfo";
    	}
    	
    	// 변수 설정 
    	reqVO.putInfoItem("mngOrgCd", mngOrgCd);
    	reqVO.putInfoItem("masterId", masterId);
    	reqVO.putInfoItem("typeDivCd", typeDivCd);
    	
    	// 사용자ID 설정 
    	NlibLoginVO nlibLoginVO = getNlibLoginVO(request);
    	if(nlibLoginVO != null && !StringUtil.isEmpty(nlibLoginVO.getMbInfoId())) {
    		reqVO.setMbInfoId(nlibLoginVO.getMbInfoId());
    	}
    	
    	// API 호출 
    	DataApiResVO resVO = sampleDataApiService.getItemInfo(reqVO);

    	// 결과값 전달 
    	model.addAttribute("resultCode", resVO.getResultCode());
    	model.addAttribute("resultMessage", resVO.getResultMessage());
    	model.addAttribute("info", resVO.getInfo());

    	// 참고 정보 : API 접속 서버 정보 
		model.addAttribute("serverProtocol", dataapiServerIp);
		model.addAttribute("serverIp", dataapiServerPort);
		model.addAttribute("serverPort", dataapiServerProtocol);
		
		return "nlib/sample/getItemInfo";
    }
    
    @RequestMapping("/sample/listRentItem.do")
    public String listRentItem(HttpServletRequest request, @RequestParam Map<String, String> commandMap, ModelMap model) throws Exception {
		
    	String message = null;
    	
    	DataApiReqVO reqVO = new DataApiReqVO("/uac/service/nlib/rent/listRentItem", HttpMethod.GET);
    	
    	// 변수 설정 
    	reqVO.putInfoItem("rows", getParamStr(commandMap, "rows", "10"));
    	reqVO.putInfoItem("page", getParamStr(commandMap, "rows", "1"));
    	reqVO.putInfoItem("mngOrgCd", getParamStr(commandMap, "mngOrgCd", "KCCF"));
    	reqVO.setMbInfoId("M1000000001");   //putInfoItem("mbInfoId", getParamStr(commandMap, "mbInfoId", "BDCCAF34CF8AA365C660ADB121CE6741"));
    	
    	// 사용자ID 설정 
//    	NlibLoginVO nlibLoginVO = getNlibLoginVO(request);
//    	if(nlibLoginVO != null && !StringUtil.isEmpty(nlibLoginVO.getMbInfoId())) {
//    		reqVO.setMbInfoId(nlibLoginVO.getMbInfoId());
//    	}
    	
    	// API 호출 
    	DataApiResVO resVO = sampleDataApiService.getItemInfo(reqVO);

    	// 입력 매개변수 전달 
    	model.addAttribute("rows", resVO.getRecordTotCount());
    	model.addAttribute("page", resVO.getPageIndex());
    	model.addAttribute("mngOrgCd", resVO.getInfoItem("mngOrgCd"));
    	model.addAttribute("mbInfoId", resVO.getInfoItem("mbInfoId"));
    	
    	// 결과값 전달 
    	model.addAttribute("resultCode", resVO.getResultCode());
    	model.addAttribute("resultMessage", resVO.getResultMessage());
    	model.addAttribute("info", resVO.getInfo());
    	model.addAttribute("list", resVO.getList(DataApiVO.XML_EL_NAME_RECORD));

    	// 참고 정보 : API 접속 서버 정보 
		model.addAttribute("serverProtocol", dataapiServerIp);
		model.addAttribute("serverIp", dataapiServerPort);
		model.addAttribute("serverPort", dataapiServerProtocol);
		
		return "nlib/sample/listRentItem";
    }

    
}
