@extends('adminlte::page')
@section('content_header')
    @notifyCss
    @notifyJs
    <script type="text/javascript">
      var training_date_arr =[];
    </script>
@stop
@section('content')
    <div class="row">
        <div class="col-12">
            <div class="card card-primary">
                <div class="card-header">
                    <h3 class="card-title">Assign Topic</h3>
                </div>
                @include('notify::messages')
                <!-- /.card-header -->
                <!-- form start -->
                <form id="userregform" class="form-horizontal" action="{{URL::to('/assign_topic_post')}}" method="post" autocomplete="off" enctype="multipart/form-data">
                  {{ csrf_field() }}
                  <div class="card-body">
                      <div class="col-12">
                          <div class="form-group row">
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Organisation</label><br>
                                  {{$schedules->organisation->organisation_name}}
                              </div>
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Department</label><br>
                                  @if(isset($schedules->department_id))
                                    {{$schedules->department->department_name}}
                                  @endif
                              </div>
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Section</label><br>
                                  @if(isset($schedules->section_id))
                                    {{$schedules->section->section_name}}
                                  @endif
                              </div>
                              <div class="col-3">
                                <label for="exampleInputEmail1">Material Type</label><br>
                                  {{$schedules->material_type->material_type_name}}
                              </div>
                          </div>
                          <div class="form-group row">
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Training Title</label><br>
                                  {{$schedules->training_title}}
                                  <input type="hidden" name="schedule_id" id="schedule_id" value="{{$schedules->id}}">
                              </div>
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Venue</label><br>
                                  {{$schedules->venue}}
                              </div>
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Training Period</label><br>
                                  @php
                                    $period_from = $schedules->period_from;
                                    $period_to = $schedules->period_to;
                                    if($period_from)
                                    {
                                        $period_from = date("d-M-Y", strtotime($period_from));
                                    }
                                    if($period_to)
                                    {
                                        $period_to = date("d-M-Y", strtotime($period_to));
                                    }
                                    echo $period_from." to ".$period_to;
                                  @endphp 
                                  
                              </div>
                              <div class="col-3">
                                <label for="exampleInputEmail1">Min & Max seats for training</label><br>
                                  @php
                                    $min_seat_training = $schedules->min_seat_training;
                                    $max_seat_training = $schedules->max_seat_training;
                                    
                                    echo $min_seat_training." & ".$max_seat_training;
                                  @endphp 
                              </div>
                          </div>
                          <div class="form-group row">
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Roles</label><br>
                                  {{$schedules->training_title}}
                              </div>
                          </div>
                          <div class="form-group row">
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Whether Registration is Needed?</label><br>
                                  @if($schedules->registration_status==1)
                                    @php
                                      echo "Yes";
                                    @endphp
                                  @else
                                    @php
                                      echo "No";
                                    @endphp
                                  @endif
                              </div>
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Question and Answers for Training</label><br>
                                  @if($schedules->question_answer_status==1)
                                    @php
                                      echo "Yes";
                                    @endphp
                                  @else
                                    @php
                                      echo "No";
                                    @endphp
                                  @endif
                              </div>
                              <div class="col-3">
                                  <label for="exampleInputEmail1">Reviews and Feedback Needed?</label><br>
                                  @if($schedules->review_status==1)
                                    @php
                                      echo "Yes";
                                    @endphp
                                  @else
                                    @php
                                      echo "No";
                                    @endphp
                                  @endif                                  
                              </div>
                              <div class="col-3">
                                <label for="exampleInputEmail1">Training Certificates Needed?</label><br>
                                  @if($schedules->training_certificates==1)
                                    @php
                                      echo "Yes";
                                    @endphp
                                  @else
                                    @php
                                      echo "No";
                                    @endphp
                                  @endif   
                              </div>
                          </div>
                          <div id="listroledata" style="padding: 10px;">
                              <table class="table table-hover table-bordered table-striped datatable" id="dept_details" width="100%">
                                  <thead>
                                      <tr class="success">
                                        @foreach($training_date as $training_date_val)
                                        <th width="20%">
                                          {{$training_date_val}}
                                        </th>
                                        @endforeach
                                      </tr>
                                  </thead>
                                  <tbody>
                                    @php
                                      $n= 0;
                                      $training_date_new ="";
                                    @endphp
                                    <tr>
                                      @foreach($training_date as $training_date_val)
                                        @php
                                          $date_training_value = date("Y-m-d", strtotime($training_date_val));

                                          if($training_date_new)
                                          {
                                            $training_date_new = $training_date_new.",".$date_training_value;
                                          }
                                          else
                                          {
                                            $training_date_new = $date_training_value;
                                          }
                                        @endphp
                                        <script type="text/javascript">
                                          var tdate = "<?=$date_training_value?>"
                                          training_date_arr.push(tdate);
                                        </script>
                                      <td width="20%">
                                        @php
                                          $schedule_topic_str = "";
                                          $schedule_topic_id = "";
                                        @endphp
                                        @if(isset($schedule_topics))
                                            @foreach($schedule_topics as $schedule_topic)
                                                @php
                                                    $schedule_topic_id = $schedule_topic->topic_id;
                                                    if($schedule_topic_str)
                                                    {
                                                        $schedule_topic_str = $schedule_topic_str.",".$schedule_topic_id;
                                                    }
                                                    else
                                                    {
                                                        $schedule_topic_str = $schedule_topic_id;
                                                    }
                                                @endphp
                                            @endforeach
                                        @endif
                                        <select name="{{$date_training_value}}topic_id[]" id="{{$date_training_value}}topic_id" class="form-control" required multiple="multiple" data-placeholder="Select Topics">
                                            @foreach($topics as $topic)
                                              @php
                                                  $topic_id = $topic->id;
                                              @endphp
                                              @if(str_contains($schedule_topic_str, $topic_id))
                                                <option value="{{$topic->id}}" selected>{{$topic->topic_name}}</option>
                                              @else
                                                <option value="{{$topic->id}}">{{$topic->topic_name}}</option>
                                              @endif
                                            @endforeach
                                        </select>
                                      </td>
                                      @endforeach 

                                    </tr>                                 
                                  </tbody>
                              </table>
                          </div>                         
                      </div>
                  </div>
                  <!-- /.card-body -->
                  <div class="card-footer">
                    <button type="submit" class="btn btn-primary">Submit</button>
                    <input type="hidden" name="training_date_new" id="training_date_new" value="{{$training_date_new}}">
                  </div>
                </form>
            </div>
        </div>        
    </div>
    <script type="text/javascript">
      $(document).ready(function() {
        for(i=0; i<training_date_arr.length; i++)
        {
            training_date = training_date_arr[i];
            $("#"+training_date+"topic_id").select2({
                columns: 1,
                placeholder : "Select Topics",
                allowClear: true,
                search: true,
                selectAll: true
            });
        }  
      });
    </script>
@stop
