package egovframework.com.mtdata.cmmnty.web;

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

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

import org.apache.log4j.Logger;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import egovframework.com.mtdata.cmmn.vo.BasicVO;
import egovframework.com.mtdata.cmmnty.service.CmmntyService;
import egovframework.com.mtdata.cmmnty.vo.CmmntyVO;

@RestController
@RequestMapping("/api")
public class CmmntyController {
	
	/** Service */
	@Resource(name = "CmmntyService")
	private CmmntyService cmmntyService;
	
	/** EgovPropertyService */
	@Resource(name = "propertiesService")
	protected EgovPropertyService propertiesService;
	
	final static Logger log = Logger.getLogger(CmmntyController.class);
	
	/**
	 * 글 목록을 조회한다. (pageing)
	 * @param searchVO - 조회할 정보가 담긴 BasicVO
	 * @param model
	 * @return "egovSampleList"
	 * @exception Exception
	 */
	@RequestMapping(value="/cmmntyList.do")
	public @ResponseBody ModelMap getCmmntyList(@ModelAttribute("searchVO") BasicVO searchVO) throws Exception {
		
		/** EgovPropertyService */
		searchVO.setPageUnit(propertiesService.getInt("pageUnit"));
		searchVO.setPageSize(propertiesService.getInt("pageSize"));
		
		
		/** pageing setting */
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
		paginationInfo.setPageSize(searchVO.getPageSize());

		searchVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
		searchVO.setLastIndex(paginationInfo.getLastRecordIndex());
		searchVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());
		
		ModelMap model = new ModelMap();
		
		// db에 있는 게시글 list를 가져와서 cmmntyList에 저장
		List<CmmntyVO> cmmntyList = cmmntyService.selectCmmntyList(searchVO);
		// 검색된 건수가 몇건인지 확인
		int totCnt = cmmntyService.selectCmmntyListTotCnt(searchVO);
		
		model.addAttribute("cmmntyList", cmmntyList);
		model.addAttribute("totCnt", totCnt); 
		
		return model;
	}
	
	/** 게시글 작성*/
	@RequestMapping(value="/insertCmmnty.do")
	public String insetCmmnty() {
		return "";
	}
}

