package com.eactive.eai.rms.onl.apim.approvalline;

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.SortDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.eactive.eai.rms.common.acl.user.UserManService;
import com.eactive.eai.rms.common.acl.user.ui.UserUI;
import com.eactive.eai.rms.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.vo.GridResponse;

import lombok.RequiredArgsConstructor;

@Controller
@RequiredArgsConstructor
public class PortalApprovalLineManController extends BaseAnnotationController {

	private final PortalApprovalLineManService portalApprovalLineManService;
	private final UserManService userManService;
	private final ComboService comboService;
	
	@GetMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.view", params = "cmd=DETAIL")
	public String detailNewView() {
		return "/onl/apim/approvalline/portalApprovalLineManPopup";
	}

	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<PortalApprovalLineUI>> selectList(@SortDefault("createdDate") Pageable pageable) {
		Page<PortalApprovalLineUI> page = portalApprovalLineManService.selectList(pageable);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=DETAIL")
	public ResponseEntity<PortalApprovalLineUI> selectDetail(String id) {
		PortalApprovalLineUI portalUserUI = portalApprovalLineManService.selectDetail(id);
		return ResponseEntity.ok(portalUserUI);
	}

	public Map<String, Object> getCommonComboData() {
		List<ComboVo> approvalTypeList = comboService.getFromApprovalType();	// 승인타입코드
		List<ComboVo> portalOrgList = comboService.getFromPortalOrgName();		// 기관코드
		
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("approvalTypeRows", approvalTypeList);
		resultMap.put("orgRows", portalOrgList);
		return resultMap;
	}
	
	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> listInitCombo() {
		Map<String, Object> resultMap = getCommonComboData();
		return ResponseEntity.ok(resultMap);
	}
	
	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo() {
		Map<String, Object> resultMap = getCommonComboData();
		resultMap.put("usedApprovalTypes", portalApprovalLineManService.getUsedApprovalTypes());
		return ResponseEntity.ok(resultMap);
	}
	
	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=SEARCH_USER")
	public ResponseEntity<GridResponse<UserUI>> findUser (@SortDefault("userid") Pageable pageable, String username) {
		 Page<UserUI> page = userManService.findUserList(pageable, username);
		 return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=INSERT")
	public ResponseEntity<PortalApprovalLineUI> insert(PortalApprovalLineUI portalApprovalLineUI) {
		portalApprovalLineManService.insert(portalApprovalLineUI);
		return ResponseEntity.ok().build();
	}
	
	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=UPDATE")
	public ResponseEntity<PortalApprovalLineUI> upate(PortalApprovalLineUI portalApprovalLineUI) {
		portalApprovalLineManService.update(portalApprovalLineUI);
		return ResponseEntity.ok().build();
	}
	
	@PostMapping(value = "/onl/apim/approvalline/portalApprovalLineMan.json", params = "cmd=DELETE")
	public ResponseEntity<String> delete(String id) {
		portalApprovalLineManService.delete(id);
		return ResponseEntity.ok().build();
	}
	
}
