@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="col-md-12">

            <div id="quinquennium" data-quinquennium-id="{{ $plan->quinquennium->id }}"></div>
            <div id="program" data-program-id="{{ $program->id }}"></div>

            @if (!$plan->is_submitted)
                @if (date('Y-m-d') <= $plan->quinquennium->five_year_plan_due_date)
                    <span class="glyphicon glyphicon-info-sign"></span>
                    <strong>Note:</strong> This plan is saved as a <strong>draft.</strong> Remember to submit it by
                    <strong>{{ date('F d', strtotime($plan->quinquennium->five_year_plan_due_date)) }}</strong>. After the
                    due date, unsubmitted drafts will not be considered.</strong>

                    <div class="text-right">
                        <br>

                        {{ Form::open(['action' => 'FiveYearPlansController@update', 'class' => 'form-inline']) }}
                        <a href="{{ URL::action('FiveYearPlansController@edit', ['program' => $program->id, 'plan' => $plan->id]) }}"
                            class="btn btn-primary edit">
                            <span class="glyphicon glyphicon-pencil"></span>
                            Edit
                        </a>

                        <button class="btn btn-primary pdf">
                            <span class="glyphicon glyphicon-arrow-left"></span>
                            PDF
                        </button>

                        <a href="{{ URL::action('FiveYearPlansController@msWord', ['plan' => $plan->id]) }}"
                            class="btn btn-primary">
                            <span class="glyphicon glyphicon-download-alt"></span>
                            MS Word
                        </a>

                        <button type="submit" class="btn btn-primary">
                            <span class="glyphicon glyphicon-send"></span>
                            Submit
                        </button>

                        <a href="{{ URL::action('FiveYearPlansController@index') }}" class="btn btn-primary submit">
                            <span class="glyphicon glyphicon-arrow-left"></span>
                            Back to Five Year Plans
                        </a>
                        {{ Form::close() }}

                        <br>
                    </div>
                @else
                    <span class="glyphicon glyphicon-info-sign"></span>
                    <strong>Note:</strong> This plan was not submitted on time.</strong>
                @endif
            @else
                <span class="glyphicon glyphicon-ok-sign"></span>
                <strong>Note:</strong> This plan was submitted on {{ date('F j, Y', strtotime($plan->submitted_on)) }}


                <div class="text-right">
                    @if (date('Y-m-d') < $plan->quinquennium->five_year_plan_due_date)
                        <br>
                        <a href="{{ URL::action('FiveYearPlansController@edit', ['program' => $program->id, 'plan' => $plan->id]) }}"
                            class="btn btn-primary revert">
                            <span class="glyphicon glyphicon-backward"></span>
                            Revert Submission
                        </a>
                    @endif

                    <br><br>
                </div>
            @endif

            <!-- Nav tabs -->
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#full-plan" aria-controls="full-plan" role="tab"
                        data-toggle="tab">
                        <h4>Full Plan</h4>
                    </a></li>
                <li role="presentation"><a href="#summary" aria-controls="summary" role="tab" data-toggle="tab">
                        <h4>Summary</h4>
                    </a></li>
            </ul>
            <br>

            <!-- Tab panes -->
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="full-plan">
                    @foreach ($plan->fypParts as $mini_plan)
                        <div class="mini-plan">
                            <table class="table table-bordered" id="table">
                                <thead>
                                    <tr class="bg-warning text-center">
                                        <th class="text-center col-md-1">Academic Year</th>
                                        <th class="text-center col-md-3">Learning Outcome to be assessed</th>
                                        <th class="text-center col-md-4">Learning Objectives</th>
                                        <th class="text-center col-md-4">Courses to use for assessment</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($mini_plan->fypPartOutcomes as $index => $outcome)
                                        <tr>
                                            <!-- If outcome is the first, include multi-row year column -->
                                            @if ($index == 0)
                                                <th class="active academic-year text-center"
                                                    rowspan="{{ count($mini_plan->fypPartOutcomes) }}">
                                                    {{ $mini_plan->year_start }}-{{ $mini_plan->year_end }}</th>
                                            @else
                                                <th class="hidden">
                                                    {{ $mini_plan->year_start }}-{{ $mini_plan->year_end }}</th>
                                            @endif
                                            <td class="outcome-cell">
                                                {{ $outcome->outcome->name }}
                                            </td>
                                            <td class="objectives-cell">
                                                <ul>
                                                    @foreach (json_decode($outcome->objectives) as $objective)
                                                        <li class="objective">{{ $objective->text }}</li>
                                                    @endforeach
                                                </ul>
                                            </td>
                                            <td>
                                                <ul>
                                                    @foreach (json_decode($outcome->courses) as $course)
                                                        <li class="course">
                                                            {{ $course->code }}{{ $course->number }}:
                                                            {{ $course->name }}</li>
                                                    @endforeach
                                                </ul>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <br>
                    @endforeach
                </div>
                <div role="tabpanel" class="tab-pane" id="summary">

                    <p>This summary is automatically generated using the information from the Full Plan. Any changes to the
                        plan will be reflected here.</p>

                    <table class="table table-bordered text-center">
                        <thead>
                            <tr class="bg-warning">
                                <th class="col-md-4">Learning Outcome</th>
                                @foreach ($plan->fypParts as $mini_plan)
                                    <th class="academic-year text-center">
                                        {{ $mini_plan->year_start }}-{{ $mini_plan->year_end }}</th>
                                @endforeach
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($outcomes as $outcome)
                                <tr>
                                    <th class="active">{{ $outcome->name }}</th>
                                    @foreach ($plan->fypParts as $mini_plan)
                                        @if ($mini_plan->willAssessOutcome($outcome->id))
                                            <td><span class="glyphicon glyphicon-ok"></span></td>
                                        @else
                                            <td></td>
                                        @endif
                                    @endforeach
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>

            @if (!$plan->is_submitted)
                @if (date('Y-m-d') <= $plan->quinquennium->five_year_plan_due_date)
                    <div class="text-right">
                        <br>

                        {{ Form::open(['action' => 'FiveYearPlansController@update', 'class' => 'form-inline']) }}
                        <a href="{{ URL::action('FiveYearPlansController@edit', ['program' => $program->id, 'plan' => $plan->id]) }}"
                            class="btn btn-primary edit">
                            <span class="glyphicon glyphicon-pencil"></span>
                            Edit
                        </a>

                        <button class="btn btn-primary pdf">
                            <span class="glyphicon glyphicon-arrow-left"></span>
                            PDF
                        </button>

                        <a href="{{ URL::action('FiveYearPlansController@msWord', ['plan' => $plan->id]) }}"
                            class="btn btn-primary">
                            <span class="glyphicon glyphicon-download-alt"></span>
                            MS Word
                        </a>

                        <button type="submit" class="btn btn-primary">
                            <span class="glyphicon glyphicon-send"></span>
                            Submit
                        </button>

                        <a href="{{ URL::action('FiveYearPlansController@index') }}" class="btn btn-primary submit">
                            <span class="glyphicon glyphicon-arrow-left"></span>
                            Back to Five Year Plans
                        </a>
                        {{ Form::close() }}

                        <br>
                    </div>
                @else
                    <span class="glyphicon glyphicon-info-sign"></span>
                    <strong>Note:</strong> This plan was not submitted on time.</strong>
                @endif
            @else
                <span class="glyphicon glyphicon-ok-sign"></span>
                <strong>Note:</strong> This plan was submitted on {{ date('F j, Y', strtotime($plan->submitted_on)) }}


                <div class="text-right">
                    @if (date('Y-m-d') < $plan->quinquennium->five_year_plan_due_date)
                        <br>
                        <a href="{{ URL::action('FiveYearPlansController@edit', ['program' => $program->id, 'plan' => $plan->id]) }}"
                            class="btn btn-primary revert">
                            <span class="glyphicon glyphicon-backward"></span>
                            Revert Submission
                        </a>
                    @endif

                    <br><br>
                </div>
            @endif
        </div>
    </div>

