package info.bukova.isspst.ui.tripbill;

import info.bukova.isspst.data.TripBill;
import info.bukova.isspst.data.TripBillApproval;
import info.bukova.isspst.data.TripRequirement;
import info.bukova.isspst.data.User;
import info.bukova.isspst.filters.TripBillApprovalFilter;
import info.bukova.isspst.services.tripbill.TripBillApprovalService;
import info.bukova.isspst.services.tripbill.TripBillService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.ui.renderers.TripBillApprovalItemRenderer;
import info.bukova.isspst.ui.requirement.RequirementSubpage;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window;

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

/**
 * @author Pepa Rokos
 */
public class TripBillListBase extends RequirementSubpage<TripBillApproval> {

	@WireVariable
	private TripBillApprovalService tripBillApprovalService;
	private TripBillApprovalItemRenderer itemRenderer;
	@WireVariable
	private TripBillService tripBillService;
	@WireVariable
	private UserService userService;
	private List<User> allUsers;

	@Init(superclass = true)
	public void initListBase() {
		service = tripBillApprovalService;
		itemRenderer = new TripBillApprovalItemRenderer();
		dataClass = TripBillApproval.class;
		TripBillApproval filter = getFilterTemplate();
		TripBill tb = new TripBill();
		tb.setRequirement(new TripRequirement());
		filter.setBill(tb);
		dataFilter = new TripBillApprovalFilter(filter);
		allUsers = userService.getUsersForCombo();
	}

	public TripBillApprovalItemRenderer getItemRenderer() {
		return itemRenderer;
	}

	@Command
	public void pay() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("bill", getDataBean().getBill());
		params.put("grid", this);
		Window payDialog = (Window) Executions.createComponents("payDialog.zul", null, params);
		payDialog.doModal();
	}

	@Command
	@Override
	public void edit() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("selected", getDataBean().getBill());
		tripBillService.loadLazyData(getDataBean().getBill());
		Window win = (Window) Executions.createComponents("../requirements/tripBill.zul", null, params);
		win.doModal();
	}

	@Override
	public List<User> getAllUsers() {
		return allUsers;
	}

	@GlobalCommand
	@NotifyChange({ "dataList", "dataBean", "fullFill", "selIndex" })
	public void reloadRelated()	{
		this.reload();
		restoreUserView();
	}
}
