/*******************************************************************************
 * Copyright 2014 2Fi Business Solutions Ltd.
 *
 * This code is part of the PCS project.
 *
 * 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.order.web;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.SerializationUtils;
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 org.springframework.web.bind.annotation.RequestParam;

import com.ffii.core.utils.CriteriaUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.utils.web.ServletRequestUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.log.LogUtils;
import com.ffii.tbms.order.Payment;
import com.ffii.tbms.order.service.PaymentService;


@Controller
@RequestMapping(value = "/order/payment")
public class PaymentController extends AbstractController {

	@Autowired
	private PaymentService paymentService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Model model, HttpServletRequest request, @RequestParam(defaultValue = "-1") Integer id) throws Exception {

		if (SecurityUtils.isGranted("ORDER_MAINTAIN")) {
			Payment instance;
			Payment oldRecord = new Payment();
			if (id > 0) {
				instance = paymentService.find(id);
				oldRecord = SerializationUtils.clone(instance);
			} else {
				instance = new Payment();
			}

			ServletRequestUtils.doBind(request, instance, null);
			id = paymentService.saveOrUpdate(instance);

			if(oldRecord.getId()!= null){
				paymentService.log(LogUtils.ACTION_UPDATE, id, instance, oldRecord);
			}else{
				paymentService.log(LogUtils.ACTION_CREATE, id, instance);
			}
			
			model.addAttribute(Params.ID, id);

			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request, @RequestParam Integer id) throws Exception {

		if (SecurityUtils.isGranted("ORDER_MAINTAIN")) {

			// find, mark delete, and save
			Payment instance = paymentService.find(id);
			instance.setDeleted(true);
			paymentService.saveOrUpdate(instance);

			paymentService.log(LogUtils.ACTION_DELETE, id, instance);

			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

	@RequestMapping(value = "/load", method = { RequestMethod.GET, RequestMethod.POST })
	public String load(Model model, HttpServletRequest request, @RequestParam Integer id) throws Exception {

		if (SecurityUtils.isGranted("ORDER_MAINTAIN")) {
			Map<String, Object> data = paymentService.queryForMap(id);

			model.addAttribute(Params.DATA, data);
			model.addAttribute(Params.SUCCESS, data != null);
		} else {
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

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

		if (SecurityUtils.isGranted("ORDER_MAINTAIN")) {
			Map<String, Object> args = new HashMap<String, Object>();

			CriteriaUtils.addInteger(request, args, "orderId");

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

			model.addAttribute(Params.RECORDS, records);
		} else {
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

}
