@extends('layouts.default')

@section('title')
	@parent
	Архив операций :: Бюджет для "{{ $workProject->name }}"
@stop

@section('breadcrumbs')
	{{ Breadcrumbs::render('budget.archive', $workProject) }}
@stop

@section('js')
	<script src="{{ asset('js/kmk-support.js') }}"></script>
	<script src="{{ asset('js/budget.js') }}"></script>
	<script>
		function reload () {
			var params = kmkGiveGetParams(location.search);
			params['startDate'] = $('#startDate').val();
			params['endDate'] = $('#endDate').val();
			kmkSetBrowserAddressParams(kmkObjectToGetString(params, false));
			location.reload();
		}

		$(document).ready(function() {
			var startDate = $('#startDate')
				.datepicker({
					onClose: function( selectedDate ) {
						$("#endDate").datepicker( "option", "minDate", selectedDate );
						reload();
					}
				});
			var endDate = $('#endDate')
				.datepicker({
					onClose: function( selectedDate ) {
						$( "#startDate" ).datepicker( "option", "maxDate", selectedDate );
						reload();
					}
				});

      var monthNames = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];

			var today = new Date,
			     i = 0;

			for(i; i>=-11; i--){
				var today = new Date,
						select,
						todayMonth;

        todayMonth = today.getMonth();
				today.setMonth(todayMonth+i);

				select += '<option value="'+i+'" class="01_'+(+today.getMonth()+1)+'_'+today.getFullYear()+'">'+monthNames[today.getMonth()]+', '+today.getFullYear()+'</option>';
			}
			$('#monthSelect').html(select);

			$('#monthSelect').on('change', function(){
				var startDate, endDate, startMonthPos, today, currMonth, month, lastDay;

				startMonthPos = $(this).val();
				today = new Date;
				currMonth = today.getMonth();

		    today.setMonth(currMonth+ +startMonthPos+1);
				month = today.getMonth();
				if(month == 0){
					month = 12;
				}
				startDate = '01.'+month+'.';
				today.setDate(0);
				startDate += today.getFullYear();

				lastDay = today.getDate();
				endDate = lastDay+'.'+(+today.getMonth()+1)+'.'+today.getFullYear();

				$("#startDate").val(startDate);
				$("#endDate").val(endDate);
				reload();
				return false;
			});

      var selectedMonth = ($("#startDate").val()).split('.').join('_');
      $('body').find('.'+selectedMonth).attr('selected','selected');

			$('a.to-humanize').each(function() {
				$(this).html(humanizeNumber($(this).html()));
			});
		});
	</script>
@stop

@section('content')
<div class="row">
  <div class="col-md-12">
		<div class="panel panel-default">
			@include('budget/menu')
			<div class="panel-body">
				<div id="operationsFiltr" class="btn-group btn-group-sm pull-left" style="margin-right:10px">
					<button class="btn btn-default @if (!$input['type']) active @endif" data-filter-name="type" data-filter-value="">Все</button>
					<button class="btn btn-default @if ($input['type'] == BudgetOperation::TYPE_INCOMING) active @endif" data-filter-name="type" data-filter-value="{{ BudgetOperation::TYPE_INCOMING }}">Приход</button>
					<button class="btn btn-default @if ($input['type'] == BudgetOperation::TYPE_OUTCOMING) active @endif" data-filter-name="type" data-filter-value="{{ BudgetOperation::TYPE_OUTCOMING }}">Расход</button>
					<button class="btn btn-default @if ($input['type'] == BudgetOperation::TYPE_EXCHANGE) active @endif" data-filter-name="type" data-filter-value="{{ BudgetOperation::TYPE_EXCHANGE }}">Перевод</button>
				</div>
				<form class="form-inline" role="form">
					<div class="form-group">
						<input id="startDate" name="startDate" class="form-control input-sm" value="{{ $startDate }}"/>
					</div>
					<div class="form-group">
						<input id="endDate" name="endDate" class="form-control input-sm" value="{{ $endDate }}"/>
					</div>
					<select class="form-control" name="monthSelect" id="monthSelect">
					</select>
					<br/><br/>
					<div class="form-group">
						<label for="accountSelect" class="control-label">Счет</label>
						{{ Form::select('accountSelect', $accountsToSelect, null, array('class' => 'form-control input-sm', 'id' => 'accountSelect')) }}
					</div>
					<div class="form-group">
						<label for="costSelect" class="control-label">Статья расходов</label>
						<select class="form-control input-sm" name="costSelect" id="costSelect">
							<?php $budgetCostsGroups = BudgetCost::whereIn('project_id', array($workProject->id, 0))
																                    ->with('budgetCostGroup')
											                              ->orderBy('name')
																                    ->groupBy('budget_cost_group_id')->get(); ?>
							  <option value="0">Все</option>
							@foreach ($budgetCostsGroups as $budgetCostsGroup)
							<optgroup label="{{ ($budgetCostsGroup->budgetCostGroup)? $budgetCostsGroup->budgetCostGroup->name:'Без группы' }}">
							  <?php
								  $costsToSelect = BudgetCost::whereIn('project_id', array($workProject->id, 0))
													                    ->where('budget_cost_group_id', '=', $budgetCostsGroup->budget_cost_group_id)
													                    ->orderBy('name')
													                    ->get(); ?>
							  @foreach ($costsToSelect as $costToSelect)
                <option value="{{ $costToSelect->id }}">{{ $costToSelect->name }}</option>
								@endforeach
						  </optgroup>
							@endforeach
						</select>
					</div>
					&nbsp;&nbsp;&nbsp;<label class="checkbox-inline"><input id="noUploaded" type="checkbox"/> только ручные операции </label>
					&nbsp;&nbsp;&nbsp;<label class="checkbox-inline"><input id="noCostId" type="checkbox"/> расходы без категории</label>
				</form>
				<br/>
				<dl class="dl-horizontal">
					<dt>Приход за период</dt>
					<dd><span class="to-humanize">{{ $budgetOperationsIn }}</span> <i class="fa fa-rub"></i></dd>
					<dt>Расход за период</dt>
					<dd><span class="to-humanize">{{ $budgetOperationsOut }}</span> <i class="fa fa-rub"></i></dd>
				</dl>
				<div class="table-responsive">
					<table class="table table-condensed table-hover">
						<thead>
							<tr>
								<th></th>
								<th>Дата</th>
								<th>Тип</th>
								<th>Счет</th>
								<th>Информация</th>
								<th class="text-right">Сумма</th>
								<th>Комментарий</th>
							</tr>
						</thead>
						<tbody id="budgetOperationsContainer">
							<?php 
								$projectsName = Project::getProjectsNameList();
							?>
							@foreach ($budgetOperations as $budgetOperation)
							@include('budget/operations/operation')
							@endforeach
						</tbody>
					</table>
				</div>
				<dl id="operationsSum" class="dl-horizontal hide">
					<dt>Итого всего</dt>
					<dd><span id="operationsSumAll" class="to-humanize"></span> <i class="fa fa-rub"></i></dd>
					<dt id="operationsSumNoIncomeText">Итого является расходом</dt>
					<dd><span id="operationsSumNoIncome" class="to-humanize"></span> <i class="fa fa-rub"></i></dd>
					<dt id="operationsSumIncomeText">Итого не является расходом</dt>
					<dd><span id="operationsSumIncome" class="to-humanize"></span> <i class="fa fa-rub"></i></dd>
				</dl>
			</div>
		</div>
	</div>
</div>
@stop
