<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%
PortletURL portletURL = renderResponse.createRenderURL();

portletURL.setParameter("struts_action", "/shopping/view");
portletURL.setParameter("tabs1", tabs1);
%>

<aui:form action="<%= portletURL.toString() %>" method="post" name="fm">
	<aui:input name="<%= Constants.CMD %>" type="hidden" />
	<aui:input name="deleteCouponIds" type="hidden" />

	<%
	CouponSearch searchContainer = new CouponSearch(renderRequest, portletURL);

	List headerNames = searchContainer.getHeaderNames();

	headerNames.add(StringPool.BLANK);

	searchContainer.setRowChecker(new RowChecker(renderResponse));
	%>

	<liferay-ui:search-form
		page="/html/portlet/shopping/coupon_search.jsp"
		searchContainer="<%= searchContainer %>"
	/>

	<%
	CouponSearchTerms searchTerms = (CouponSearchTerms)searchContainer.getSearchTerms();

	int total = ShoppingCouponLocalServiceUtil.searchCount(scopeGroupId, company.getCompanyId(), searchTerms.getCode(), searchTerms.isActive(), searchTerms.getDiscountType(), searchTerms.isAndOperator());

	searchContainer.setTotal(total);

	List results = ShoppingCouponServiceUtil.search(scopeGroupId, company.getCompanyId(), searchTerms.getCode(), searchTerms.isActive(), searchTerms.getDiscountType(), searchTerms.isAndOperator(), searchContainer.getStart(), searchContainer.getEnd());

	searchContainer.setResults(results);
	%>

	<div class="separator"><!-- --></div>

	<aui:button-row>
		<aui:button onClick='<%= renderResponse.getNamespace() + "deleteCoupons();" %>' type="button" value="delete" />
	</aui:button-row>

	<%
	List resultRows = searchContainer.getResultRows();

	for (int i = 0; i < results.size(); i++) {
		ShoppingCoupon coupon = (ShoppingCoupon)results.get(i);

		coupon = coupon.toEscapedModel();

		ResultRow row = new ResultRow(coupon, coupon.getCouponId(), i);

		PortletURL rowURL = renderResponse.createRenderURL();

		rowURL.setParameter("struts_action", "/shopping/edit_coupon");
		rowURL.setParameter("redirect", currentURL);
		rowURL.setParameter("couponId", String.valueOf(coupon.getCouponId()));

		// Code

		row.addText(coupon.getCode(), rowURL);

		// Name and description

		if (Validator.isNotNull(coupon.getDescription())) {
			row.addText(coupon.getName().concat("<br />").concat(coupon.getDescription()), rowURL);
		}
		else {
			row.addText(coupon.getName(), rowURL);
		}

		// Start date

		row.addText(dateFormatDateTime.format(coupon.getStartDate()), rowURL);

		// End date

		if (coupon.getEndDate() == null) {
			row.addText(LanguageUtil.get(pageContext, "never"), rowURL);
		}
		else {
			row.addText(dateFormatDateTime.format(coupon.getEndDate()), rowURL);
		}

		// Discount type

		row.addText(LanguageUtil.get(pageContext, coupon.getDiscountType()), rowURL);

		// Action

		row.addJSP("right", SearchEntry.DEFAULT_VALIGN, "/html/portlet/shopping/coupon_action.jsp");

		// Add result row

		resultRows.add(row);
	}
	%>

	<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
</aui:form>

<aui:script>
	Liferay.provide(
		window,
		'<portlet:namespace />deleteCoupons',
		function() {
			if (confirm('<%= UnicodeLanguageUtil.get(pageContext, "are-you-sure-you-want-to-delete-the-selected-coupons") %>')) {
				document.<portlet:namespace />fm.<portlet:namespace /><%= Constants.CMD %>.value = "<%= Constants.DELETE %>";
				document.<portlet:namespace />fm.<portlet:namespace />deleteCouponIds.value = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, "<portlet:namespace />allRowIds");
				submitForm(document.<portlet:namespace />fm, "<portlet:actionURL><portlet:param name="struts_action" value="/shopping/edit_coupon" /><portlet:param name="redirect" value="<%= currentURL %>" /></portlet:actionURL>");
			}
		},
		['liferay-util-list-fields']
	);
</aui:script>