@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')
        @elseif(Auth::user()->role==4)
            @include('local.professors._navigation')
        @endif
@stop

@section('main')

<div class="row">
  <div class="col-md-12">
    <div class="btn-group pull-right">
      {{ HTML::linkAction('TemplatesController@printview', 'Print', array($template->id), array('class'=>'btn btn-default')) }}
      <button class= 'btn btn-default' onclick ='fetchObjectivesForTemplate("#bodyRubric")'>View Objectives</button>
   
    </div>

    <p>Passing Criteria:
      <span id="expected_percentage">{{{ $template->expected_percentage }}}% of students must obtain at least </span>
      <span id="expected_points">{{{$template->expected_points}}}</span> points
    </p>

    <table class="table datatable" id= "RubricTable">
      <thead>
          <tr>
            <th></th>
            
            <th>Criterion</th>
            @for($i = 0; $i <$template->num_scales; $i++)
              <th>{{$template->titles[$i]}}  ({{1+($i*($template->max_score/$template->num_scales))}} - {{(1+$i)*($template->max_score/$template->num_scales)}})</th>
              
                           
            @endfor
            
            <th>Learning Outcome</th>
          </tr>
        </thead>
      <tbody id ="bodyRubric">
        @foreach($template->criteria as $index=> $criterion)
      <tr data-criterion-id = "{{$criterion->id}}" data-criterion-copyright="{{$criterion->copyright}}" data-criterion-notes="{{$criterion->notes}}">
        <td>{{$index+1}}.</td>

        @if($criterion->notes)
            
          <td><span><em data-toggle="tooltip" data-placement="top" title="{{$criterion->notes}}">{{$criterion->name}}</em></span><sup></sup></td>
          

        
        @else
          
          <td><span>{{$criterion->name}}</span><sup></sup></td>

        
@endif

@foreach ($criterion->scales as $scale)
<td>{{$scale}}</td>
    
@endforeach
<td><ol>
@foreach ($criterion->outcomes as $outcome)
    <p>{{$outcome->name}}</p>
@endforeach
</ol></td>
        @endforeach

      </tbody>
    </table>


    <br>
    <br>
    <div id="copyright-info">
      <hr>
      <p class="small"><strong>Copyright Information</strong></p>
      <ul id="copyright-list" class="list-unstyled small">
      </ul>
      <hr>
    </div>


    <br>
    <br>
    <br>

  </div>
</div>


<div class="modal fade" id="objectives-crit-modal">
  <div class="modal-dialog modal-md">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title text-center" id ="modal-title-rubric">These are the objectives in each Criterion in this Rubric</h4>
      </div>
        <div class="modal-body" id ="objectives-crit-modal-body">
            



               
                
                
        </div>
      <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div>
<script> 

function fetchObjectivesForTemplate(tbody){
  
  allCriteria = [];
        $(tbody).children('tr').each(function (index){
            allCriteria.push($(this).data('criterion-id'));
        })
        $.post(
            "{{URL::action('TemplatesController@fetchObjectivesForTemplate')}}",
            {
                allCriteria:allCriteria
            },

            function(crit){
                
                modal_name = '#objectives-crit-modal';
                $(modal_name+'-body').html(' ');
                ol_crit = $("<ol>")
                $(modal_name+'-body').append(ol_crit);
                $.each(crit, function(index, cri){

                    
                    li = $("<li>").html("<strong>"+cri.name+"</strong>");
                    
                    ul_for_ob = $("<ul>");
                    
                        $.each(cri.all_objectives, function(ind, obj){
                            li2 = $("<li>").html(obj.text)
                            ul_for_ob.append(li2);
                        })
                        li.append(ul_for_ob);
                    ol_crit.append(li);

                    
                })
                $(modal_name).modal('show');

            }
        )
    }

/*
  function createTable(){
    $.post("{{URL::action('TemplatesController@onLoadFetch')}}",
        {id :{{$template->id}}},
        function (json){
          table = $('#RubricTable');
        for(criterionIndex in json.criteria ){

          criterion = json.criteria[criterionIndex];
          trFull = '<tr data-criterion-copyright="'+criterion.copyright+'" data-criterion-notes="'+criterion.notes+'">';
          trFull+= '<td>'+(parseInt(criterionIndex)+1)+'.</td>';
          if(criterion.notes){
            
            trFull+= '<td><span><em data-toggle="tooltip" data-placement="top" title="'+criterion.notes+'">'+criterion.name+'</em></span><sup></sup></td>';
            //if(criterion.subcriteria){
              
            //}

          }
          else{
            
            trFull+='<td><span>'+criterion.name+'</span><sup></sup></td>';

          }

          for(scalesIndex in json.scales[criterion.criterion_id]){
            scale = json.scales[criterion.criterion_id][scalesIndex];
            trFull += '<td>'+scale.description+'</td>';
          }
          trFull += '<td>Prueba</td>';
          trFull +='</tr>';
          
          $("#bodyRubric").append(trFull);
        }


        
        
        }, 'json'
        );
  }**/
  </script>
@stop

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

@section('javascript')

// --------------------------------------------------------------------------
// Page load
// --------------------------------------------------------------------------

buildCopyrightList();

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

// Build list from copyright info in template
function buildCopyrightList()
{
  // Empty the copyright list
  $('#copyright-list').empty();

  $('tbody tr').each(function( index )
  {
    var criterion = $(this);
    // If there's copyright info (in this particular view, it checks for empty strings)
    if(criterion.data('criterion-copyright')!='')
    {
      var copyright = criterion.data('criterion-copyright');

      // If there is anything in the copyright list
      if($('#copyright-list li').length>0)
      {

        // Check copyright list for the same copyright text
        var found = false;
        $('#copyright-list li').each(function()
        {
          // If found, give the string its number
          if(copyright==$(this).find('span').text())
          {
            copyrightNumber = Number.parseInt($(this).find('sup').text());


            criterion.children('td:nth-child(2)').find('sup').text(copyrightNumber);
            found =true;

            //to break
            return false;
          }
        });

        // Otherwise, give it the next number and append a new item to the
        // list
        if(!found)
        {
          var copyrightNumber = $('#copyright-list li').length+1;
          criterion.children('td:nth-child(2)').find('sup').text(copyrightNumber);
          $('#copyright-list').append('<li><sup>'+copyrightNumber+' </sup><span>'+copyright+'<span></li>');
        }
      }
      // Otherwise, give it number 1 and append it
      else
      {

        criterion.children('td:nth-child(2)').find('sup').text('1');
        $('#copyright-list').append('<li><sup>1 </sup><span>'+copyright+'<span></li>');
      }
    }
  });

  if($('#copyright-info li').length>0)
  {
    $('#copyright-info').show();
  }
  else
  {
    $('#copyright-info').hide();
  }
}


@stop
