package com.dbnt.faisp.main.faRpt;

import com.dbnt.faisp.main.authMgt.service.AuthMgtService;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.faRpt.model.FaRptBoard;
import com.dbnt.faisp.main.faRpt.model.FaRptReadUser;
import com.dbnt.faisp.main.faRpt.service.FaRptService;
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.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/faRpt")
public class FaRptController {
  private final FaRptService faRptService;
  private final AuthMgtService authMgtService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/faRptBoard")
  public ModelAndView faRptBoard(@AuthenticationPrincipal UserInfo loginUser, FaRptBoard faRptBoard){
    ModelAndView mav;
    if(faRptBoard.getDashboardFlag()){
      mav = new ModelAndView("faRpt/faRptDashboard");
      faRptBoard.setRowCnt(5);
    }else{
      mav = new ModelAndView("faRpt/faRptBoard");
    }
    if(faRptBoard.getActiveTab()==null){
      faRptBoard.setActiveTab("send");
    }
    mav.addObject("searchUrl", "/faRpt/faRptBoard");
    String accessAuth = authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/affairPlan/planMgt").get(0).getAccessAuth();
    mav.addObject("accessAuth", accessAuth);

    if(faRptBoard.getActiveTab().equals("send")){
      faRptBoard.setWrtUserSeq(loginUser.getUserSeq());
    }else if(faRptBoard.getActiveTab().equals("receive")){
      faRptBoard.setStatus("DST007");
      faRptBoard.setReceiveUserSeq(loginUser.getUserSeq());
    }else if(faRptBoard.getActiveTab().equals("all") && accessAuth.equals("ACC003")){
      faRptBoard.setDownOrganCdList(loginUser.getDownOrganCdList());
    }else if(faRptBoard.getActiveTab().equals("all")){
      faRptBoard.setActiveTab("send");
      faRptBoard.setWrtUserSeq(loginUser.getUserSeq());
    }
    if(accessAuth.equals("ACC003")){
      mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    }

    faRptBoard.setQueryInfo();
    mav.addObject("faRptList", faRptService.selectFaRptList(faRptBoard));
    faRptBoard.setContentCnt(faRptService.selectFaRptCnt(faRptBoard));
    faRptBoard.setPaginationInfo();
    mav.addObject("searchParams", faRptBoard);
    return mav;
  }

  @GetMapping("/faRptEditModal")
  public ModelAndView faRptEditModal(@AuthenticationPrincipal UserInfo loginUser, FaRptBoard faRptBoard){
    ModelAndView mav = new ModelAndView("faRpt/faRptEditModal");
    if(faRptBoard.getFaRptKey()!=null){
      faRptBoard = faRptService.selectFaRptBoard(faRptBoard.getFaRptKey(), null);
    }else{
      if(faRptBoard.getRefKey()!=null){
        FaRptReadUser readUser = new FaRptReadUser();
        readUser.setUserSeq(faRptBoard.getWrtUserSeq());
        readUser.setOgCd(faRptBoard.getWrtOrgan());
        readUser.setOfcCd(faRptBoard.getWrtPart());
        readUser.setTitleCd(faRptBoard.getWrtUserGrd());
        readUser.setUserNm(faRptBoard.getWrtUserNm());
        faRptBoard.setReadUserList(new ArrayList<>());
        faRptBoard.getReadUserList().add(readUser);
      }
      faRptBoard.setWrtUserSeq(loginUser.getUserSeq());
      faRptBoard.setWrtOrgan(loginUser.getOgCd());
      faRptBoard.setWrtPart(loginUser.getOfcCd());
      faRptBoard.setWrtUserGrd(loginUser.getTitleCd());
      faRptBoard.setWrtUserNm(loginUser.getUserNm());
      faRptBoard.setWrtDt(LocalDateTime.now());
    }
    mav.addObject("frCategoryList", codeMgtService.selectCodeMgtList("FRC", ""));
    mav.addObject("faRpt", faRptBoard);
    return mav;
  }

  @GetMapping("/faRptViewModal")
  public ModelAndView faRptViewModal(@AuthenticationPrincipal UserInfo loginUser, FaRptBoard faRptBoard){
    ModelAndView mav = new ModelAndView("faRpt/faRptViewModal");
    mav.addObject("faRpt", faRptService.selectFaRptBoard(faRptBoard.getFaRptKey(), loginUser.getUserSeq()));
    mav.addObject("userSeq",loginUser.getUserSeq());
    //메뉴권한 확인
    mav.addObject("accessAuth", authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/faRpt/faRptBoard").get(0).getAccessAuth());
    return mav;
  }

  @PostMapping("/saveFaRpt")
  public Integer saveFaRpt(FaRptBoard faRptBoard,
                          MultipartHttpServletRequest request,
                          @RequestParam(value = "fileSeq", required = false) List<Integer> deleteFileSeq){
    faRptBoard.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
    return faRptService.saveFaRptBoard(faRptBoard, deleteFileSeq);
  }

  @PostMapping("/selectedUserTable")
  @ResponseBody
  public ModelAndView selectedUserTable(@RequestBody List<FaRptReadUser> userList){
    ModelAndView mav = new ModelAndView("faRpt/readUserRow");
    mav.addObject("userList", userList);
    return mav;
  }
}
