
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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
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 org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import nlib.bbs.service.ArticleVO;
import nlib.bbs.service.BoardService;
import nlib.bbs.service.BoardServiceApi;
import nlib.cmm.NlibCommonController;
import nlib.cmm.crypto.AriaCrypto;
import nlib.cmm.crypto.NlibPasswordEncoder;
import nlib.cmm.service.CodeService;
import nlib.cmm.service.NlibProperty;
import nlib.cmm.service.NotificationVO;
import nlib.cmm.service.PagingVO;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.user.service.NlibLoginVO;
import nlib.util.StringUtil;

@Controller
public class FaqController extends NlibCommonController {

	private static final Logger log = LoggerFactory.getLogger(FaqController.class);
	
	static final int DEFUALT_PAGE_SIZE = NlibProperty.getInt("list.paging.page.size", 10);
	
	@Resource(name = "faqService")
	private BoardService faqService;
	
	@Resource(name="codeService")
	private CodeService codeService;

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


		List<Map<String, String>> questionTypeList = codeService.listCodes("NLIB_FAQ_TYPE_CD");
		model.addAttribute("questionTypeList", questionTypeList);
		model.addAttribute("searchArticle", searchArticleVO);
    	
    	return "nlib/bbs/listFaqs";
	}

	/**
	 * 목록 조회한다.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/bbs/listFaqsAjax.do")
	public ResponseEntity<String> listFaqsAjax(
			HttpServletRequest req, 
			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(req));
		
		List<ArticleVO> list = faqService.listArticles(searchArticleVO);
		
		int totRecordCount = faqService.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/selectFaqArticleAjax.do")
	public ResponseEntity<String> selectFaqArticleAjax(HttpServletRequest req, Authentication authentication, @RequestBody ArticleVO searchArticleVO) throws Exception {

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

		//-------------------------------
		// JSON변환 응답 처리
		//-------------------------------
    	// JS-GRID 페이징 처리를 포함한 응답값 처리
    	// {data: [{...}], 
    	//  itemsCount: 255
    	// }
    	HashMap<String, Object> retMap = new HashMap<String, Object>();
    	retMap.put("data", articleVO);
    	
    	return makeResponseEntityJson(retMap);
	}	
}