package com.dbnt.faisp.main.fpiMgt.sri;

import com.dbnt.faisp.main.authMgt.service.AuthMgtService;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.faRpt.model.FaRptReadUser;
import com.dbnt.faisp.main.fpiMgt.sri.model.Sri;
import com.dbnt.faisp.main.fpiMgt.sri.model.SriReadUser;
import com.dbnt.faisp.main.fpiMgt.sri.service.SriService;
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("/sri")
public class SriController {
	
  private final SriService sriService;
  private final AuthMgtService authMgtService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/sriBoard")
  public ModelAndView faRptBoard(@AuthenticationPrincipal UserInfo loginUser, Sri sri){
    ModelAndView mav = new ModelAndView("sri/sriBoard");

    if(sri.getActiveTab()==null){
    	sri.setActiveTab("send");
    }
    mav.addObject("searchUrl", "/sri/sriBoard");
    String accessAuth = authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/sri/sriBoard").get(0).getAccessAuth();
    mav.addObject("accessAuth", accessAuth);

    if(sri.getActiveTab().equals("send")){
    	sri.setWrtUserSeq(loginUser.getUserSeq());
    }else if(sri.getActiveTab().equals("receive")){
    	sri.setStatus("DST007");
    	sri.setReceiveUserSeq(loginUser.getUserSeq());
    }else if(sri.getActiveTab().equals("all") && accessAuth.equals("ACC003")){
    	sri.setDownOrganCdList(loginUser.getDownOrganCdList());
    }else if(sri.getActiveTab().equals("all")){
    	sri.setActiveTab("send");
    	sri.setWrtUserSeq(loginUser.getUserSeq());
    }
    if(accessAuth.equals("ACC003")){
      mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    }
   
    sri.setQueryInfo();
    mav.addObject("SriList", sriService.selectSriList(sri));
    sri.setContentCnt(sriService.selectSriCnt(sri));
    sri.setPaginationInfo();
    
    mav.addObject("searchParams", sri);
    return mav;
  }

  @GetMapping("/sriEditModal")
  public ModelAndView faRptEditModal(@AuthenticationPrincipal UserInfo loginUser, Sri sri){
    ModelAndView mav = new ModelAndView("sri/sriEditModal");
    if(sri.getFaSriKey()!=null){
    	sri = sriService.selectFaSriBoard(sri.getFaSriKey(), null);
    }else{
    	if(sri.getRefKey()!=null){
            SriReadUser readUser = new SriReadUser();
            readUser.setUserSeq(sri.getWrtUserSeq());
            readUser.setOgCd(sri.getWrtOrgan());
            readUser.setOfcCd(sri.getWrtPart());
            readUser.setTitleCd(sri.getWrtTitle());
            readUser.setUserNm(sri.getWrtUserNm());
            sri.setReadUserList(new ArrayList<>());
            sri.getReadUserList().add(readUser);
          }
    	  
    	sri.setWrtUserSeq(loginUser.getUserSeq());
    	sri.setWrtOrgan(loginUser.getOgCd());
    	sri.setWrtPart(loginUser.getOfcCd());
    	sri.setWrtTitle(loginUser.getTitleCd());
    	sri.setWrtUserNm(loginUser.getUserNm());
    	sri.setWrtDt(LocalDateTime.now());
    }
    mav.addObject("SriCategoryList", codeMgtService.selectCodeMgtList("SRI",""));
    mav.addObject("sri", sri);
    return mav;
  }
  
  @PostMapping("/selectedUserTable")
  @ResponseBody
  public ModelAndView selectedUserTable(@RequestBody List<FaRptReadUser> userList){
    ModelAndView mav = new ModelAndView("sri/readUserRow");
    mav.addObject("userList", userList);
    return mav;
  }
  
  @PostMapping("/saveSri")
  public Integer saveFaRpt(Sri sri,
                          MultipartHttpServletRequest request,
                          @RequestParam(value = "fileSeq", required = false) List<Integer> deleteFileSeq){
	  sri.setMultipartFileList(request.getMultiFileMap().get("uploadFiles"));
    return sriService.saveSriBoard(sri, deleteFileSeq);
  }

  @GetMapping("/sriViewModal")
  public ModelAndView faRptViewModal(@AuthenticationPrincipal UserInfo loginUser, Sri sri){
    ModelAndView mav = new ModelAndView("sri/sriViewModal");
    mav.addObject("FaSri", sriService.selectFaSriBoard(sri.getFaSriKey(), loginUser.getUserSeq()));
    mav.addObject("userSeq",loginUser.getUserSeq());
    //메뉴권한 확인
    mav.addObject("accessAuth", authMgtService.selectAccessConfigList
        (loginUser.getUserSeq(), "/sri/sriBoard").get(0).getAccessAuth());
    return mav;
  }
  
  @PostMapping("/sriDelete")
  public void sriDelete(@AuthenticationPrincipal UserInfo loginUser, @RequestBody List<Sri> sri){
	  sriService.sriDelete(sri);
  }


  
}
