/*******************************************************************************
 * 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.Params;
import com.ffii.core.web.AbstractController;
import com.ffii.tbms.reports.service.AuditLogExportService;

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

	@Autowired
	private AuditLogExportService auditLogExportService;

	@RequestMapping(value = "/audit-log-export/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.addDate(request, args, "dateFrom");
		CriteriaUtils.addDateTo(request, args, "dateTo");
		CriteriaUtils.addString(request, args, "tableName");

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

		if (args.containsKey("dateFrom")) model.addAttribute("dateFrom", args.get("dateFrom"));
		if (args.containsKey("dateTo")) model.addAttribute("dateTo", args.get("dateTo"));
		if (args.containsKey("tableName")) model.addAttribute("tableName", args.get("tableName"));

		model.addAttribute(Params.RECORDS, records);

		return "auditLogExportExcelView";
	}
}
