package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.StrukBuktiPengeluaranService;
import com.jasamedika.medifirst2000.vo.StrukPengeluaranVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/struk-pengeluaran")
public class StrukBuktiPengeluaranController extends LocaleController<StrukPengeluaranVO> {

	@Autowired
	private StrukBuktiPengeluaranService strukBuktiPengeluaranService;

	@RequestMapping(value = "/pengeluaran-umum", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> simpanPengeluaranUmum(@RequestBody StrukPengeluaranVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = strukBuktiPengeluaranService.simpanPengeluaranUmum(vo);
			if (result != null)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException se) {
			addHeaderMessage(ERROR_MESSAGE, se.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT);
		}
	}

	@RequestMapping(value = "/get-struk-bukti-pengeluaran", method = GET, produces = APPLICATION_JSON_VALUE)
	public List<Map<String, Object>> getDaftarTagihanRekananVerifikasi(
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate,
			@RequestParam(value = "ketLain", required = false) String ketLain,
			@RequestParam(value = "idPegawai", required = false) Integer idPegawai,
			@RequestParam(value = "noSBK", required = false) String noSBK,
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan) {
        return strukBuktiPengeluaranService.getStrukBuktiPengeluaran(startDate,
                endDate, ketLain, idPegawai, noSBK, idRuangan);
	}

}
