{block content}
	<div class="span12">
		<div class="box-content">
			<div class="filter">
				{form filterForm, class => 'form-inline', style => 'float: left; width: 100%;'}
					<i class="icon-search fs20"></i>&nbsp;{input search, class => 'input-large'}&nbsp;&nbsp;&nbsp;&nbsp;
					{input show_critical}&nbsp;&nbsp;&nbsp;&nbsp;
					{input show_warn}&nbsp;&nbsp;&nbsp;&nbsp;
					{input show_over}&nbsp;&nbsp;&nbsp;&nbsp;
					{input id_stock_item_category}&nbsp;&nbsp;&nbsp;&nbsp;
					{input submit}
					{input reset}
					<a n:href="addItem" class="btn btn-primary" style="float: right;">
						<i class="icon-plus icon-white"></i>&nbsp;Přidat skladovou položku
					</a>
					<a n:href="showImportCsvModal!" class="btn btn-primary ajax" style="float: right; margin-right: 5px;">
						<i class="icon-plus icon-cloud-download"></i>&nbsp;Import faktury
					</a>
				{/form}
				<div class="clear"></div>
			</div>

			{snippet flashMessages}
				<div n:foreach="$flashes as $flash" class="alert alert-{$flash->type}">{$flash->message}</div>
			{/snippet}

			<div class="alert alert-info alert-no-margin" n:if="count($stockItemList) == 0">
				Nic nebylo nalezeno, zkuste jiné nastavení filtru.
			</div>

			<table class="table table-hover table-order-list" style="border-collapse: collapse;" n:snippet="orderList" n:if="count($stockItemList) > 0">
				<thead>
					<tr>
						<th style="width: 25px;"></th>
						{* <th>Id&nbsp;<a n:href='this, orderBy => id, orderWay => asc' class="order"><i class='icon-circle-arrow-up'></i></a>&nbsp;<a n:href='this, orderBy => id, orderWay => desc' class="order"><i class='icon-circle-arrow-down'></i></a></th> *}
						<th>Kód&nbsp;<a n:href='this, orderBy => code, orderWay => asc' class="order"><i class='icon-circle-arrow-up'></i></a>&nbsp;<a n:href='this, orderBy => code, orderWay => desc' class="order"><i class='icon-circle-arrow-down'></i></a></th>
						<th>Název&nbsp;<a n:href='this, orderBy => title, orderWay => asc' class="order"><i class='icon-circle-arrow-up'></i></a>&nbsp;<a n:href='this, orderBy => title, orderWay => desc' class="order"><i class='icon-circle-arrow-down'></i></a></th>
						<th>Skladem</th>
						<th>Zásoba</th>
						<th>Umístění</th>
						<th>Kategorie</th>
						<th>Příjem / výdej zboží</th>
						<th style="width: 60px;"></th>
					</tr>
				</thead>

				<tbody n:inner-foreach="$stockItemList as $stockItem">

					<tr n:class="$stockItem->stock_level === 'critical' ? bg-critical-light" data-id="{$stockItem->id}">
						<td>
							<input type="checkbox" name="si[]" value="{$stockItem->id}" onchange="updateLinkCreateOrder();"/>
						</td>
						<td >
							<a n:href="editItem 'id' => $stockItem->id" title="Zobrazit detail">{$stockItem->code}</a>
						</td>
						<td>
							<a n:href="editItem 'id' => $stockItem->id" title="Zobrazit detail">{$stockItem->title}</a>
						</td>
						<td>
							{$stockItem->in_stock} {\App\Models\StockItemModel::$UNIT_LIST[$stockItem->unit]}
						</td>
						<td>
							<div class="limit-box limit-box-small {if $stockItem->stock_level === 'critical'}bg-critical{/if}"></div>
							<div class="limit-box limit-box-small {if $stockItem->stock_level === 'warn'}bg-warn{/if}"></div>
							<div class="limit-box limit-box-small {if $stockItem->stock_level === 'ideal'}bg-ideal{/if}"></div>
							<div class="limit-box limit-box-small {if $stockItem->stock_level === 'over'}bg-over{/if}"></div>
						</td>
						<td>
							{$stockItem->location}
						</td>
						<td>
							{$stockItem->stock_item_category_title}
						</td>
						<td>
							<a n:href="showStockItemMoveModal! 'idStockItem' => $stockItem->id" class="ajax"><i class="icon-plus-sign"></i> Skladový pohyb</a>
						</td>
						<td>
							<a n:href="editItem 'id' => $stockItem->id" title="Zobrazit detail"><i class="icon-pencil"></i></a>
						</td>
					</tr>
				</tbody>
			</table>

			<table style="width: 100%;">
				<tr>
					<td style="width: 85%;">
						{control VisualPaginator}
					</td>
					<td></td>
				</tr>
			</table>
			<hr />

			<table style="width: 100%;">
				<tr>
					<td>
						Z vybraných výrobků
						<a n:href="showExportCsvModal!" class="btn btn-primary ajax" data-ref="CREATE-ORDER-LINK">
							<i class="icon-plus icon-cloud-upload"></i>&nbsp;Vytvořit objednávku
						</a>
					</td>
				</tr>
			</table>
		</div>
	</div>

	<script>
        /**
		 * Modifikace URL adresy - odkaz se vola ajaxem, proto je potreba aktualizovat odkaz pri kazde zmene checkboxu
         */
		function updateLinkCreateOrder() {
		    var anchorHref = $('[data-ref="CREATE-ORDER-LINK"]').attr('href');

		    var hrefParts = anchorHref.split('?');

            var queryResult = '?';
		    if (hrefParts.length >= 1) {
		        var query = hrefParts[1];
                var queryArray = query.split('&');
                for (var i = 0; i < queryArray.length; i++) {
                    var queryParam = queryArray[i];
                    if (queryParam.indexOf('si[]') === 0) {
                        continue;
                    }

                    if (queryResult !== '?') {
                        queryResult += '&';
                    }

                    queryResult += queryParam;
                }
			}

            $('input[name="si[]"]:checked').each(function() {
                if (queryResult !== '?') {
                    queryResult += '&';
                }

                queryResult += 'si[]=' + $(this).val();
			});

            $('[data-ref="CREATE-ORDER-LINK"]').attr('href', hrefParts[0] + queryResult);
		}
	</script>

{snippet stockItemMoveModal}
	{if isset($stockItemMoveModal)}
		{include '@stockItemMoveForm.latte'}
	{/if}
{/snippet}

{snippet importCsvModal}
	{if isset($importCsvModal)}
		{include '@importCsvForm.latte'}
	{/if}
{/snippet}

{snippet exportCsvModal}
	{if isset($exportCsvModal)}
		{include '@exportCsvForm.latte'}
	{/if}
{/snippet}