@php
  // Import the necessary classes
  use Illuminate\Support\Facades\File;
  use Carbon\Carbon;
  // Define the folder path where the PDF files are located
  $folderPath = public_path('uploads/current-affairs');
  // Get all the PDF files in the folder
  $files = File::glob($folderPath . '/*.pdf');
  // Initialize an empty array to store the processed data
  $data = [];
  // Initialize an error message variable
  $errorMessage = null;
  try {
      // Define the pattern to match the date range in the filename
      $pattern = '/(\d{4}-\d{2}-\d{2}(?:_\d{4}-\d{2}-\d{2})*)/';
      // Iterate over each file
      foreach ($files as $file) {
          // Get the filename without the extension
          $filename = pathinfo($file)['filename'];
          // Extract the date range from the filename using regex matching
          if (preg_match($pattern, $filename, $matches)) {
              $dateRange = $matches[1];
              // Split the date range into individual dates
              $dates = array_map(function ($date) {
                  // Parse the date string into a Carbon instance and format it
                  return Carbon::createFromFormat('Y-m-d', $date)->format('d M Y');
              }, explode('_', $dateRange));
              // Add the processed data to the array
              $data[] = [
                  'date_range' => implode(' - ', $dates),
                  'url' => asset(rawurlencode('/uploads/current-affairs/' . basename($file))),
              ];
          }
      }
      //  Reverse the array
      $data = array_reverse($data);
      // Define the number of items per page
      $perPage = 10;
      // Use the `array_slice` function to paginate the array
      $currentPage = request()->get('page', 1); // Get the current page from the URL
      $paginatedData = array_slice($data, ($currentPage - 1) * $perPage, $perPage);
      // Convert the paginated data to a collection for easier pagination
      $collection = collect($paginatedData);
      // Create a LengthAwarePaginator instance
      $paginator = new \Illuminate\Pagination\LengthAwarePaginator(
          $collection,
          count($data),
          $perPage,
          $currentPage,
          ['path' => request()->url()], // Set the path for the paginator
      );
  } catch (\Exception $e) {
      // If an exception occurs, capture the error message
      $errorMessage = $e->getMessage();
  }
@endphp
@extends('frontend.mainlayout') @section('title', 'Current Affairs - RERF Group of Institutions') @section('banner')
<style>
  .table-responsive table thead tr th:nth-child(1),
  .table-responsive table tbody tr td:nth-child(1) {
    text-align: center;
  }

  .table-responsive table tbody tr td:nth-child(2),
  .table-responsive table tbody tr td:nth-child(3) {
    min-width: 270px;
    text-wrap: pretty;
  }

  @media (max-width: 767px) {

    .table>tbody>tr>td,
    .table>tbody>tr>th,
    .table>tfoot>tr>td,
    .table>tfoot>tr>th,
    .table>thead>tr>td,
    .table>thead>tr>th {
      padding: 0.75rem 1rem !important;
    }
  }

  .table {
    margin-bottom: 10PX;
  }

  .pagination .page-item .page-link {
    color: #818181;
  }

  .pagination .page-item.active .page-link {
    background-color: rgba(22, 22, 22, 0.1);
    border-color: transparent;
    color: #6e6e6e;
  }
</style>
<section class="color-1 background-10 font-1 fw-400 py-6">
  <div class="container">
    <div class="row">
      <div class="col-lg-8">
        <div class="fs--1 font-1 mb-2">
          <a class="mr-2" href="/home">Home</a><span class="fa fa-angle-double-right color-6"></span>
          <a class="mx-2">Current Affairs</a>
        </div>
        <h1 class="fs-3 fs-sm-4 fw-400">Current Affairs</h1>
        <p class="lead color-4">Daily Current Affairs Synopsis</p>
      </div>
    </div>
    <!--/.row-->
  </div>
  <!--/.container-->
</section>
@endsection
@section('main')
<section class="py-5">
  <div class="container">
    <div class="row font-1">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="col-lg-12"> {{-- <h4>— Daily Current Affairs Synopsis</h4> --}}
          <div class="my-4"></div>
          <div class="table-responsive">
            <table class="table">
              <thead>
                <tr>
                  <th>Sl No</th>
                  <th>Topic</th>
                  <th>Date </th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
                @foreach ($paginator as $item)
                  <tr>
                    <td>{{ $loop->iteration + ($paginator->currentPage() - 1) * $paginator->perPage() }}</td>
                    <td>Daily Current Affairs Synopsis</td>
                    <td>{{ $item['date_range'] }}</td>
                    <td>
                      <a class="btn btn-icon btn-secondary btn-icon-left" href="{{ $item['url'] }}" target="_blank">
                        <span class="icon-File-Download"></span> View/Download
                      </a>
                    </td>
                  </tr>
                @endforeach
              </tbody>
            </table>
          </div>
          <div class="col d-flex justify-content-center align-items-center"> {{ $paginator->links() }}</div>
        </div>
      </div>
    </div>
  </div>
</section>
@endsection
