package com.eactive.eai.rms.onl.manage.control.control;

import com.eactive.eai.rms.onl.apim.common.BaseRestController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
public class ControlApiController extends BaseRestController {
    ControlManController controlManController;
    
    @Autowired
    private ControlManService service;

    public ControlApiController(ControlManController controlManController) {
        this.controlManController = controlManController;
    }


    @RequestMapping(value = "/control/controlMan.json", params = "cmd=LIST")
    public ModelAndView selectList(HttpServletRequest request, ControlManUISearch uiSearch) {
        return controlManController.selectList(request, uiSearch);
    }

    @RequestMapping(value = "/control/controlMan.json", params = "cmd=INSERT")
    private String insert(HttpServletRequest request, HttpServletResponse response, ControlManUIDetail uiDetail)
            throws Exception {
	
	if (!"A".equals(uiDetail.getSearchEaiBzwkDstcd())) {

	    Map<String, Object> map = service.selectList(uiDetail);

	    List<ControlManUI> uiList = (List<ControlManUI>) map.get("list");
	    
	    uiDetail.arrEaiSvcName = new ArrayList<String>();
	    
	    uiList.forEach(item -> {
		uiDetail.arrEaiSvcName.add(item.getEaisvcname());

	    });
	}
	return controlManController.insert(request, response, uiDetail);
    }

    @RequestMapping(value = "/control/controlMan.json", params = "cmd=DELETE")
    public String delete(HttpServletRequest request, HttpServletResponse response, ControlManUIDetail uiDetail)
            throws Exception {
	
	if (!"A".equals(uiDetail.getSearchEaiBzwkDstcd())) {

	    Map<String, Object> map = service.selectList(uiDetail);

	    List<ControlManUI> uiList = (List<ControlManUI>) map.get("list");
	    
	    uiDetail.arrEaiSvcName = new ArrayList<String>();
	    
	    uiList.forEach(item -> {
		uiDetail.arrEaiSvcName.add(item.getEaisvcname());

	    });
	}
	
        return controlManController.delete(request, response, uiDetail);
    }
}
