package com.rms.tfc.web;

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

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

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

import com.rms.tfc.mapper.MapperBBDD;
import com.rms.tfc.service.LineaBean;
import com.rms.tfc.service.PedidoService;

@Controller
public class SaveRecPedidoController {

	@Resource
	private PedidoService pedidoService;
	
	@RequestMapping("/SaveRecPedido.html")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long idPedido = new Long(request.getParameter("idPedido"));
		int numLineas =  Integer.parseInt((String)request.getParameter("numLineas"));
		List<LineaBean> listaLineas = new ArrayList<LineaBean>();
		Long idLinea = null;
		Long cantidadRecibida = null;
		Long cantidadDevuelta = null;
		LineaBean lineaBean = null;
		for (int i = 1; i <= numLineas; i++) {
			idLinea = new Long(request.getParameter("idLinea_" + i));
			cantidadRecibida = new Long(request.getParameter("recibida_" + i));
			cantidadDevuelta = new Long(request.getParameter("devuelta_" + i));
			lineaBean = MapperBBDD.getLineaBean(idLinea, cantidadRecibida, cantidadDevuelta);
			listaLineas.add(lineaBean);
		}
		pedidoService.recibePedido(idPedido, listaLineas);
		return new ModelAndView("admin");
    }

	public void setPedidoService(PedidoService pedidoService) {
		this.pedidoService = pedidoService;
	}
}