package com.dbnt.faisp.main.userInfo;

import com.dbnt.faisp.kwms.service.KwmsService;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.userInfo.model.UserInoutLog;
import com.dbnt.faisp.main.userInfo.model.UserRequestLog;
import com.dbnt.faisp.main.userInfo.service.UserInfoService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.userInfo.service.UserLogService;
import lombok.RequiredArgsConstructor;

import java.util.List;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequiredArgsConstructor
@RequestMapping("/userMgt")
public class UserMgtController {

  private final UserInfoService userInfoService;
  private final UserLogService userLogService;
  private final CodeMgtService codeMgtService;
  private final KwmsService kwmsService;

  @GetMapping("/userMgtPage")
  public ModelAndView codeMgtPage(UserInfo userInfo) {
    ModelAndView mav = new ModelAndView("adminPage/userMgt/userMgt");
    userInfo.setQueryInfo();
    if(userInfo.getUserStatus() == null || userInfo.getUserStatus().equals("")) {
    	userInfo.setUserStatus("USC003");
    }
    mav.addObject("userInfoList", userInfoService.selectUserInfoList(userInfo));
    userInfo.setContentCnt(userInfoService.selectUserInfoListCnt(userInfo));
    userInfo.setPaginationInfo();
    mav.addObject("searchParams", userInfo);
    return mav;
  }

  @GetMapping("/userEditModal")
  public ModelAndView menuEditModal(UserInfo userInfo){
    ModelAndView mav = new ModelAndView("adminPage/userMgt/userEditModal");
    mav.addObject("ogList", codeMgtService.selectCodeMgtList("OG", ""));
    mav.addObject("ofcList", codeMgtService.selectCodeMgtList("OFC", ""));
    mav.addObject("titleList", codeMgtService.selectCodeMgtList("JT", ""));
    mav.addObject("outturnList", codeMgtService.selectCodeMgtList("OTC", ""));
    mav.addObject("seriesList", codeMgtService.selectCodeMgtList("SRC", ""));
    mav.addObject("languageList", codeMgtService.selectCodeMgtList("LNG", ""));
    mav.addObject("statusList", codeMgtService.selectCodeMgtList("USC", ""));

    mav.addObject("userInfo", userInfoService.selectUserInfo(userInfo.getUserSeq()));
    return mav;
  }
  
  @PostMapping("/userApproval")
  @ResponseBody
  public int userApproval(@RequestBody List<UserInfo> userInfo){
    return userInfoService.updateUserApproval(userInfo);
  }
  
  @PostMapping("/userCompanion")
  @ResponseBody
  public int userCompanion(@RequestBody List<UserInfo> userInfo){
    return userInfoService.updateUserCompanion(userInfo);
  }
  
  @PostMapping("/updateUserInfo")
  public void updateUserInfo(@AuthenticationPrincipal UserInfo loginUser,UserInfo userInfo) {
	  userInfoService.updateUserInfo(loginUser,userInfo);
  }
  
  @PostMapping("/userDelete")
  @ResponseBody
  public void userDelete(@RequestBody List<UserInfo> userInfo) {
	  userInfoService.userDelete(userInfo);  
  }


  @PostMapping("/syncUserInfoToKwms")
  @ResponseBody
  public String syncUserInfoToKwms(@AuthenticationPrincipal UserInfo loginUser,@RequestBody List<UserInfo> infoList){
    for(UserInfo info: infoList){
      UserInfo kwmsInfo = kwmsService.selectEmpInfo(info.getDicCode());
      if(kwmsInfo!=null){
        kwmsInfo.setUserSeq(info.getUserSeq());
        userInfoService.updateUserInfo(loginUser,kwmsInfo);
      }
    }
    if(infoList.size()==1){
      return infoList.get(0).getUserSeq().toString();
    }else{
      return "";
    }
  }

  @GetMapping("/userLog/requestLog")
  public ModelAndView requestLog(@AuthenticationPrincipal UserInfo loginUser, UserRequestLog requestLog){
    ModelAndView mav = new ModelAndView("adminPage/userLog/requestLog");
    requestLog.setDownOrganCdList(loginUser.getDownOrganCdList());
    requestLog.setQueryInfo();
    mav.addObject("logList", userLogService.selectRequestLogList(requestLog));
    requestLog.setContentCnt(userLogService.selectRequestLogListCnt(requestLog));
    requestLog.setPaginationInfo();
    mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    mav.addObject("searchParams", requestLog);
    return mav;
  }
  @GetMapping("/userLog/inoutLog")
  public ModelAndView inoutLog(@AuthenticationPrincipal UserInfo loginUser, UserInoutLog inoutLog){
    ModelAndView mav = new ModelAndView("adminPage/userLog/inoutLog");
    inoutLog.setDownOrganCdList(loginUser.getDownOrganCdList());
    inoutLog.setQueryInfo();
    mav.addObject("logList", userLogService.selectInoutLogList(inoutLog));
    inoutLog.setContentCnt(userLogService.selectInoutLogListCnt(inoutLog));
    inoutLog.setPaginationInfo();
    mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    mav.addObject("searchParams", inoutLog);
    return mav;
  }
}
