package com.eactive.eai.rms.onl.manage.serviceinfo.stddefault;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.onl.manage.serviceinfo.stdmessage.ExtendedColumnDefinitionUI;

@Controller
public class StdDefaultManController extends BaseController {

	@Autowired
	private StdDefaultManService stdDefaultManService;

	@Autowired
	private ComboService comboService;

	@GetMapping(value = "/onl/admin/service/stdDefaultMan.view")
	public String view() {
		return "/onl/admin/service/stdDefaultMan";
	}

	@GetMapping(value = "/onl/admin/service/stdDefaultMan.view", params = "cmd=DETAIL")
	public String viewDetail() {
		return "/onl/admin/service/stdDefaultManDetail";
	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<ExtendedColumnDefinitionUI>> selectList(Pageable pageable,
			String searchColumnName, String searchColumnDesc) {

		Page<ExtendedColumnDefinitionUI> page = stdDefaultManService
				.selectList(pageable, searchColumnName, searchColumnDesc);

		return ResponseEntity.ok(new GridResponse<>(page));

	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.json", params = "cmd=LIST_DETAIL_COMBO")
	public ResponseEntity<Map<String, Object>> selectDetailCombo() {
		List<ComboVo> listYn = comboService.getFromCode("YN");

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("ynList", listYn);

		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.json", params = "cmd=DETAIL")
	public ResponseEntity<ExtendedColumnDefinitionUI> selectDetail(String columnName) {
		ExtendedColumnDefinitionUI extendedColumnDefinitionUI = stdDefaultManService.selectDetail(columnName);
		return ResponseEntity.ok(extendedColumnDefinitionUI);
	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insert(ExtendedColumnDefinitionUI extendedColumnDefinitionUI) {

		stdDefaultManService.insert(extendedColumnDefinitionUI);

		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.json", params = "cmd=UPDATE")
	public ResponseEntity<Void> update(ExtendedColumnDefinitionUI extendedColumnDefinitionUI) {
		stdDefaultManService.update(extendedColumnDefinitionUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(String columnName) {
		stdDefaultManService.delete(columnName);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/service/stdDefaultMan.excel", params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(String searchColumnName, String searchColumnDesc) {

		List<Map<String, Object>> list = stdDefaultManService.selectListToExcel(searchColumnName, searchColumnDesc);

		Map<String, Object> resultMap = new HashMap<>();
		
		resultMap.put("datas", list);
		return new ModelAndView("excelView", resultMap);
	}

}
