
package nlib.bbs.web;

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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import nlib.bbs.service.ArticleVO;
import nlib.bbs.service.BoardService;
import nlib.cmm.NlibCommonController;
import nlib.cmm.service.NlibProperty;
import nlib.cmm.service.PagingVO;

@Controller
public class AncmntController extends NlibCommonController
{
	private static final Logger log = LoggerFactory.getLogger(AncmntController.class);
	
	static final int DEFUALT_PAGE_SIZE = NlibProperty.getInt("list.paging.page.size", 10);
	
	@Resource(name = "ancmntService")
	//private AncmntServiceImpl ancmntService;
	private BoardService ancmntService;
	

	/**
	 * 목록 화면을 표시한다. 
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/bbs/listAncmnts.do")
	public String listAncmnts(
			HttpServletRequest req, 
			@RequestParam Map<String, String> paramMap,
			ArticleVO searchArticleVO,
			ModelMap model
			) throws Exception {

		model.addAttribute("searchArticle", searchArticleVO);
    	
    	return "nlib/bbs/listAncmnts";
	}

	/**
	 * 목록 조회한다.
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/bbs/listAncmntsAjax.do")
	public ResponseEntity<String> listAncmntsAjax(
			HttpServletRequest request, 
			Authentication authentication,
			@RequestBody ArticleVO searchArticleVO) throws Exception {

		if(searchArticleVO.getPageIndex() < 1) searchArticleVO.setPageIndex(1);
		if(searchArticleVO.getPageSize() < 1) searchArticleVO.setPageSize(DEFUALT_PAGE_SIZE);
		searchArticleVO.setMngOrgCd(getCurCouncilCd(request));
		
		List<ArticleVO> list = ancmntService.listArticles(searchArticleVO);
		
		int totRecordCount = ancmntService.countArticles(searchArticleVO);
		
		//-------------------------------
		// JSON변환 응답 처리
		//-------------------------------
    	// JS-GRID 페이징 처리를 포함한 응답값 처리
    	// {data: [{...}], 
    	//  itemsCount: 255
    	// }
    	HashMap<String, Object> retMap = new HashMap<String, Object>();
    	retMap.put("data", list);
    	retMap.put("itemsCount", totRecordCount);
    	
    	PagingVO pageVO = new PagingVO();
	  	pageVO.setPagingVO(totRecordCount, searchArticleVO.getPageIndex(), searchArticleVO.getPageSize());
	  	retMap.put("pagingPageIndex"     , pageVO.getPageIndex());
	  	retMap.put("pagingTotRecordCount", pageVO.getTotRecordCount());
	  	retMap.put("pagingStartPage"     , pageVO.getStartPage());
	  	retMap.put("pagingEndPage"       , pageVO.getEndPage());
	  	retMap.put("pagingLastPage"      , pageVO.getLastPage());
	  	
    	return makeResponseEntityJson(retMap);
	}


	/**
	 * 알림 상세 내용 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/bbs/selectAncmntArticle.do")
	public String selectAncmntArticle(HttpServletRequest req, Authentication authentication, ArticleVO searchArticleVO, ModelMap model) throws Exception {

		// 읽음처리 및 상세내용 조회 
		ArticleVO articleVO = ancmntService.selectArticle(searchArticleVO);

		model.addAttribute("searchArticle", searchArticleVO);
		model.addAttribute("article", articleVO);
    	
    	return "nlib/bbs/selectAncmntArticle";
	}
	
}