package com.dbnt.faisp.kwms;

import com.dbnt.faisp.kwms.model.VEmployee;
import com.dbnt.faisp.kwms.service.KwmsService;
import com.dbnt.faisp.main.codeMgt.service.CodeMgtService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.main.userInfo.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequiredArgsConstructor
@RequestMapping("/kwms")
public class KwmsController {
  private final KwmsService kwmsService;
  private final UserInfoService userInfoService;
  private final CodeMgtService codeMgtService;

  @GetMapping("/getEmpInfoToJoinForm")
  public ModelAndView getEmpInfo(VEmployee empInfo){
    ModelAndView mav = new ModelAndView("login/joinForm");
    if(userInfoService.selectUserInfoToDicCode(empInfo.getDicCode()) == null){
      UserInfo userInfo = kwmsService.selectEmpInfo(empInfo.getDicCode());
      if(userInfo==null){
        mav.addObject("joinFlag", "F");
        mav.addObject("userInfo", empInfo);
        mav.addObject("msg", "검색 결과가 없습니다.");
      }else{
        mav.addObject("joinFlag", "T");
        mav.addObject("userInfo", userInfo);
        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", ""));
      }
    }else{
      mav.addObject("joinFlag", "F");
      mav.addObject("userInfo", empInfo);
      mav.addObject("msg", "이미 가입된 식별번호입니다.");
    }
    return mav;
  }

  @GetMapping("/updateUserInfoToKwms")
  public ModelAndView updateUserInfoToKwms(VEmployee empInfo){
    ModelAndView mav = new ModelAndView("login/joinForm");
    if(userInfoService.selectUserInfoToDicCode(empInfo.getDicCode()) == null){
      UserInfo userInfo = kwmsService.selectEmpInfo(empInfo.getDicCode());
      if(userInfo==null){
        mav.addObject("joinFlag", "F");
        mav.addObject("userInfo", empInfo);
        mav.addObject("msg", "검색 결과가 없습니다.");
      }else{
        mav.addObject("joinFlag", "T");
        mav.addObject("userInfo", userInfo);
        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", ""));
      }
    }else{
      mav.addObject("joinFlag", "F");
      mav.addObject("userInfo", empInfo);
      mav.addObject("msg", "이미 가입된 식별번호입니다.");
    }
    return mav;
  }

  @GetMapping("/kwmsCareerModal")
  public ModelAndView kwmsCareerModal(Integer userSeq, String dicCode){
    ModelAndView mav = new ModelAndView("faisp/career/kwmsCareerModal");
    mav.addObject("crcList", codeMgtService.selectCodeMgtList("CRC", ""));
    mav.addObject("careerList", kwmsService.selectUserCareerList(userSeq, dicCode));
    return mav;
  }
}
