<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
	
	<var name="searchCriteria" class="org.springframework.webflow.samples.booking.SearchCriteria" />
	
	<view-state id="enterSearchCriteria">
		<on-render>
			<evaluate expression="bookingService.findBookings(currentUser?.name)" result="viewScope.bookings" result-type="dataModel" />
		</on-render>
		<transition on="search" to="reviewHotels">
			<evaluate expression="searchCriteria.resetPage()" />
		</transition>
		<transition on="cancelBooking">
			<evaluate expression="bookingService.cancelBooking(bookings.selectedRow)" />
			<render fragments="bookingsFragment" />
		</transition>
	</view-state>
	
	<view-state id="reviewHotels">
		<on-render>
			<evaluate expression="bookingService.findHotels(searchCriteria)" result="viewScope.hotels" result-type="dataModel" />
		</on-render>
        <transition on="sort">
            <set name="searchCriteria.sortBy" value="requestParameters.sortBy" />
            <render fragments="hotels:searchResultsFragment" />
        </transition>
		<transition on="previous">
			<evaluate expression="searchCriteria.previousPage()" />
			<render fragments="hotels:searchResultsFragment" />
		</transition>
		<transition on="next">
			<evaluate expression="searchCriteria.nextPage()" />
			<render fragments="hotels:searchResultsFragment" />
		</transition>
		<transition on="select" to="reviewHotel">
			<set name="flowScope.hotel" value="hotels.selectedRow" />
		</transition>
		<transition on="changeSearch" to="changeSearchCriteria" />
	</view-state>
	
	<view-state id="reviewHotel">
		<transition on="book" to="bookHotel" />
		<transition on="cancel" to="enterSearchCriteria" />
	</view-state>

	<subflow-state id="bookHotel" subflow="booking">
		<input name="hotelId" value="hotel.id" />
		<transition on="bookingConfirmed" to="finish" />
		<transition on="bookingCancelled" to="enterSearchCriteria" />
	</subflow-state>

	<view-state id="changeSearchCriteria" view="enterSearchCriteria.xhtml" popup="true">
		<on-entry>
			<render fragments="hotelSearchFragment" />
		</on-entry>
		<transition on="search" to="reviewHotels">
			<evaluate expression="searchCriteria.resetPage()"/>
		</transition>
	</view-state>
			
	<end-state id="finish" />
	
</flow>