package kcg.imis.member.web;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import kcg.imis.cmmn.service.CodeService;
import kcg.imis.member.service.MemberService;
import kcg.imis.member.vo.MemberSearchVO;
import kcg.imis.member.vo.MemberVO;
import kcg.imis.sec.LoginUserVO;
import kcg.imis.sec.UserUtil;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import egovframework.rte.fdl.property.EgovPropertyService;

/**
  * @FileName : MemberController.java
  * @Project : 국제해양프로젝트
  * @Date : 2018. 4. 9. 
  * @작성자 : Moon
  * @변경이력 : 
  * @프로그램 설명 :
 */
@Controller
@RequestMapping("/member")
public class MemberController {

	@Resource(name = "memberService")
	MemberService memberService;

	@Resource(name = "propertiesService")
	private EgovPropertyService propertiesService;

	/**
	 * 코드관련 정보 인터페이스.
	 */
	@Resource(name = "codeService")
	private CodeService codeService;

	
	@RequestMapping("/update.do")
	public String memberUpdateView(final Model model) throws Exception {
		try {
			
			LoginUserVO user = UserUtil.getMemberInfo();
			model.addAttribute("code", codeService.getCodeList(null));
			model.addAttribute("memberVO", memberService.getMemberInfo(user.getUserid()));
			model.addAttribute("isAdmin", UserUtil.isAdmin());
			model.addAttribute("isSysAdmin", UserUtil.isSysAdmin());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "member/update.tiles";
	}
	
	@RequestMapping(value = {"/memberUpdate.do"}, method = RequestMethod.POST)
	public String memberUpdate(@ModelAttribute("memberVO") final MemberVO memberVO,
			final BindingResult bindingResult, final Model model, final SessionStatus status) throws Exception {
		
		try {
			memberService.updateUserInfo(memberVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/main.do";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/getMemberListAll.json")
	public List<MemberVO>  getMemberListAll(final MemberSearchVO searchVO) throws Exception {
		return memberService.getMemberListAll(searchVO);
	}
	
	@ResponseBody
	@RequestMapping("/regist.json")
	public  HashMap<String, Object> regist2(final MemberVO memberVO) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int result = memberService.insertUserInfo(memberVO);
			resultMap.put("result", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("/checkId.json")
	public  HashMap<String, Object> checkId(final MemberVO memberVO) throws Exception {
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		try {
			int result = memberService.selectCheckId(memberVO.getUserid());
			resultMap.put("cnt", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	
}
