package com.dbnt.sims.fpiMgt.affair;

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.model.AffairRating;
import com.dbnt.sims.fpiMgt.affair.service.AffairService;
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.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/affair")
public class AffairController { // 첩보수집활동 > 외사경찰 견문관리 > 견문관리
  private final AffairService affairService;
  private final AuthMgtService authMgtService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/affairMgt")
  public ModelAndView affairMgtPage(@AuthenticationPrincipal UserInfo loginUser, AffairBoard affairBoard){
    ModelAndView mav;
    if(affairBoard.getDashboardFlag()){
      mav = new ModelAndView("igActivities/fpiMgt/affair/affairMgtDashboard");
      affairBoard.setRowCnt(5);
    }else{
      mav = new ModelAndView("igActivities/fpiMgt/affair/affairMgt");
    }
    mav.addObject("page", "main");
    mav.addObject("searchUrl", "/affair/affairMgt");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/affair/affairMgt?affairCategory="+affairBoard.getAffairCategory()).get(0).getAccessAuth();
    switch (accessAuth){
      case "ACC001": // 조회
      case "ACC002": // 작성
        affairBoard.setWrtUserSeq(loginUser.getUserSeq());
        break;
      case "ACC003": // 관리 // 자신 외 하위 기관에서 작성한 문서 열람가능
        affairBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
        break;
    }
    //결재권한 확인
    String apprvAuth = authMgtService.selectApprovalConfigList
        (loginUser.getUserSeq(), "/affair/affairMgt?affairCategory="+affairBoard.getAffairCategory()).get(0).getApprovalAuth();
    affairBoard.setRatingOrgan(loginUser.getOgCd());

    mav.addObject("accessAuth", accessAuth);
    mav.addObject("apprvAuth", apprvAuth);
    affairBoard.setQueryInfo();
    mav.addObject("affairList", affairService.selectAffairBoardList(affairBoard));
    affairBoard.setContentCnt(affairService.selectAffairBoardListCnt(affairBoard));
    affairBoard.setPaginationInfo();
    mav.addObject("searchParams", affairBoard);
    return mav;
  }

  @GetMapping("/stayPage")
  public ModelAndView apprvStayPage(@AuthenticationPrincipal UserInfo loginUser, AffairBoard affairBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affair/affairMgt");
    mav.addObject("page", "stay");
    mav.addObject("searchUrl", "/affair/stayPage");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/affair/affairMgt?affairCategory="+affairBoard.getAffairCategory()).get(0).getAccessAuth();
    switch (accessAuth){
      case "ACC001": // 조회
      case "ACC002": // 작성
        break;
      case "ACC003": // 관리 // 자신 외 하위 기관에서 작성한 문서 열람가능
        affairBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
        mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
        break;
    }
    //결재권한 확인
    String apprvAuth = authMgtService.selectApprovalConfigList
        (loginUser.getUserSeq(), "/affair/affairMgt?affairCategory="+affairBoard.getAffairCategory()).get(0).getApprovalAuth();
    affairBoard.setRatingOrgan(loginUser.getOgCd());
    if(apprvAuth==null) {
      if(!accessAuth.equals("ACC003")){
        affairBoard.setWrtUserSeq(loginUser.getUserSeq());
        affairBoard.setUserType("normalStayList");
      }else{
        affairBoard.setUserType("managerStayList");
      }
    }else{
      mav.addObject("userNm", loginUser.getUserNm());
      switch (apprvAuth){
        case "APC004": // 계장대행
        case "APC003": // 계장 // 결재대기 문서 조회
          affairBoard.setAffairStatus("DST002");
          break;
        case "APC002": // 부장대행
        case "APC001": // 부장 // 계장승인 문서 조회
          affairBoard.setAffairStatus("DST004");
          break;
      }
    }
    mav.addObject("userOrgan", loginUser.getOgCd());
    mav.addObject("accessAuth", accessAuth);
    mav.addObject("apprvAuth", apprvAuth);
    affairBoard.setQueryInfo();
    mav.addObject("affairList", affairService.selectAffairBoardList(affairBoard));
    affairBoard.setContentCnt(affairService.selectAffairBoardListCnt(affairBoard));
    affairBoard.setPaginationInfo();
    mav.addObject("searchParams", affairBoard);
    mav.addObject("ratingCodeList", codeMgtService.selectCodeMgtList("AAR"));
    return mav;
  }

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

  @GetMapping("/affairEditModal")
  public ModelAndView affairEditModal(@AuthenticationPrincipal UserInfo loginUser, AffairBoard affairBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affair/affairEditModal");
    if(affairBoard.getAffairKey()!=null){
      affairBoard = affairService.selectAffairBoard(affairBoard.getAffairKey());
      for(AffairRating rating: affairBoard.getRateList()){
        if(rating.getRatingOrgan().equals(loginUser.getOgCd())){
          affairBoard.setAffairStatus(rating.getAffairStatus());
        }
      }
    }else{
      affairBoard.setWrtOrgan(loginUser.getOgCd());
      affairBoard.setWrtPart(loginUser.getOfcCd());
      affairBoard.setWrtUserSeq(loginUser.getUserSeq());
      affairBoard.setWrtUserNm(loginUser.getUserNm());
      affairBoard.setWrtDt(LocalDateTime.now());
      affairBoard.setReportDt(LocalDate.now());
    }
    mav.addObject("affair", affairBoard);
    return mav;
  }

  @GetMapping("/affairViewModal")
  public ModelAndView affairViewModal(@AuthenticationPrincipal UserInfo loginUser, AffairBoard affairBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affair/affairViewModal");
    affairBoard = affairService.selectAffairBoard(affairBoard.getAffairKey());
    for(AffairRating rating: affairBoard.getRateList()){
      if(rating.getRatingOrgan().equals(loginUser.getOgCd())){
        affairBoard.setAffairStatus(rating.getAffairStatus());
      }
    }
    mav.addObject("affair", affairBoard);
    mav.addObject("userSeq",loginUser.getUserSeq());
    mav.addObject("userOrgan", loginUser.getOgCd());
    //메뉴권한 확인
    mav.addObject("accessAuth", authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/affair/affairMgt?affairCategory="+affairBoard.getAffairCategory()).get(0).getAccessAuth());
    String apprvAuth = authMgtService.selectApprovalConfigList
        (loginUser.getUserSeq(), "/affair/affairMgt?affairCategory="+affairBoard.getAffairCategory()).get(0).getApprovalAuth();
    mav.addObject("apprvAuth", apprvAuth);
    if(apprvAuth!=null && (apprvAuth.equals("APC002") || apprvAuth.equals("APC001"))){
      mav.addObject("ratingCodeList", codeMgtService.selectCodeMgtList("AAR"));
    }
    return mav;
  }

  @PostMapping("/saveAffair")
  public Integer saveAffair(AffairBoard affairBoard,
                          MultipartHttpServletRequest request,
                          @RequestParam(value = "fileSeq", required = false) List<Integer> deleteFileSeq){
    affairBoard.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
    return affairService.saveAffairBoard(affairBoard, deleteFileSeq);
  }

  @PostMapping("/affairStateChange")
  @ResponseBody
  public Integer affairStateChange(@AuthenticationPrincipal UserInfo loginUser, @RequestBody List<AffairRating> ratingList){
    return affairService.affairStateChange(loginUser, ratingList);
  }
}
