package egovframework.com.its.web.server.controller;

import egovframework.com.its.web.server.service.NoticeService;
import egovframework.com.its.web.server.util.PageCriteria;
import egovframework.com.its.web.server.vo.NoticeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/archive")
public class AchiveController {

    private final NoticeService noticeService;
    private PageCriteria criteria;

    @PostConstruct
    void init() {
        this.criteria = new PageCriteria();
    }
//
    @RequestMapping(value = {"/archiveList.do","/archiveList/{page}.do"}, method = RequestMethod.GET)
    public String archiveListPage(Model model, @PathVariable(required = false) String page) {
        HashMap <String, String> paramMap = new HashMap<>();
        paramMap.put("boardId", "2");
        if (NumberUtils.isParsable(page)) {
            paramMap.put("page", page);
        }
        Map<String, Object> resultMap = noticeService.selectBoardList(paramMap, this.criteria);
        model.addAttribute("siteSubInfo", "sub archive");
        model.addAttribute("subTitle", "자료실");
        model.addAttribute("noticeList", resultMap.get("noticeList"));
        model.addAttribute("pageMaker", resultMap.get("pageMaker"));
        return "archive/list.tiles";
    }

    @RequestMapping(value = "/listView/{boardNo}.do", method = RequestMethod.GET)
    public String view(Model model, @PathVariable HashMap<String, String> paramMap) {
        model.addAttribute("siteSubInfo", "sub archive");
        model.addAttribute("subTitle", "자료실 상세페이지");
        paramMap.put("boardId", "2");
        NoticeVO result = this.noticeService.selectBoardView(paramMap);
        model.addAttribute("notice", result);
        return "archive/listView.tiles";
    }

    @RequestMapping(value = "/getArchiveList.do", method = RequestMethod.POST)
    @ResponseBody
    public  Map<String, Object> getArchiveList(String page, String searchText, String searchType) {
        HashMap <String, String> paramMap = new HashMap<>();
        paramMap.put("boardId", "2");
        paramMap.put("page", page);
        paramMap.put("searchType", searchType);
        paramMap.put("searchText", searchText);
        Map<String, Object> resultMap = noticeService.selectBoardList(paramMap, this.criteria);
        return resultMap;
    }
}
