package info.bukova.isspst.ui.requirement;

import info.bukova.isspst.data.TripRequirement;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.filters.TripRequirementFilter;
import info.bukova.isspst.services.requirement.TripRequirementService;
import info.bukova.isspst.services.workgroups.WorkgroupService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.access.AccessDeniedException;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;

public class TripRequirementListCentre extends RequirementSubpage<TripRequirement> {
	
	@WireVariable
	private TripRequirementService tripRequirementService;

	@WireVariable
	private WorkgroupService workgroupService;

	private List<Workgroup> myCentres;

	@Init
	public void init() {
		service = tripRequirementService;
		dataClass = TripRequirement.class;
		formZul = "requirementsForm.zul";
		dataFilter = new TripRequirementFilter(getFilterTemplate());
		
		myCentres = workgroupService.getUserCentres(userService.getCurrent());
	}

	@Override
	protected List<TripRequirement> getListFromService() {
		try {
			return tripRequirementService.getCentreReq();
		} catch (AccessDeniedException e) {
			BindUtils.postGlobalCommand(null, null, "disableCentre", null);
			return new ArrayList<TripRequirement>();
		}
	}

	public List<Workgroup> getMyCentres() {
		return myCentres;
	}

	@GlobalCommand
	@NotifyChange({ "dataList", "dataBean", "fullFill" })
	public void reloadRelated() {
		this.reload();
	}
	
	@Override
	protected void beforeSelectViaUrl() {
		BindUtils.postGlobalCommand(null, null, "selectCentre", null);
	}

}
