<%
	var containerId = m.lib.containerId, tuix = m.tuix, lib = m.lib;
	zenario.sendSignal("eventListPreDisplay", {containerId: containerId, microTemplate: "fea_list_blocks", tuix: tuix, lib: lib});
%>


{{m.lib.displayDevTools()}}


<div class="{{m.lib.wrapperClassName()|e}} zfea_blocks">
	<% m._position = 'zfea_at_top'; %>
	
	{{m.tuix.header_html}}
	
	<div class="zfea_title_area">
		{{m.lib.pMicroTemplate('title', m)}}
		
		<% if (!m.tuix.hide_item_count && !m.tuix.hide_item_count_at_top) { %>
			{{m.lib.pMicroTemplate('list_item_count', m)}}
		<% } %>
	</div>
	
	<div class="zfea_back_button">
		<% if (zenarioT.find(m.thus.sortedCollectionButtons, "back")) { %>
			{{m.lib.pMicroTemplate("button", m.thus.sortedCollectionButtons, "back")}}
		<% } %>
	</div>
	<div class="zfea_buttons zfea_top_buttons">
		<% if (zenarioT.find(m.thus.sortedCollectionButtons, "top")) { %>
			{{m.lib.pMicroTemplate("button", m.thus.sortedCollectionButtons, "top")}}
		<% } %>
	</div>
	
	<% var ci, col, chi, child, bi, button, oi, log, logName, gi, lg, lgName, ii, item, tag, showParent, headerId = false, firstItem = true; %>
	
	{{m.lib.pMicroTemplate("list_search_and_sort", m)}}
	
	<% if (m.tuix.item_count_message && m.tuix._cms_itemCount > 1) { %>
		<div class="item_count">
			<p>{{m.tuix.item_count_message.replace("[[count]]", m.tuix._cms_itemCount)|e}}</p> 
		</div>
	<% } %>
	
	{{m.lib.pMicroTemplate('subtitle', m)}}
	
	{{m.tuix.body_html}}
	
	<% if (m.thus.sortedCollectionButtons.length) { %>
		<% if (zenarioT.find(m.thus.sortedCollectionButtons, ["above_list", "column_header", undefined]) && !_.isEmpty(m.tuix.columns)) { %>
			<div class="zfea_buttons zfea_buttons_above_list sorted_buttons">
				{{m.lib.pMicroTemplate("button", m.thus.sortedCollectionButtons, ["above_list", "column_header", undefined])}}
			</div>
		<% } %>
	<% } %>
	
	<% if (!_.isEmpty(m.tuix.columns)) { %>
	
		<% foreach (m.thus.sortedListOuterGroupings as oi => logName) { %>
			<% log = m.tuix.list_outer_groupings && m.tuix.list_outer_groupings[logName]; %>
			
			<% if (log) { %>
				{{m.lib.pMicroTemplate("list_block_outer_grouping", log)}}
			<% } %>
	
			<% foreach (m.thus.sortedListGroupings as gi => lgName) { %>
				<% lg = m.tuix.list_groupings && m.tuix.list_groupings[lgName]; %>
				
				<% if (lg) { %>
					
					<% if (log && logName != lg.list_outer_grouping) { %>
						<% continue; %>
					<% } %>
					
					{{m.lib.pMicroTemplate("list_block_grouping", lg)}}
				<% } %>
		
				<div class="zfea_block_list_wrap">
					<% foreach (m.thus.sortedItems as ii => item) { %>
		
						<% if (lg && lgName != item.list_grouping) { %>
							<% continue; %>
						<% } %>
				
						<% if (m.tuix.has_headers) { %>
							<% if (item.header_id != headerId) { %>
								<% if (headerId !== false) { %> 
									</div></div>
								<% } %>
								<% headerId = item.header_id; %>
								<div class="section">
									<h4>{{item.header_name|e}}</h4>
									<div class="zfea_block_list_wrap_inner">
							<% } %>
						<% } %>
				
						<div class="zfea_block item_{{item.id|e}} {{item.row_class|e}}">
							
							<% if (item.thumbnail) { %>
								<div class="zfea_block_item_thumbnail_wrap">
									<div class="zfea_block_thumbnail_wrap">
										<img
											class="zfea_thumbnail"
											src="{{item.thumbnail.src|e}}"
									
											<% if (item.thumbnail.width) { %>
												width="{{item.thumbnail.width|e}}"
											<% } %>
											<% if (item.thumbnail.height) { %>
												height="{{item.thumbnail.height|e}}"
											<% } %>
										/>
									</div>
									<div class="zfea_block_item_wrap">
							<% } %>
							
							
							
							<% if (item._cms_sortedItemButtons.length && zenarioT.find(item._cms_sortedItemButtons, "top")) { %>
								<div class="buttons block_buttons_top">
									{{m.lib.pMicroTemplate("button", item._cms_sortedItemButtons, "top")}}
								</div>
							<% } %>
							
							<% var sameRow = false; %>
							<% foreach (m.thus.sortedColumns as ci => col) { %>
						
								<% if (!(showParent = m.lib.columnVisibleForItem(col.id, item.id))) { %>
									<% var blockInvis = true; %>
									
									<% if (col.children) { %>
										<% foreach (col.children as chi => child) { %>
											<% if (m.lib.columnVisibleForItem(child.id, item.id)) { %>
												<% blockInvis = false; %>
											<% } %>
										<% } %>
									<% } %>
									
									<% if (blockInvis) { %>
										<% continue; %>
									<% } %>
								<% } %>
						
								<% if (m.thus.sortedColumns[+ci + 1] && m.thus.sortedColumns[+ci + 1].same_row && !sameRow) { %>
									<% sameRow = true; %>
									<div class="wrap_div">
								<% } %>
						
								<% tag = col.tag ? col.tag : "p"; %>
								<div
									id="{{('zfea_' + m.lib.containerId + '_row_' + ii + '_col_' + ci)|e}}"
									data-label="{{col.title|e}}" 
									class="
										block_view_box
										column_{{col.id|e}} 
										{{col.css_class|e}}
										<% if (ci == 0) { %>
											zfea_first_col
										<% } %>
										<% if (item.cell_css_classes && item.cell_css_classes[col.id]) { %>
											{{item.cell_css_classes[col.id]|e}}
										<% } %>
									"
								>
									<div class="icon"></div>
									
									<div class="
										label
										<% if (col._is_block) { %>
											<% if (col._is_metric) { %>
												metric
											<% } else { %>
												real_time
											<% } %>
										<% } %>
									">
									    <% if (col.show_title_in_block_view && col.title) { %>
   											<div class="field_label resize_text">{{col.title|e}}</div>
										<% } %>
										
										<% if (zenario.defined(col.previous_label) && zenario.defined(item["prev__" + col.id])) { %>
											<div class="block_boxes period_boxes">
												<div class="box_wrapper this_wrapper">
													<div class="box_label">
														{{col.current_label|e}}
													</div>
													<div class="resize_text">
														{{item[col.id]|e}}
													</div>
												</div>
												<div class="box_wrapper previous_wrapper">
													<div class="box_label">
														{{col.previous_label|e}}
													</div>
													<div class="resize_text">
														{{item["prev__" + col.id]|e}}
													</div>
												</div>
											</div>
											

										<% } else if (col.children) { %>
											<div class="block_boxes parent_child_boxes">
												<% if (showParent && zenario.defined(item[col.id])) { %>
													<div class="box_wrapper parent_wrapper">
														<div class="resize_text">
															{{m.lib.pMicroTemplate("list_cell", {col: col, item: item})}}
														</div>
													</div>
												<% } %>
												
												<% foreach (col.children as chi => child) { %>
													<% if (m.lib.columnVisibleForItem(child.id, item.id)) { %>
														<div class="box_wrapper child_wrapper child_wrapper_{{child.id|e}} {{child.css_class|e}}">
															<div class="resize_text">
																{{m.lib.pMicroTemplate('list_cell', {col: child, item: item})}}
															</div>
														</div>
													<% } %>
												<% } %>
											</div>
											
										<% } else { %>
											<div class="field_value resize_text">
												{{m.lib.pMicroTemplate("list_cell", {col: col, item: item})}}
											</div>
										<% } %>
										
										<% if (col.cell_tooltip) { %>
										    <i class="info_icon" title="{{col.cell_tooltip|e}}"></i>
										<% } %>
									</div>
								</div>
						
								<% if (col.same_row && sameRow && (!m.thus.sortedColumns[+ci + 1] || !m.thus.sortedColumns[+ci + 1].same_row)) { %>
									<% sameRow = false; %>
									</div>
								<% } %>
							<% } %>
							
							<% if (item._cms_sortedItemButtons.length && zenarioT.find(item._cms_sortedItemButtons, ["bottom", undefined])) { %>
								<div class="buttons block_buttons_bottom">
									{{m.lib.pMicroTemplate("button", item._cms_sortedItemButtons, ["bottom", undefined])}}
								</div>
							<% } %>
							
							
							<% if (item.thumbnail) { %>
									</div>
								</div>
							<% } %>
						</div>
				
					<% } %>
			
					<%
						if (!zenarioL.mobile && m.lib.mode != "view_data_pool_block") {
							m.lib.after(function() {
								var maxHeight = 0;
								$("#" + m.lib.containerId + " .zfea_block_list_wrap .zfea_block")
									.each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
									.height(maxHeight);
							});
						}
					%>
					<% if (m.thus.sortedItems.length == 0) { %>
						<% var msg; %>
						<% if (lib.request.search && (msg = m.tuix.no_items_in_search_message)) { %>
							<div class="zfea_no_items_in_search_message">{{msg}}</div>
						
						<% } else if (msg = m.tuix.no_items_message) { %>
							<div class="zfea_no_items_message">{{msg}}</div>
						<% } %>
					<% } %>
				</div>
						
				<% if (lg) { %>
					{{m.lib.pMicroTemplate("list_block_grouping_end", lg)}}
				<% } %>
			<% } %>
			
			<% if (log) { %>
				{{m.lib.pMicroTemplate("list_block_outer_grouping_end", log)}}
			<% } %>
		<% } %>
	<% } %>
	<% m._position = 'zfea_at_bottom'; %>
	
	<% if (!m.tuix.hide_item_count && !m.tuix.hide_item_count_at_bottom && m.tuix._cms_itemCount >= 10) { %>
		{{m.lib.pMicroTemplate('list_item_count', m)}}
	<% } %>
	<% if (!m.tuix.hide_pagination) { %>
		{{m.lib.pMicroTemplate('list_pagination', m)}}
	<% } %>
	
	<% if (zenarioT.find(m.thus.sortedCollectionButtons, "bottom")) { %>
		<div class="zfea_buttons zfea_bottom_buttons">
			{{m.lib.pMicroTemplate("button", m.thus.sortedCollectionButtons, "bottom")}}
		</div>
	<% } %>
	
	{{m.tuix.footer_html}}
</div>


<%
	m.lib.after(function() {
		zenario.sendSignal("eventListPostDisplay", {containerId: containerId, microTemplate: "fea_list_blocks", tuix: tuix, lib: lib});
	});
%>
