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 SavePedidoController {

	@Resource
	private PedidoService pedidoService;
	
	@RequestMapping("/SavePedido.html")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String descripcionPedido = request.getParameter("descripcionPedido");
		int numLineas =  Integer.parseInt((String)request.getParameter("numLineas"));
		List<LineaBean> listaLineas = new ArrayList<LineaBean>();
		Long idProducto = null;
		Long cantidadPedida = null;
		LineaBean lineaBean = null;
		for (int i = 1; i <= numLineas; i++) {
			idProducto = new Long(request.getParameter("idProducto_" + i));
			cantidadPedida = new Long(request.getParameter("cantidadPedida_" + i));
			lineaBean = MapperBBDD.getLineaBean(idProducto, cantidadPedida);
			listaLineas.add(lineaBean);
		}
		pedidoService.creaNewPedido(descripcionPedido, listaLineas);
		return new ModelAndView("admin");
    }

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