package com.dbnt.sims.fpiMgt.affairResult;

import com.dbnt.sims.authMgt.service.AuthMgtService;
import com.dbnt.sims.codeMgt.service.CodeMgtService;
import com.dbnt.sims.fpiMgt.affair.model.AffairBoard;
import com.dbnt.sims.fpiMgt.affair.service.AffairService;
import com.dbnt.sims.fpiMgt.affairResult.model.ResultApprv;
import com.dbnt.sims.fpiMgt.affairResult.model.ResultBoard;
import com.dbnt.sims.fpiMgt.affairResult.model.ClearInfo;
import com.dbnt.sims.fpiMgt.affairResult.model.ResultInfo;
import com.dbnt.sims.fpiMgt.affairResult.service.ResultService;
import com.dbnt.sims.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("/affairResult")
public class ResultController { // 첩보수집활동 > 외사경찰 견문관리 > 계획수립
  private final ResultService resultService;
  private final AffairService affairService;
  private final AuthMgtService authMgtService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/resultMgt")
  public ModelAndView resultMgtPage(@AuthenticationPrincipal UserInfo loginUser, ResultBoard resultBoard){
    ModelAndView mav = null;
    if(resultBoard.getDashboardFlag()){
      mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultMgtDashboard");
      resultBoard.setRowCnt(5);
    }else{
      mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultMgt");
    }
    mav.addObject("page", "main");
    mav.addObject("searchUrl", "/affairResult/resultMgt");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getAccessAuth();
    switch (accessAuth){
      case "ACC001": // 조회
      case "ACC002": // 작성 // 자신이 작성한 문서만 열람가능
        resultBoard.setWrtUserSeq(loginUser.getUserSeq());
        break;
      case "ACC003": // 관리 // 자신 외 하위 기관에서 작성한 문서 열람가능
        resultBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
        break;
    }
    //결재권한 확인
    String apprvAuth = authMgtService.selectApprovalConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getApprovalAuth();
    mav.addObject("accessAuth", accessAuth);
    mav.addObject("apprvAuth", apprvAuth);
    resultBoard.setQueryInfo();
    mav.addObject("resultList", resultService.selectResultBoardList(resultBoard));
    resultBoard.setContentCnt(resultService.selectResultBoardListCnt(resultBoard));
    resultBoard.setContentCnt(0);
    resultBoard.setPaginationInfo();
    mav.addObject("searchParams", resultBoard);
    return mav;
  }

  @GetMapping("/stayPage")
  public ModelAndView apprvStayPage(@AuthenticationPrincipal UserInfo loginUser, ResultBoard resultBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultMgt");
    mav.addObject("page", "stay");
    mav.addObject("searchUrl", "/affairResult/stayPage");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getAccessAuth();
    switch (accessAuth){
      case "ACC001": // 조회
      case "ACC002": // 작성 // 자신의 관서 내에서 작성한 문서만 열람가능
        resultBoard.setWrtOrgan(loginUser.getOgCd());
        break;
      case "ACC003": // 관리 // 자신 외 하위 기관에서 작성한 문서 열람가능
        resultBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
        break;
    }
    //결재권한 확인
    String apprvAuth = authMgtService.selectApprovalConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getApprovalAuth();
    if(apprvAuth==null) {
      if(!accessAuth.equals("ACC003")){
        resultBoard.setWrtUserSeq(loginUser.getUserSeq());
        resultBoard.setUserType("normalStayList");
      }else{
        resultBoard.setUserType("managerStayList");
      }
    }else{
      mav.addObject("userNm", loginUser.getUserNm());
      switch (apprvAuth){
        case "APC004": // 계장대행
        case "APC003": // 계장 // 결재대기 문서 조회
          resultBoard.setResultState("DST002");
          break;
        case "APC002": // 부장대행
        case "APC001": // 부장 // 계장승인 문서 조회
          resultBoard.setResultState("DST004");
          break;
      }
    }
    mav.addObject("accessAuth", accessAuth);
    mav.addObject("apprvAuth", apprvAuth);
    resultBoard.setQueryInfo();
    mav.addObject("resultList", resultService.selectResultBoardList(resultBoard));
    resultBoard.setContentCnt(resultService.selectResultBoardListCnt(resultBoard));
    resultBoard.setPaginationInfo();
    mav.addObject("searchParams", resultBoard);
    return mav;
  }

