package com.dbnt.sims.fpiMgt.affairPlan;

import com.dbnt.sims.authMgt.service.AuthMgtService;
import com.dbnt.sims.fpiMgt.affairPlan.model.PlanApprv;
import com.dbnt.sims.fpiMgt.affairPlan.model.PlanBoard;
import com.dbnt.sims.fpiMgt.affairPlan.service.PlanService;
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("/affairPlan")
public class PlanController { // 첩보수집활동 > 외사경찰 견문관리 > 계획수립
  private final PlanService planService;
  private final AuthMgtService authMgtService;

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

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

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

  @GetMapping("/planEditModal")
  public ModelAndView planEditModal(@AuthenticationPrincipal UserInfo loginUser, PlanBoard planBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairPlan/planEditModal");
    if(planBoard.getPlanKey()!=null){
      planBoard = planService.selectPlanBoard(planBoard.getPlanKey());
    }else{
      planBoard.setWrtOrgan(loginUser.getOgCd());
      planBoard.setWrtPart(loginUser.getOfcCd());
      planBoard.setWrtUserSeq(loginUser.getUserSeq());
      planBoard.setWrtUserNm(loginUser.getUserNm());
      planBoard.setWrtDt(LocalDateTime.now());
    }
    mav.addObject("plan", planBoard);
    return mav;
  }

  @GetMapping("/planViewModal")
  public ModelAndView planViewModal(@AuthenticationPrincipal UserInfo loginUser, PlanBoard planBoard){
    ModelAndView mav = new ModelAndView("igActivities/fpiMgt/affairPlan/planViewModal");
    planBoard = planService.selectPlanBoard(planBoard.getPlanKey());
    mav.addObject("plan", planBoard);
    mav.addObject("userSeq",loginUser.getUserSeq());
    //메뉴권한 확인
    mav.addObject("accessAuth", authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/affairPlan/planMgt").get(0).getAccessAuth());
    mav.addObject("apprvAuth", authMgtService.selectApprovalConfigList(loginUser.getUserSeq(), "/affairPlan/planMgt").get(0).getApprovalAuth());
    return mav;
  }

  @PostMapping("/savePlan")
  public Integer savePlan(PlanBoard planBoard,
                          @RequestParam(value = "planInfos", required = false) List<String> planInfos,
                          @RequestParam(value = "detailPlanInfos", required = false)List<String> detailPlanInfos,
                          MultipartHttpServletRequest request,
                          @RequestParam(value = "fileSeq", required = false) List<Integer> deleteFileSeq){
    planBoard.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
    return planService.savePlanBoard(planBoard, planInfos, detailPlanInfos, deleteFileSeq);
  }


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