/*******************************************************************************
 * 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.log.web;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.utils.SecurityUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.log.service.ScheduledTaskLogService;

@Controller
@RequestMapping(value = "/admin/scheduled-task-log")
public class ScheduledTaskLogController extends AbstractController {

	@Autowired
	private ScheduledTaskLogService scheduledTaskLogService;

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

		if (SecurityUtils.isGranted("ADMIN")) {
			Map<String, Object> args = new HashMap<String, Object>();
			CriteriaUtils.addString(request, args, "module");
			CriteriaUtils.addStringLike(request, args, "task");
			CriteriaUtils.addDate(request, args, "dateFrom");
			CriteriaUtils.addDateTo(request, args, "dateTo");
			CriteriaUtils.addString(request, args, "result");

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

			List<Map<String, Object>> records = scheduledTaskLogService.search(args);
			Map<String, Object> countData = scheduledTaskLogService.searchTotalCount(args);

			model.addAttribute(Params.RECORDS, records);
			model.addAttribute("total", countData.get("count"));
			model.addAttribute(Params.SUCCESS, records != null);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

}