  @GetMapping("/commitPage")
  public ModelAndView apprvCommitPage(@AuthenticationPrincipal UserInfo loginUser, ResultBoard resultBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultMgt");
    mav.addObject("page", "commit");
    mav.addObject("searchUrl", "/affairResult/commitPage");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getAccessAuth();
    switch (accessAuth){
      case "ACC001": // 조회
      case "ACC002": // 작성 // 자신의 관서 내에서 작성한 문서만 열람가능
        resultBoard.setWrtOrgan(loginUser.getOgCd());
        break;
      case "ACC003": // 관리 // 자신 외 하위 기관에서 작성한 문서 열람가능
        resultBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
        break;
    }
    //결재권한 확인
    String apprvAuth = authMgtService.selectApprovalConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getApprovalAuth();
    if(apprvAuth==null) {
      if(!accessAuth.equals("ACC003")){
        resultBoard.setWrtUserSeq(loginUser.getUserSeq());
        resultBoard.setUserType("normalCommitList");
      }else{
        resultBoard.setUserType("managerCommitList");
      }
    }else{
      switch (apprvAuth) {
        case "APC004": // 계장대행
        case "APC003": // 계장 // 계장반려, 계장승인 문서 조회
          resultBoard.setUserType("sectionCommitList");
          break;
        case "APC002": // 부장대행
        case "APC001": // 부장 // 부장반려, 부장승인 문서 조회
          resultBoard.setUserType("headCommitList");
          break;
      }
    }
    mav.addObject("accessAuth", accessAuth);
    mav.addObject("apprvAuth", apprvAuth);
    resultBoard.setQueryInfo();
    mav.addObject("resultList", resultService.selectResultBoardList(resultBoard));
    resultBoard.setContentCnt(resultService.selectResultBoardListCnt(resultBoard));
    resultBoard.setPaginationInfo();
    mav.addObject("searchParams", resultBoard);
    return mav;
  }

  @GetMapping("/resultEditModal")
  public ModelAndView resultEditModal(@AuthenticationPrincipal UserInfo loginUser, ResultBoard resultBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultEditModal");
    if(resultBoard.getResultKey()!=null){
      resultBoard = resultService.selectResultBoard(resultBoard.getResultKey());
    }else{
      resultBoard.setWrtOrgan(loginUser.getOgCd());
      resultBoard.setWrtPart(loginUser.getOfcCd());
      resultBoard.setWrtUserSeq(loginUser.getUserSeq());
      resultBoard.setWrtUserNm(loginUser.getUserNm());
      resultBoard.setWrtDt(LocalDateTime.now());
    }
    mav.addObject("result", resultBoard);
    mav.addObject("categoryList", codeMgtService.selectCodeMgtList("RIC"));
    mav.addObject("codeList", codeMgtService.selectCodeMgtList("RID"));
    return mav;
  }

  @GetMapping("/affairListModal")
  public ModelAndView affairListModal(@AuthenticationPrincipal UserInfo loginUser, AffairBoard affair){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/affairListModal");
    affair.setWrtUserSeq(loginUser.getUserSeq());
    affair.setRatingOrgan(loginUser.getOgCd());
    affair.setFirstIndex(0);
    affair.setRowCnt(9999);
    mav.addObject("affairList", affairService.selectAffairBoardList(affair));
    return mav;
  }

  @GetMapping("/resultViewModal")
  public ModelAndView resultViewModal(@AuthenticationPrincipal UserInfo loginUser, ResultBoard resultBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultViewModal");
    resultBoard = resultService.selectResultBoard(resultBoard.getResultKey());
    mav.addObject("result", resultBoard);
    mav.addObject("userSeq",loginUser.getUserSeq());
    //메뉴권한 확인
    mav.addObject("accessAuth", authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getAccessAuth());
    mav.addObject("apprvAuth", authMgtService.selectApprovalConfigList(loginUser.getUserSeq(), "/affairResult/resultMgt").get(0).getApprovalAuth());
    return mav;
  }

  @PostMapping("/saveResult")
  public Integer saveResult(ResultBoard resultBoard,
                            MultipartHttpServletRequest request,
                            @RequestParam(value = "fileSeq", required = false) List<Integer> deleteFileSeq){
    resultBoard.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
    return resultService.saveResultBoard(resultBoard, deleteFileSeq);
  }


  @PostMapping("/resultStateChange")
  public Integer resultStateChange(@AuthenticationPrincipal UserInfo loginUser, ResultApprv apprv){
    apprv.setUserSeq(loginUser.getUserSeq());
    apprv.setUserNm(loginUser.getUserNm());
    apprv.setSaveDt(LocalDateTime.now());
    return resultService.resultStateChange(apprv);
  }

  @GetMapping("/getClearInfoRow")
  public ModelAndView getClearInfoRow(ClearInfo info){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/clearInfoRow");
    mav.addObject("info", info);
    mav.addObject("categoryList", codeMgtService.selectCodeMgtList("RIC"));
    mav.addObject("codeList", codeMgtService.selectCodeMgtList("RID"));
    return mav;
  }
  @GetMapping("/getResultInfoRow")
  public ModelAndView getResultInfoRow(ResultInfo info){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairResult/resultInfoRow");
    mav.addObject("info", info);
    return mav;
  }
}
