package com.dbnt.faisp.main.ivsgtMgt.boardInvestigation;

import com.dbnt.faisp.main.authMgt.service.AuthMgtService;
import com.dbnt.faisp.main.ivsgtMgt.boardInvestigation.model.ArrestType;
import com.dbnt.faisp.main.ivsgtMgt.boardInvestigation.model.BoardInvestigation;
import com.dbnt.faisp.main.ivsgtMgt.boardInvestigation.model.RelatedReports;
import com.dbnt.faisp.main.ivsgtMgt.boardInvestigation.service.BoardInvestigationService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ivsgt")
public class IvsgtController {

    private final AuthMgtService authMgtService;
    private final BoardInvestigationService boardInvestigationService;

    @GetMapping("/{ivsgtType}")
    public ModelAndView arrest(@AuthenticationPrincipal UserInfo loginUser, @PathVariable("ivsgtType") String ivsgtType,BoardInvestigation boardInvestigation) {
        ModelAndView mav = new ModelAndView("ivsgt/ivsgt");
        boardInvestigation.setIvsgtType(ivsgtType);

        //메뉴권한 확인
        String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/ivsgt/"+ivsgtType).get(0).getAccessAuth();

        mav.addObject("accessAuth", accessAuth);
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());

        boardInvestigation.setQueryInfo();
        mav.addObject("boardInvestigationList", boardInvestigationService.selectBoardInvestigationList(boardInvestigation));
        boardInvestigation.setContentCnt(boardInvestigationService.selectBoardInvestigationListCnt(boardInvestigation));
        boardInvestigation.setPaginationInfo();
        mav.addObject("searchParams", boardInvestigation);
        return mav;
    }

    @GetMapping("/ivsgtEditModal")
    public ModelAndView ivsgtEditModal(@AuthenticationPrincipal UserInfo loginUser, BoardInvestigation boardInvestigation){
        ModelAndView mav = new ModelAndView("ivsgt/ivsgtEditModal");
        if(boardInvestigation.getIvsgtKey()!=null){
            boardInvestigation = boardInvestigationService.selectBoardInvestigation(boardInvestigation.getIvsgtKey());
        }else{
            boardInvestigation.setWrtOrgan(loginUser.getOgCd());
            boardInvestigation.setWrtNm(loginUser.getUserNm());
            boardInvestigation.setWrtDt(LocalDateTime.now());
        }
        mav.addObject("boardInvestigation", boardInvestigation);
        return mav;
    }

    @GetMapping("/ivsgtViewModal")
    public ModelAndView ivsgtViewModal(@AuthenticationPrincipal UserInfo loginUser, BoardInvestigation boardInvestigation){
        ModelAndView mav = new ModelAndView("ivsgt/ivsgtViewModal");
        boardInvestigation = boardInvestigationService.selectBoardInvestigation(boardInvestigation.getIvsgtKey());
        mav.addObject("boardInvestigation", boardInvestigation);
        mav.addObject("userSeq",loginUser.getUserSeq());
        //메뉴권한 확인
        mav.addObject("accessAuth", authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/ivsgt/"+boardInvestigation.getIvsgtType()).get(0).getAccessAuth());
        return mav;
    }

    @PostMapping("/saveBoardInvestigation")
    public Integer saveBoardInvestigation(@AuthenticationPrincipal UserInfo loginUser,
                                          BoardInvestigation boardInvestigation,
                                          ArrestType arrestType,
                                          RelatedReports relatedReports,
                                          MultipartHttpServletRequest request,
                                          @RequestParam(value = "fileSeq", required = false) List<Integer> deleteFileSeq){
        boardInvestigation.setWrtUserSeq(loginUser.getUserSeq());
        boardInvestigation.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
        return boardInvestigationService.saveBoardInvestigation(boardInvestigation, arrestType, relatedReports, deleteFileSeq);
    }

    @GetMapping("/searchViewModal")
    public ModelAndView searchViewModal(@AuthenticationPrincipal UserInfo loginUser, BoardInvestigation boardInvestigation){
        ModelAndView mav = new ModelAndView("ivsgt/searchViewModal");
        mav.addObject("userSeq",loginUser.getUserSeq());
        //메뉴권한 확인
        mav.addObject("accessAuth", authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/ivsgt/"+boardInvestigation.getIvsgtType()).get(0).getAccessAuth());

        boardInvestigation.setIvsgtType(null);
        mav.addObject("boardInvestigationList", boardInvestigationService.selectBoardInvestigationList(boardInvestigation));
        return mav;
    }

}
