<vn>
<vn-lot-query id="params">
	<vn-spec name="id" type="Number"/>
</vn-lot-query>
<vn-group>
	<vn-lot id="lot" on-change="this.onAddressChange()"/>
	<db-form id="defaults" on-ready="onValuesReady">
		<db-model property="model">
			SELECT deliveryMethod, agencyModeFk, addressFk, defaultAgencyFk
				FROM myBasketDefaults
		</db-model>
	</db-form>
	<db-form id="order-form" on-ready="onValuesReady">
		<db-model property="model" lot="params">
			SELECT m.code deliveryMethod, o.sent, o.agencyModeFk, o.addressFk
				FROM myOrder o
					JOIN vn.deliveryMethod m ON m.id = o.deliveryMethodFk
				WHERE o.id = #id
		</db-model>
	</db-form>
	<db-model id="agencies"
		auto-load="false"
		lot="lot"
		result-index="1"
		on-status-changed="onAgenciesReady">
		CALL vn.zone_getAgency(#address, #date);
		SELECT DISTINCT a.agencyModeFk id, a.description	
			FROM tmp.zoneGetAgency a
				JOIN vn.deliveryMethod d
					ON d.id = a.deliveryMethodFk
			WHERE d.code IN ('AGENCY', 'DELIVERY')
				AND a.isVisible
			ORDER BY a.description;
		DROP TEMPORARY TABLE tmp.zoneGetAgency;
	</db-model>
	<db-model id="warehouses"
		auto-load="false"
		lot="lot"
		result-index="1"
		on-status-changed="onWarehousesReady">
		CALL vn.zone_getAgency(#address, #date);
		SELECT DISTINCT a.agencyModeFk id, a.description	
			FROM tmp.zoneGetAgency a
				JOIN vn.deliveryMethod d
					ON d.id = a.deliveryMethodFk
			WHERE d.code IN ('PICKUP')
				AND a.isVisible
			ORDER BY a.description;
		DROP TEMPORARY TABLE tmp.zoneGetAgency;
	</db-model>
</vn-group>
<div id="title">
	<h1><t>ConfigureOrder</t></h1>
</div>
<div id="actions">
	<htk-bar-button
		icon="close"
		tip="_Cancel"
		on-click="onCancelClick"/>
</div>
<div id="form" class="hedera-checkout">
	<div class="vn-w-sm">
		<div class="box bar">
			<htk-assistant-bar
				id="assistant-bar"
				assistant="assistant"/>
		</div>
		<div class="box vn-px-md vn-py-xl">
			<div class="form">
			<htk-assistant id="assistant"
				endFunc="onConfirmClick">
				<htk-step
					name="method"
					validate-func="methodValidate">
					<h2><t>DeliveryOrPickupQuestion</t></h2>
					<div class="answers radio">
						<htk-radio-group
							id="rg-method"
							form="lot"
							column="method"
							on-changed="onMethodChange"/>
						<div>
							<label>
								<htk-radio radio-group="rg-method" value="AGENCY"/>
								<t>Receive the order</t>
							</label>
						</div>
						<div>
							<label>
								<htk-radio radio-group="rg-method" value="PICKUP"/>
								<t>PickupInStore</t>
							</label>
						</div>
					</div>
				</htk-step>
				<htk-step
					name="date"
					show-func="dateShow"
					validate-func="dateValidate">
					<h2 id="date-question"/>
					<div class="answers">
						<htk-calendar
							id="calendar"
							class="thin-calendar"
							form="lot"
							column="date"
							restrict-func="calendarRestrict"
							on-changed="onFieldChange"/>
					</div>
				</htk-step>
				<htk-step
					name="address"
					show-func="addressShow"
					validate-func="addressValidate">
					<h2 id="address-question"/>
					<div class="answers target">
						<db-form id="address-form" model="addresses"/>
						<htk-repeater
							id="repeater"
							form-id="iter"
							on-change="onAddressChange">
							<db-model property="model" id="addresses">
								SELECT a.id, a.nickname, p.name province, a.city, a.street, a.isActive, c.country
									FROM myAddress a
										LEFT JOIN vn.province p ON p.id = a.provinceFk
										JOIN vn.country c ON c.id = p.countryFk
									WHERE a.isActive
							</db-model>
							<custom>
								<div
									class="address"
									on-click="this.onAddressClick(iter.id)"
									id="address">
									<p class="consignee">{{iter.nickname}}</p>
									<p>{{iter.street}}</p>
								</div>
							</custom>
						</htk-repeater>
					</div>
				</htk-step>
				<htk-step
					name="agency"
					show-func="agencyShow"
					validate-func="agencyValidate">
					<h2><t>AgencyQuestion</t></h2>
					<div class="answers target">
						<htk-combo
							id="agency-combo"
							form="lot"
							column="agency"
							on-changed="onFieldChange"
							model="agencies"/>
					</div>
				</htk-step>
				<htk-step
					name="pickup"
					show-func="pickupShow"
					validate-func="pickupValidate">
					<h2><t>PickupWarehouseQuestion</t></h2>
					<div class="answers target">
						<htk-combo
							id="warehouse-combo"
							form="lot"
							column="agency"
							on-changed="onFieldChange"
							model="warehouses"/>
					</div>
				</htk-step>
				<htk-step
					name="confirm-delivery">
					<h2><t>ConfirmData</t></h2>
					<div class="answers target">
						<p>
							<t>Arrival</t> 
							<htk-text format="%D" form="lot" column="date"/>
						</p>
						<p>
							<htk-text form="address-form" column="street"/>
						</p>
						<p>
							<t>Agency</t> 
							<htk-text form="agency-combo" column="description"/>
						</p>
						<div class="clear"/>
					</div>
				</htk-step>
				<htk-step
					name="confirm-pickup">
					<h2><t>ConfirmData</t></h2>
					<div class="answers target">
						<p>
							<t>Pickup</t> 
							<htk-text format="%D" form="lot" column="date"/>
						</p>
						<p>
							<t>Warehouse</t> 
							<htk-text form="warehouse-combo" column="description"/>
						</p>
						<div class="clear"/>
					</div>
				</htk-step>
			</htk-assistant>
			</div>
		</div>
	</div>
</div>
</vn>
