package itn.let.mjo.conf.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import egovframework.rte.fdl.security.userdetails.util.EgovUserDetailsHelper;
import itn.com.cmm.LoginVO;
import itn.com.cmm.util.StringUtil;
import itn.com.utl.fcc.service.EgovStringUtil;
import itn.let.mjo.conf.service.MjonConfService;
import itn.let.mjo.conf.service.MjonConfVO;
import itn.let.mjo.msgagent.service.MjonMsgAgentStsService;
import itn.let.mjo.msgagent.service.MjonMsgAgentStsVO;

@Controller
public class MjonConfController {

	@Resource(name = "mjonConfService")
	private MjonConfService mjonConfService;
	
	@Resource(name = "mjonMsgAgentStsService")
	private MjonMsgAgentStsService mjonMsgAgentStsService;
	

	/**
	* @Method Name : updateMjonConfAjax
	* @작성일 : 2023. 2. 22.
	* @작성자 :  anjooyoung
	* @Method 설명 : 문자온 설정 수정 Ajax
	*/
	@RequestMapping(value = "/web/mjon/conf/selectMjonMsgUseConfAjax.do")
	public ModelAndView selectMjonMsgUseConfAjax(@ModelAttribute MjonConfVO confVO, Model model) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("jsonView");
		
		try {
			
			MjonConfVO vo = mjonConfService.selectConfDetail("CONF01");		//문자발송 사용여부
			
			if(vo == null || "".equals(vo.getConfCode()) || !"Y".equals(vo.getConfCode())) {
				modelAndView.addObject("message", "문자 발송 불가");	
				modelAndView.addObject("result", "fail");
			} else {
				modelAndView.addObject("message", "문자 발송 가능");	
				modelAndView.addObject("result", "success");
			}
		}catch (Exception e) {
			modelAndView.addObject("message", "오류가 발생하였습니다.");
			modelAndView.addObject("result", "fail");
		}
		
		return modelAndView;
	}	
	
	/**
	* @Method Name : updateMjonConfAjax
	* @작성일 : 2023. 2. 22.
	* @작성자 :  anjooyoung
	* @Method 설명 : 문자온 설정 수정 Ajax
	*/
	@RequestMapping(value = "/let/mjo/conf/updateMjonConfAjax.do")
	public ModelAndView updateMjonConfAjax(@ModelAttribute MjonConfVO confVO, Model model) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("jsonView");
		
		try {
			LoginVO loginVO = EgovUserDetailsHelper.isAuthenticated()? (LoginVO)EgovUserDetailsHelper.getAuthenticatedUser():null;
			String userId = loginVO == null ? "" : EgovStringUtil.isNullToString(loginVO.getId());
			if(StringUtil.isEmpty(userId)) {
				modelAndView.addObject("message", "로그인 정보가 없습니다.");
				modelAndView.addObject("result", "fail");
				modelAndView.setViewName("redirect:/uat/uia/EgovLoginUsr.do");
				return modelAndView;
			}
			confVO.setLastUpdusrId(loginVO.getId());
			
			int succCnt = mjonConfService.updateConfCode(confVO);
			
			if(succCnt > 0) {
				modelAndView.addObject("message", "수정 완료.");	
				modelAndView.addObject("result", "success");
			} else {
				modelAndView.addObject("message", "수정 실패.");	
				modelAndView.addObject("result", "fail");
			}
			
		}catch (Exception e) {
			modelAndView.addObject("message", "오류가 발생하였습니다.");
			modelAndView.addObject("result", "fail");
		}
		
		return modelAndView;
	}	
	
	@RequestMapping(value = "/web/mjon/conf/selectBlineSendCheckAjax.do")
	public ModelAndView selectBlineSendCheckAjax(
			@ModelAttribute MjonMsgAgentStsVO mjonMsgAgentStsVO
			, Model model) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("jsonView");
		
		try {
			
			System.out.println(mjonMsgAgentStsVO.getAgentCode());
			if(mjonMsgAgentStsVO.getAgentCode().equals("b1")||mjonMsgAgentStsVO.getAgentCode().equals("B1")) {
				mjonMsgAgentStsVO.setAgentCode("08");
			}else if(mjonMsgAgentStsVO.getAgentCode().equals("b2")||mjonMsgAgentStsVO.getAgentCode().equals("B2")) {
				mjonMsgAgentStsVO.setAgentCode("09");
			}
			
			MjonMsgAgentStsVO vo = mjonMsgAgentStsService.selectMsgAgentBlineYnInfo(mjonMsgAgentStsVO);
			
			if(vo == null || "".equals(vo.getUseYn()) || !"Y".equals(vo.getUseYn())) {
				modelAndView.addObject("message", "문자 발송 불가");	
				modelAndView.addObject("result", "fail");
			} else {
				modelAndView.addObject("message", "문자 발송 가능");	
				modelAndView.addObject("result", "success");
			}
			
		}catch (Exception e) {
			modelAndView.addObject("message", "오류가 발생하였습니다.");
			modelAndView.addObject("result", "fail");
		}
		
		return modelAndView;
	}
}
