@extends('layouts.master')

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

@section('main')
  <button class="btn btn-primary pull-right" id="exportAll">Export All (PDF)</button>

      <div class="row">
          <div class="col-md-12">
            @foreach ($programs_info as $program)

            <h3 id="{{ $program[0]->id }}" class="outcome">{{ $program[0]->name }}</h3>
              <table id="table{{ $program[0]->id }}" class="table table-condensed table-bordered datatable-local">
                  <thead>
                    <tr class="center-text">
                        <th>Campus Domains
                         </th>
                        <th>Results based on target (% of students scoring >= target)</th>
                        <th>Courses used to measure and tools</th>
                        <th>Summary of dissemination methods employed</th>
                        <th>Summary of transformative actions or improvement</th>
                    </tr>

                  </thead>
                  <tfoot>
                      <tr class="column-search">
                          <th><input class="column-search-bar form-control" type="text" placeholder="Buscar"/></th>
                          <th><input class="column-search-bar form-control" type="text" placeholder="Buscar"/></th>
                          <th><input class="column-search-bar form-control" type="text" placeholder="Buscar"/></th>
                          <th><input class="column-search-bar form-control" type="text" placeholder="Buscar"/></th>
                          <th><input class="column-search-bar form-control" type="text" placeholder="Buscar"/></th>
                      </tr>
                  </tfoot>
                  <tbody>
                    <tr class="center-text">
                      <th>
                      </th>
                      <th>
                      </th>
                      <th>
                        {{ $program[0]->name }}
                      </th>
                      <th>
                      </th>
                      <th>
                      </th>
                    </tr>
                  @foreach ($program[1] as $domain)
                    @foreach ($selected_semesters as $semester)
                      @if ($domain->activation_date >= $semester->start
                           and $domain->activation_date <= $semester->end)
                      <tr>
                        <td>
                          {{$domain->name}}
                          <br>{{$domain->activation_date}}
                        </td>
                        <td>### Measurement <br>
                          (semester) <br>
                          <br>
                          ###% <br>
                          N= ## <br>
                        </td>
                        <td>
                          <b>Courses</b><br>
                          @foreach ($program[2] as $course)
                            @if ($course->domain_name == $domain->name)
                              {{$course->code}} ({{$course->number}}) <br>
                            @endif
                          @endforeach
                          <br>
                          <b>Tools</b><br>

                          <br>
                        </td>
                        <td>(No esta implementado)</td>
                        <td>
                          @foreach ($program[3] as $action)
                            @if ($action->domain_name == $domain->name)
                              {{$action->transformative_actions}}<br><br>
                            @endif
                          @endforeach
                        </td>
                      </tr>
                    @endif
                  @endforeach
                @endforeach
              </tbody>
              </table>
            @endforeach
          </div>
      </div>
      <span class="js-vars"
          data-pdf-url="{{ URL::action('OutcomesController@assessmentReport') }}"
      ></span>

@stop

@section('included-js')
    @include('global._datatables_js')
@stop


@section('javascript')

