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

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.Criteria;
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.AuditLogService;

@Controller
@RequestMapping(value = "/common/log")
public class AuditLogController extends AbstractController {

	@Autowired
	private AuditLogService service;

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

		if (SecurityUtils.isGranted("SUPERUSER")) {
			Map<String, Object> args = new HashMap<>();
			CriteriaUtils.addInteger(request, args, "orderId");
			CriteriaUtils.addStringExact(request, args, "action");

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

			model.addAttribute(Params.RECORDS, service.searchOrderLog(args));
			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.MSG, "No Auth");
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}
		return JsonView.NAME;
	}
}
