/*******************************************************************************
 * 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.OrderItem;
import com.ffii.tbms.order.service.OrderItemService;


@Controller
@RequestMapping(value = "/order/item")
public class OrderItemController extends AbstractController {

	@Autowired
	private OrderItemService orderItemService;

	@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")) {
			OrderItem instance;
			OrderItem oldRecord = new OrderItem();
			if (id > 0) {
				instance = orderItemService.find(id);
				orderItemService.compare(instance, request);
				oldRecord = SerializationUtils.clone(instance);
			} else {
				instance = new OrderItem();
			}

			ServletRequestUtils.doBind(request, instance, null);
			id = orderItemService.saveOrUpdate(instance);
			
			if(oldRecord.getId() != null){
				orderItemService.log(LogUtils.ACTION_UPDATE, id, instance, oldRecord);
			}else{
				orderItemService.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") &&  SecurityUtils.isGranted("ORDER_ITEM_DELETE")) {

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

			orderItemService.log("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") || SecurityUtils.isGranted("WORKSHOP_MAINTAIN")) {
			Map<String, Object> data = orderItemService.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") || SecurityUtils.isGranted("WORKSHOP_MAINTAIN")) {
			Map<String, Object> args = new HashMap<String, Object>();

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

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

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

		return JsonView.NAME;
	}

}