$(document).ready(function()
{
  // --------------------------------------------------------------------------
  // Page load
  // --------------------------------------------------------------------------

  // --------------------------------------------------------------------------
  // Functions
  // --------------------------------------------------------------------------
  var k = 0;
  var divulgaciones = "Divulgaciones:{{$selected_divulgaciones}}";
  var programs = [<?php
    foreach ($programs_info as $program) {
      print('"'.$program[0]->name.'",');
    }
  ?>]
  var tables_names = {{$tables_names}};
  var table = null;
  var tables = [];
  $('.datatable-local').each(function()
  {
      let thisTable = this;
      table = $(this).DataTable({
          lengthMenu: [5, 10, 25, 50],
          autoWidth: false,
          retrieve: true,
          pageLength: 10,
          // //fixed cols
          // fixedColumns:   {
          //     leftColumns: 1,
          // },
          // scrollY:        "600px",
          // scrollX:        true,
          // scrollCollapse: true,

          bSortCellsTop: true, // To apply sort using top row only
          order: [[ 0, "asc" ]],
          dom: 'Bflrtip',
          buttons: [
              {
                  extend: 'collection',
                  text: 'Export <span class="caret"></span>',
                  buttons: [
                      {
                          extend: 'print',
                          exportOptions: {
                              columns: ':visible'
                          },
                          text: 'Print',
                      },
                      {
                          extend: 'excelHtml5',
                          exportOptions: {
                              columns: ':visible'
                          }
                      },
                      {
                          extend: 'pdfHtml5',
                          messageTop: function () {
                            var message = 'Program: '+tables_names[$(thisTable).attr("id")]+'\n\n'+divulgaciones;
                            return message;
                         },
                          exportOptions: {
                              columns: ':visible',
                              stripNewlines: false,
                          },
                          customize: function(doc){
                              //$('#loadingModal').modal('show');

                              doc.pageSize = 'LEGAL';
                              doc.pageOrientation ='landscape';
                              doc.defaultStyle.fontSize = 10;
                              doc.styles.tableHeader.fillColor = '#E70033';

                              // doc.content[1].layout= 'lightHorizontalLines';
                              doc.content[1].layout = {
                                  hLineWidth: function (i, node) {
                                      if(i === 1){
                                          return 2;
                                      }
                                      else if(i > 1){
                                          return 1;
                                      }
                                      else {
                                          return 0;
                                      }
                                  },
                                  vLineWidth: function (i, node) {
                                      return 0;
                                  },
                                  hLineColor: function (i, node) {
                                      if(i === 1){
                                          return '#000';
                                      }
                                      else if(i > 1){
                                          return '#ccc';
                                      }
                                      else {
                                          return 'transparent';
                                      }
                                  },
                                  // vLineColor: function (i, node) {
                                  //     return (i === 0 || i === node.table.widths.length) ? 'black' : 'gray';
                                  // },
                                  paddingLeft: function(i, node) { return 5; },
                                  paddingRight: function(i, node) { return 5; },
                                  paddingTop: function(i, node) { return 5; },
                                  paddingBottom: function(i, node) { return 5; }
                              };

                              doc.content.splice( 0, 0, {
                                  margin: [ 0, 0, 0, 12 ],
                                  alignment: 'center',
                                  width: 150,
                                  image: 'data:image/png;base64,[encoded data, 31780 characters]'
                              } );

                              //$('#loadingModal').modal('hide');

                          }
                      },
                  ]
              },
              {
                  extend: 'colvis',
                  columns: ':gt(0)',
                  text: 'Column visibility <span class="caret"></span>',
              }
          ],
      });
      tables.push(table);

      // Apply the search
      table.columns().every( function ()
      {
          // Search by keyword
          var column = this;
          $('input.column-search-bar', column.footer()).on('keyup change', function () {
              if (column.search() !== this.value) {
                  console.log(this.value)

                  column
                      .search(this.value)
                      .draw();
              }
          } );

          // Search by dropdown menu
          column.data().unique().sort().each( function (d, j) {
              $('select.column-search-select', column.footer()).append('<option value="'+d+'">'+d+'</option>');
          });

          $('.column-search-select', column.footer()).on( 'change', function () {
          var val = $.fn.dataTable.util.escapeRegex($(this).val());

          column
              .search(val ? '^'+val+'$' : '', true, false)
              .draw();

          } );
      } );

      table.buttons().container()
      .appendTo( $('.col-sm-6:eq(0)', table.table().container()));

      $(document).on('click', '.buttons-columnVisibility', function(){
          console.log('visibility toggled');
          table.columns.adjust().responsive.recalc();
      });
  });
  // --------------------------------------------------------------------------
  // Events
  // --------------------------------------------------------------------------

  // When export button is clicked, export all tables as PDF
  $(document).on('click', '#exportAll', function() {
    var r = confirm("Are you sure you want to download all programs info as separate PDFs? \n It may take a moment to process.");
    if (r == true) {

      //var 'tables' is defined in master.blade.php

      $.each(tables, function(index, table)
      {
        {{-- $(table).text = 'test title'; --}}
        table.buttons('0-2').trigger();
      });

    }
  });
});
@stop
