@extends('layouts.master')

@section('navigation')
    @if (Auth::user()->role == 1)
        @include('local.managers.admins._new_navigation')
    @elseif(Auth::user()->role == 2)
        @include('local.managers.sCoords._new_navigation')
    @elseif(Auth::user()->role == 3)
        @include('local.managers.pCoords._new_navigation')
    @endif
@stop

@section('main')
    <div class="row">
        <div class="table-responsive">
            <table class="table table-bordered datatable">
                <thead>
                    <tr>
                        <th class="active col-sm-2 col-md-2">Program</th>
                        <th class="active col-sm-2 col-md-2">School</th>
                        @foreach ($quinquenniums as $quinquennium)
                            @for ($year = date('Y', strtotime($quinquennium->start_date)); $year < date('Y', strtotime($quinquennium->end_date)); $year++)
                                <th class="active">{{ $year }} - {{ $year + 1 }}</th>
                            @endfor
                        @endforeach
                    </tr>
                </thead>
                @foreach ($programs as $program)
                    <tr>
                        <th class="active col-sm-2 col-md-2"> {{ $program->name }} </th>
                        <td class="col-sm-2 col-md-2"> {{ $program->school->name }} </td>
                        @foreach ($quinquenniums as $quinquennium)
                            @for ($year = date('Y', strtotime($quinquennium->start_date)); $year < date('Y', strtotime($quinquennium->end_date)); $year++)
                                <td>
                                    <?php $annual_plan = AnnualPlan::where('program_id', $program->id)
                                        ->where('quinquennium_id', $quinquennium->id)
                                        ->where('year_start', $year)
                                        ->first(); ?>
                                    @if ($annual_plan)
                                        <a class="btn btn-sm btn-default"
                                            href="{{ URL::action('AnnualPlansController@show', [$program->id, $annual_plan->id]) }}">
                                            <span class="glyphicon glyphicon-eye-open"></span>
                                            View
                                        </a>
                                        @if ($quinquennium->id == $current_quinquennium->id && // Quinquennium is running
                                        date('Y') == $year && // Current year is the same as year
                                        date('m') <= date('m', strtotime($current_quinquennium->annual_plan_due_date)) && // Current month (and day, below) is the same or before the due month (and day)
                                            date('d') <= date('d', strtotime($current_quinquennium->annual_plan_due_date)))
                                            <a class="btn btn-sm btn-default"
                                                href="{{ URL::action('AnnualPlansController@edit', [$program->id, $annual_plan->id]) }}">
                                                <span class="glyphicon glyphicon-pencil"></span>
                                                Edit
                                            </a>
                                        @endif
                                    @elseif($quinquennium->id == $current_quinquennium->id && // Quinquennium is running
                                    date('Y') == $year && // Current year is the same as year
                                    date('m') <= date('m', strtotime($current_quinquennium->annual_plan_due_date)) && // Current month (and day, below) is the same or before the due month (and day)
                                        date('d') <= date('d', strtotime($current_quinquennium->annual_plan_due_date)))
                                        <a class="btn btn-sm btn-default"
                                            href="{{ URL::action('AnnualPlansController@create', [$program->id]) }}">
                                            <span class="glyphicon glyphicon-plus"></span>
                                            Create
                                        </a>
                                    @else
                                        Plan unavailable
                                    @endif
                                </td>
                            @endfor
                        @endforeach
                    </tr>
                @endforeach
            </table>
        </div>
    </div>
@stop

@section('included-js')

@stop

@section('javascript')

    table = $('.datatable').dataTable({
    "columnDefs": [
    { "searchable": true, "sortable": true, "targets": [0, 1]},
    ]
    });

    $('a.toggle-vis').on('click', function (e) {
    e.preventDefault();

    // Get the column API object
    var column = table.column( $(this).attr('data-column') );

    // Toggle the visibility
    column.visible( ! column.visible() );
    } );

@stop
