@extends('layouts.default')

@section('title')
	@parent
	Продукты проекта {{ $project->name }}
@stop

@section('js')
	<script src="{{ asset('js/products.js') }}"></script>
@stop

@section('content')
<div class="row">
  <div class="col-md-12">
		<h4>Продукты проекта {{ $project->name }} <small><a href="{{ action('ProductsController@export', array($project->id)) }}" target="_blank">экспорт в Excel</a></small></h4>
		<div class="btn-group btn-group-search-input">
			<input id="partnerProductsSearch" type="text" class="form-control" placeholder="Поиск" autocomplete="off" autofocus>
			<span class="btn-search-clear glyphicon glyphicon-remove-circle"></span>
		</div>
		<div class="table-responsive">
			<table id="partnerProducts" class="table table-striped table-hover">
				<thead>
					<th>Название</th>
					<th>Цена</th>
					<th class="text-center">Доступно (забронировано)</th>
					<th>Действия</th>
				</thead>
				<tbody>
					<?php
					  $productGroupQuant = $project->getProductGroupQuant();
					?>
					@foreach ($project->products as $product)
					<tr>
						<td>
							<span class="to-search">{{ $product->mark->name }} {{ $product->name }}</span>
							<br/>
							<small>{{ $product->year_start }} - {{ $product->year_end }}</small>
						</td>
						<td class="td-nobr">
							<?php
								$prices = $product->getPrices();
							?>
							@foreach (ProductType::all() as $productType)
								@if ((isset($prices[$productType->id]['price']) && $prices[$productType->id]['price']) || (isset($prices[$productType->id]['price_back']) && $prices[$productType->id]['price_back']))
									{{ $productType->name }} / <span class="to-humanize">{{ $prices[$productType->id]['price'] }}</span> <i class="fa fa-rub"></i> / <span class="to-humanize">{{ $prices[$productType->id]['price_back'] }}</span> <i class="fa fa-rub"></i> / <span class="to-humanize">{{ $prices[$productType->id]['price_opt'] }}</span> <i class="fa fa-rub"></i>, {{ $prices[$productType->id]['plates'] }}/{{ $prices[$productType->id]['hooks'] }}/{{ $prices[$productType->id]['splates'] }}<br/>
								@endif
							@endforeach
						</td>
						<?php
						  $quantProductType = $product->getQuant();
							$noProduction     = $product->no_production;
						?>
						<td class="text-center td-nobr">
							@if ($noProduction == 0)	
								@foreach (ProductType::all() as $productType)
									@if ((isset($prices[$productType->id]['price']) && $prices[$productType->id]['price']) || (isset($prices[$productType->id]['price_back']) && $prices[$productType->id]['price_back']))
									<?php
									  $groupId = $prices[$productType->id]['product_group_id'];
										$quantBlocked = 0;
										$quant        = 0;
										$groupName    = '';
										if($groupId){
										  $quantBlocked = $productGroupQuant[$groupId]['quant_blocked'];
										  $quant        = $productGroupQuant[$groupId]['quant'];
											$groupName    = $productGroupQuant[$groupId]['name'];;
										}
									?>
									  @if($groupId)
										<a href="#" class="td-nobr-block"><i class="fa fa-caret-down fa-2x product-quant-list setQuantGroupDown" data-action="{{ action('ProductsGroupController@updateQuant', array($project->id, $groupId, "down")) }}"></i></a>
										<span class="to-humanize td-nobr-block text-bold text-success productQuant" data-product-group-id="{{ $groupId }}" title="Группа: {{ $groupName }}">{{ $quant - $quantBlocked }}</span>
										<a href="#" class="td-nobr-block"><i class="fa fa-caret-up fa-2x product-quant-list setQuantGroupUp" data-action="{{ action('ProductsGroupController@updateQuant', array($project->id, $groupId, "up")) }}"></i></a>
										<span class="to-humanize td-nobr-block">({{ $quantBlocked }})</span><br>
									  @else
										<a href="#" class="td-nobr-block"><i class="fa fa-caret-down fa-2x product-quant-list setQuantProductTypeDown" data-action="{{ action('ProductsController@updateQuant', array($project->id,  $product->id, $productType->id, "down")) }}"></i></a>
											<span class="to-humanize td-nobr-block productQuant">{{ $quantProductType[$productType->id]['quant'] - $quantProductType[$productType->id]['quant_blocked'] }}</span>
										<a href="#" class="td-nobr-block"><i class="fa fa-caret-up fa-2x product-quant-list setQuantProductTypeUp" data-action="{{ action('ProductsController@updateQuant', array($project->id,  $product->id, $productType->id, "up")) }}"></i></a>
										<span class="to-humanize td-nobr-block">({{ ($quantProductType[$productType->id]['quant_blocked'])? $quantProductType[$productType->id]['quant_blocked']:0 }})</span><br>
										@endif
									@endif
								@endforeach
							@endif
						</td>
						<td>
							<button class="btn btn-sm btn-default btn-block" onClick="location.href ='{{ action('ProductsController@edit', array($project->id, $product->id)) }}'"> &nbsp;Редактировать&nbsp; </button>
							<button class="btn btn-sm btn-default btn-block action_confirm" type="button" data-method="delete" href="{{ action('ProductsController@destroy', array($project->id, $product->id)) }}"> Удалить </button>
						</td>
					</tr>
					@endforeach
				</tbody>
			</table>
		</div>
		<button class="btn btn-primary" onClick="location.href ='{{ URL::to('projects') }}/{{ $project->id }}/products/create'">Добавить продукт</button>
	</div>
</div>
@stop
