package incheon.res.mng.nt.faq.web;

import incheon.com.cmm.ComDefaultVO;
import incheon.com.cmm.ResponseCode;
import incheon.com.cmm.api.DefaultApiResponse;
import incheon.com.security.vo.LoginVO;
import incheon.res.mng.nt.faq.service.MngNtFaqService;
import incheon.res.mng.nt.faq.vo.MngNtFaq;
import incheon.res.mng.nt.faq.vo.MngNtFaqVO;
import incheon.res.rdm.com.attachfile.service.RdmComAttachfileService;
import incheon.res.rdm.com.attachfile.service.RdmFileMngUtil;
import incheon.res.rdm.com.attachfile.vo.RdmComAttachfileVO;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

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

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

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

@Controller
@SessionAttributes(types = ComDefaultVO.class)
@RequestMapping("${api.rcs-path}")
public class MngNtFaqController {

    @Resource
    private MngNtFaqService mngNtFaqService;

    @Resource(name = "propertiesService")
    protected EgovPropertyService propertyService;

    @Resource(name="RdmFileMngUtil")
    private RdmFileMngUtil fileUtil;

    @Resource
    private RdmComAttachfileService rdmComAttachfileService;

    Logger logger = Logger.getLogger(this.getClass());

    /**
     * @MethodDESC : faq관리 조회.
     */
    @RequestMapping(value = "/MngNtFaqL010.do")
    public String selectFaqList(@RequestParam(defaultValue = "1") int pageIndex, @ModelAttribute("mngNtFaqVO")MngNtFaqVO vo, ModelMap model) throws Exception{
        vo.setPageIndex(pageIndex);

        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setCurrentPageNo(pageIndex);
        paginationInfo.setRecordCountPerPage(vo.getRecordCountPerPage());
        paginationInfo.setPageSize(vo.getPageSize());

        vo.setFirstIndex(paginationInfo.getFirstRecordIndex());
        vo.setLastIndex(paginationInfo.getLastRecordIndex());
        vo.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());

        int totCnt = mngNtFaqService.countFaq(vo);
        paginationInfo.setTotalRecordCount(totCnt);
        model.addAttribute("paginationInfo", paginationInfo);

        List resultList = mngNtFaqService.selectFaqList(vo);
        List faqCdeList = mngNtFaqService.selectFaqCdeList(vo);

        model.addAttribute("faqCdeList", faqCdeList);
        model.addAttribute("resultList", resultList);
        model.addAttribute("resultCnt", totCnt);
        return "res/mng/nt/faq/MngNtFaqL010";
    }

    @RequestMapping(value = "/MngNtFaqR010.do")
    public String selectFaq(@ModelAttribute("loginUser") LoginVO loginVO, @ModelAttribute("mngNtFaqVO") MngNtFaqVO vo, ModelMap model) throws Exception{

        String sessionUserId = loginVO.getUserUnqId();
        vo.setUserAcntMngNo(sessionUserId);

        //상세정보 조회
        MngNtFaq result = mngNtFaqService.selectFaq(vo);
        model.addAttribute("result", result);
        model.addAttribute("searchVo", vo);

        //조회수 증가
        mngNtFaqService.updateFaqHitCntAdd(vo);
        return "res/mng/nt/faq/MngNtFaqR010";

    }

    @RequestMapping(value = "MngNtFaqU010.do")
    public String updateFaqView(@ModelAttribute("loginUser") LoginVO loginVO, @ModelAttribute("mngNtFaqVO") MngNtFaqVO vo, ModelMap model) throws Exception{
        String sessionUserId = loginVO.getUserUnqId();
        vo.setUserAcntMngNo(sessionUserId);

        MngNtFaq result = mngNtFaqService.selectFaq(vo);
        List faqCdeList = mngNtFaqService.selectFaqCdeList(vo);

        model.addAttribute("faqCdeList", faqCdeList);
        model.addAttribute("mngNtFaqVO",result);
        model.addAttribute("searchVo",vo);
        return "res/mng/nt/faq/MngNtFaqU010";
    }

    @RequestMapping(value = "MngNtFaqU011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> updateFaq(@ModelAttribute("mngNtFaqVO") MngNtFaqVO vo) throws Exception{
        Map<String, Object> resultMap = new HashMap<>();
        mngNtFaqService.updateFaq(vo);

        resultMap.put("result", "succ");
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }

    @RequestMapping(value = "MngNtFaqC010.do")
    public String insertFaqView(@ModelAttribute("loginUser") LoginVO loginVO, @ModelAttribute("mngNtFaqVO") MngNtFaqVO vo, ModelMap model) throws Exception{
        String sessionUserId = loginVO.getUserUnqId();
        vo.setUserAcntMngNo(sessionUserId);

        List faqCdeList = mngNtFaqService.selectFaqCdeList(vo);
        model.addAttribute("faqCdeList", faqCdeList);

        return "res/mng/nt/faq/MngNtFaqC010";
    }

    @RequestMapping(value = "MngNtFaqC011.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> insertFaq(@ModelAttribute("loginUser") LoginVO loginVO,
                                                                             @ModelAttribute("mngNtFaqVO") MngNtFaqVO vo) throws Exception{


        String sessionUserId = loginVO.getUserUnqId();
        vo.setUserAcntMngNo(sessionUserId);

        Map<String, Object> resultMap = new HashMap<String, Object>();

        String faqIdn = mngNtFaqService.selectMaxFaqIdn(vo);
        vo.setPstSn(faqIdn);
        mngNtFaqService.insertFaq(vo);
        resultMap.put("result","succ");
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }

    @RequestMapping(value = "MngNtFaqD010.do")
    public ResponseEntity<DefaultApiResponse<Map<String, Object>>> deleteFaq(@ModelAttribute MngNtFaqVO vo) throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();

        mngNtFaqService.deleteFaq(vo);
        resultMap.put("success", true);
        return ResponseEntity.ok(DefaultApiResponse.success(resultMap));
    }

}
