/*******************************************************************************
 * 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.utils.SecurityUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.reports.service.CustomerReportService;
import com.ffii.tbms.reports.service.PaymentReportService;

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

	@Autowired
	private CustomerReportService service;

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

		if (SecurityUtils.isGranted("CUSTOMER_REPORT")) {
			Map<String, Object> args = new HashMap<String, Object>();
			CriteriaUtils.addDate(request, args, "dateFrom");
			CriteriaUtils.addDateTo(request, args, "dateTo");

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

			model.addAttribute(Params.RECORDS, records);
			model.addAttribute("map", args);

			return "customerReportExcelView";
		} else {
			model.addAttribute(Params.AUTH, Boolean.FALSE);
			return JsonView.NAME;
		}
	}

	@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.addDate(request, args, "dateFrom");
		CriteriaUtils.addDateTo(request, args, "dateTo");

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

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

		//Map<String, Object> count = service.count(args);
		//model.addAttribute("total", NumberUtils.intValue(count.get("count")));
		model.addAttribute(Params.RECORDS, records);
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}

}