@stop

@section('included-js')

    <script src="{{ asset('vendor/jsPDF-master/dist/jspdf.min.js') }}"></script>
    <script src="{{ asset('vendor/jsPDF-AutoTable-master/dist/jspdf.plugin.autotable.js') }}"></script>

@stop

@section('javascript')

    // --------------------------------------------------------------------------
    // Page Load
    // --------------------------------------------------------------------------
    $('.datatable').DataTable(
    {
    "searching": false,
    "paging": false,
    "ordering": false,
    "info": false
    });

    // --------------------------------------------------------------------------
    // Events
    // --------------------------------------------------------------------------

    $('.pdf').on('click', function(e)
    {
    e.preventDefault();

    console.log('start');

    var doc = new jsPDF('l', 'pt');
    doc.setFontSize(10);
    doc.text("From HTML", 40, 50);


    var columns = ['Academic Year', 'Learning Outcome to be assessed', 'Learning Objectives', 'Courses to use for
    assessment'];

    $('table').each(function(index, value)
    {
    if(index != 0 )
    {
    doc.addPage();
    }

    console.log('start table');

    var rows = new Array();

    var table = $(this);

    var disp_table = new Object();
    disp_table.columns = columns;

    // Iterate tables
    table.find('tbody tr').each(function(index, value)
    {
    console.log('row: '+(index+1));
    var tr = $(this);
    var row = new Array();
    var academic_year = tr.find(':nth-child(1)').html();
    var outcome = $.trim(tr.find(':nth-child(2)').html());
    var objectives_dom = tr.find(':nth-child(3)');
    var courses_dom = tr.find(':nth-child(4)');
    var objectives = new Array();
    var courses = new Array();


    objectives_dom.find('.objective').each(function()
    {
    objectives.push('• '+$.trim($(this).text()));
    });

    courses_dom.find('.course').each(function()
    {
    courses.push('• '+$.trim($(this).text()));
    });

    console.log(academic_year);
    console.log(outcome);
    console.log(objectives);
    console.log(courses);

    row.push(academic_year);
    row.push(outcome);
    row.push(jQuery.extend([], objectives));
    row.push(jQuery.extend([], courses));

    rows.push(jQuery.extend([], row));

    disp_table.rows = jQuery.extend([], rows);

    console.log('end row: '+(index+1));
    });

    console.log(disp_table);

    doc.autoTable(columns, rows,
    {
    theme: "grid",
    styles: {
    overflow: 'linebreak'
    },
    drawCell: function (cell, data) {
    console.log('CELL DRAW START');

    console.log(cell);
    if (data.column.index == 2 || data.column.index == 3)
    {
    doc.rect(cell.x, cell.y, cell.width, cell.height, 'S');
    doc.text(cell.raw, cell.x + 5, cell.y + 12);

    console.log('CELL DRAW END');

    return false;
    }

    console.log('CELL DRAW END');

    return true;
    }
    });

    console.log('end table');


    });

    //doc.save();
    return;

    });

    // --------------------------------------------------------------------------
    // Functions
    // --------------------------------------------------------------------------


@stop
