package info.bukova.isspst.ui.main.orders.requirements;

import info.bukova.isspst.data.Requirement;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.filters.RequirementFilter;
import info.bukova.isspst.services.requirement.RequirementService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import info.bukova.isspst.ui.requirement.RequirementSubpage;

import java.math.BigDecimal;
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 ReqListMyAll extends RequirementSubpage<Requirement>
{
	@WireVariable
	protected RequirementService requirementService;

	@WireVariable
	protected WorkgroupService workgroupService;

	public List<Workgroup> getCentres()
	{
		return workgroupService.getCentres();
	}

	@Init(superclass = true)
	public void initReqListMyAll()
	{
		service = requirementService;
		dataClass = Requirement.class;
		formZul = "/main/orders/requirements/reqForm.zul";
		dataFilter = new RequirementFilter(getFilterTemplate());
	}

	@Override
	protected List<Requirement> getListFromService()
	{
		try
		{
			return requirementService.getAll();
		}
		catch (AccessDeniedException e)
		{
			BindUtils.postGlobalCommand(null, null, "disableAll", null);
			return new ArrayList<Requirement>();
		}
	}

	@GlobalCommand
	@NotifyChange({ "dataList", "dataBean", "fullFill" })
	public void reloadRelated()
	{
		this.reload();
	}
	
	@Override
	protected void beforeSelectViaUrl() {
		BindUtils.postGlobalCommand(null, null, "selectAll", null);
	}
	
	public BigDecimal getInvoicedAmount() {
		if (getDataBean() != null) {
			return requirementService.getInvoicedAmount(getDataBean());
		} else {
			return null;
		}
	}
	
	@Override
	@NotifyChange({ "dataBean", "ableToDelete", "canApprove", "invoicedAmount" })
	public void setDataBean(Requirement data) {
		super.setDataBean(data);
	}
}
