/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 *
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.reports.web;

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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ffii.core.utils.CriteriaUtils;
import com.ffii.core.utils.NumberUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.reports.service.OutstandingReportService;

/**
 * @author fung
 */
@Controller
@RequestMapping(value = "/report/outstanding")
public class OutstandingReportController extends AbstractController {

	@Autowired
	private OutstandingReportService outstandingReportService;

	@RequestMapping(value = "/excel", method = { RequestMethod.GET, RequestMethod.POST })
	public String excel(Model model, HttpServletRequest request, HttpServletResponse response)
	 throws ServletRequestBindingException {

		Map<String, Object> args = new HashMap<String, Object>();
		CriteriaUtils.addStringLike(request, args, "custName");
		CriteriaUtils.addStringLike(request, args, "orderNo");
		List<Map<String, Object>> records = outstandingReportService.search(args);

		model.addAttribute(Params.RECORDS, records);

		return "outstandingReportExcelView";
	}

	
	@RequestMapping(value = "/list.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String listJson(Model model, HttpServletRequest request) throws Exception {

		Map<String, Object> args = new HashMap<String, Object>();
		CriteriaUtils.addStringLike(request, args, "custName");
		CriteriaUtils.addStringLike(request, args, "orderNo");

		CriteriaUtils.addInteger(request, args,"start");
		CriteriaUtils.addInteger(request, args,"limit");

		List<Map<String, Object>> records = outstandingReportService.search(args);

		Map<String, Object> count = outstandingReportService.count(args);
		model.addAttribute("total", NumberUtils.intValue(count.get("count")));
		model.addAttribute(Params.RECORDS, records);
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		
		return JsonView.NAME;
	}

}
