@extends('master')
@section('content')
<style>
  .catergories::before {
    background: #fff;
  }
  .category-item {
    box-shadow: 0px 3.59817px 28.7854px rgba(0, 0, 0, 0.1);
  }
  .modal {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1050;
    display: none;
    width: 100%;
    height: 100%;
    overflow: auto;
    outline: 0;
}
</style>
{{-- <x-slot name="header">
    <h2 class="font-semibold text-xl text-gray-800 leading-tight">
        {{ __('Dashboard') }}
    </h2>
</x-slot> --}}


@if (session()->has('message'))
    <div class="alert alert-icon-right alert-success alert-dismissible mt-2" role="alert">
        <span class="alert-icon">
            <i class="la la-info"></i>
        </span>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">×</span>
        </button>
        <strong>{{ session('message') }}</strong>
    </div>
@endif
<section class="banner bg-img-none">
    <img src="{{ asset('img/frontend/banner-data.jpg') }}" alt="Banner image">
    <div class="banner-content">
      <div class="container">
        <div class="row">
          <div class="col-12 col-sm-10 col-md-9 col-lg-7 col-xl-6">
            <h1>Data Repository</h1>
          </div>
        </div>
      </div>
    </div>
  </section>

@include('frontend/filter')

  <section class="downloads-details mobileFilterHeader pb-4">
    <div class="container">
      <h2>All Data Repository</h2>
      <div class="filterIcon" onclick="showfilterModal();" class="btn btn-primary" data-toggle="modal">
        <svg width="17" height="15" viewBox="0 0 17 15" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M16 0.5H1C0.867392 0.5 0.740215 0.552679 0.646447 0.646447C0.552678 0.740215 0.5 0.867392 0.5 1V1.835C0.500093 1.95309 0.523557 2.07 0.569038 2.17898C0.61452 2.28797 0.68112 2.38687 0.765 2.47L6.5 8.29V13.39L7.5 13.77V8C7.50038 7.9342 7.48777 7.86897 7.46289 7.80805C7.438 7.74713 7.40134 7.69172 7.355 7.645L1.5 1.795V1.5H15.5V1.805L9.665 7.645C9.61503 7.69003 9.57467 7.74468 9.54631 7.80568C9.51796 7.86668 9.50221 7.93277 9.5 8V14.605L10.5 15V8.25L16.235 2.5C16.3202 2.41476 16.3876 2.31336 16.4331 2.20174C16.4786 2.09012 16.5013 1.97053 16.5 1.85V1C16.5 0.867392 16.4473 0.740215 16.3536 0.646447C16.2598 0.552679 16.1326 0.5 16 0.5Z" fill="white"/>
        </svg>
      </div>
    </div>
  </section>
  @if(!empty( $_GET ) && ((isset($_GET['categoryid']) && $_GET['categoryid'] != '')))
  <section class="downloads-details filterTitle">
    <div class="container">
      <div class="d-flex flex-wrap align-items-center">
          <a class="category-item boxLifting m-0" href="#">
            <span>
              @if(isset($_GET['categoryid']) && $_GET['categoryid'] != '')
               @if($categories[$_GET['categoryid']]['logo_image_path'] != '')  
                <img src="{{$path.$categories[$_GET['categoryid']]['logo_image_path']}}" alt=""> 
              @else
                <img src="{{ asset('img/frontend/default-category.svg') }}" alt="">
              @endif 
            </span>
            @if($categories[$_GET['categoryid']]['bg_image_path'] != '')
                <img class="fixed-category-img" src="{{$path.$categories[$_GET['categoryid']]['bg_image_path']}}" alt="">
            @else
                <img class="fixed-category-img" src="{{ asset('img/frontend/bg-image.png') }}" alt="">
            @endif
          </a>
          <h1>{{$categories[$_GET['categoryid']]['categoryname']}}</h1>
          @elseif(isset($_GET['subcategoryid']) && $_GET['subcategoryid'] != '' && isset($categories[$_GET['subcategoryid']]['sub_of']) && isset($categories[$categories[$_GET['subcategoryid']]['sub_of']]) && isset($categories[$categories[$_GET['subcategoryid']]['sub_of']]['logo_image_path']))
               @if($categories[$categories[$_GET['subcategoryid']]['sub_of']]['logo_image_path'] != '')  
                <img src="{{$path.$categories[$categories[$_GET['subcategoryid']]['sub_of']]['logo_image_path']}}" alt="">
              @else
                <img src="{{ asset('img/frontend/default-category.svg') }}" alt="">
              @endif 
            </span>
            @if($categories[$categories[$_GET['subcategoryid']]['sub_of']]['bg_image_path'] != '')
                <img class="fixed-category-img" src="{{$path.$categories[$categories[$_GET['subcategoryid']]['sub_of']]['bg_image_path']}}" alt="">
            @else
                <img class="fixed-category-img" src="{{ asset('img/frontend/bg-image.png') }}" alt="">
            @endif
          </a>
          <h1>{{$categories[$categories[$_GET['subcategoryid']]['sub_of']]['categoryname']}}</h1>
          @endif
      </div>
    </div>
  </section>
 @endif
  <section class="category-details downloads-details pt-0">
    <div class="container">
      <div class="row">
        <div class="col-12 all-listing">
          @forelse($result as $results)
          <?php $hasdownload = 0; ?>
                        @if(Session::has('user.userID'))
                          @if($results->doc_priority==1 || isset( $results['selectedUsers'][Session::get('user.userID')] ))
                            <?php $hasdownload = 1; ?>
                          @elseif($results->doc_priority==2 || isset( $results['selectedUsers'][Session::get('user.userID')] ))
                            @if($userdetails->access_level =='M' || $userdetails->access_level == 'H')
                                <?php $hasdownload = 1; ?>
                            @endif
                          @elseif($results->doc_priority==3)
                            @if($userdetails->access_level == 'H')
                               <?php $hasdownload = 1; ?>
                            @endif 
                          @elseif($results->doc_priority==4)
                            @if( isset($results['selectedUsers']) && !empty( $results['selectedUsers'] ) )
                              @if( isset( $results['selectedUsers'][Session::get('user.userID')] ) )
                                <?php $hasdownload = 1; ?>
                              @endif
                            @endif
                          @endif
                        @else
                            @if($results->doc_priority==1)
                              <?php $hasdownload = 1; ?>
                            @endif
                        @endif
          <div class="cata-content py-4 py-sm-5">
            <h5>{{$results->title}}</h5>

            <div class="subHead pb-2">
              @if( isset( $categories[$results->category_id] ))
                @if( $categories[$results->category_id]['sub_of'] == NULL )
                  <div class="subItems" data-toggle="tooltip" data-placement="bottom" title="Document Category"><img src="{{ asset('img/frontend/category.svg') }}" alt=""><span>{{$categories[$results->category_id]['categoryname']}}</span></div>
                @else
                  <div class="subItems" data-toggle="tooltip" data-placement="bottom" title="Document Category"><img src="{{ asset('img/frontend/category.svg') }}" alt="" ><span>@if(isset($categories[$categories[$results->category_id]['sub_of']]['categoryname'])){{$categories[$categories[$results->category_id]['sub_of']]['categoryname']}} @endif</span></div>
                  <div class="subItems" data-toggle="tooltip" data-placement="bottom" title="Document Sub-Category"><img src="{{ asset('img/frontend/sub-cat.svg') }}" alt="" ><span>@if(isset($categories[$results->category_id]['categoryname'])){{$categories[$results->category_id]['categoryname']}} @endif</span></div>
                @endif
              @endif

               <div class="subItems" data-toggle="tooltip" data-placement="bottom" title="Document Classification"><img src="{{ asset('img/frontend/file.svg') }}" alt="" ><span>{{$fileAcces[$results->doc_priority]}}</span> </div>
               
               <div class="subItems" data-toggle="tooltip" data-placement="bottom" title="Document Published Date"><img src="{{ asset('img/frontend/date.svg') }}" alt="" ><span>@if(!empty($results->publish_date) && isset($results->publish_date)){{date("d M Y", strtotime($results->publish_date))}} @else -- @endif</span></div>
            </div>
          
             <div id="description_{{$results->id}}">{!!substr( strip_tags($results->description),0,400) !!} @if(strlen ( $results->description) > 400)<a class="readmore" href="javascript:void(0)" onclick="readMore('{{$results->id}}');">View More</a> @endif</div>

             <div id="fulldescripton_{{$results->id}}"  class="listDecs" style="display:none;" >{!! $results->description !!}<a class="readmore pl-1" href="javascript:void(0)" onclick="readLess('{{$results->id}}');"> View Less</a></div>


            <div class="row mt-3">
              <div class="col-12 mb-3">
                @if($results->doc_path != '')
                  <a @if( $hasdownload== 1 ) href="{{url('download/'.$results->id)}}" @elseif(Session::has('user.userID')) onclick="showAlert('{{$results->id}}')" href="javascript:void(0)" @elseif(!Session::has('user.userID')) onclick="showLoginAlert()" href="javascript:void(0)" @endif ><svg width="14" height="16" viewBox="0 0 14 16" fill="none"xmlns="http://www.w3.org/2000/svg"><path
                  d="M12.8334 5.5H9.50004V0.5H4.50004V5.5H1.16671L7.00004 12.1667L12.8334 5.5ZM0.333374 13.8333H13.6667V15.5H0.333374V13.8333Z"
                  fill="#2B7DF7" /></svg>{{$results->doc_name}}</a>
                @endif   
              </div>
              @if(ENVIRONMENT == 'Development')
               @if(Session::has('user.userID') )
              <div class="col-12">
                <div class="dowload-details">
                  <span>Downloaded Count : <span class="downResult"> @if( !empty( $downloadCounts ) && isset($downloadCounts[$results->id]) ) {{$downloadCounts[$results->id]['total']}} @else -- @endif</span></span> <sab>|</sab> <span>Last Downloaded :<span class="downResult"> @if( !empty( $downloadCounts ) && isset($downloadCounts[$results->id]) ) {{date("d M Y - g:i A", strtotime($downloadCounts[$results->id]['lastdownload']))}} @else -- @endif</span></span> 
                  <!-- <sab>|</sab> <span class="ModelDownload"> <a href="javascript:void(0)">Download Summary</a></span> -->
                </div>
              </div>
              @endif
              @endif

          </div>
        </div>
          @empty
            <div class="w-100 pt-3">
              <div class="no-data mb-4">
                  <i class="fas fa-exclamation-triangle"></i>
                  <h5>No records found</h5>
              <div>
          </div>
          @endforelse
          {{$result->links("pagination::bootstrap-4")}}
        </div>   
      </div>
    </div>
<div class="modal fade signin-up" id="noaccessModal" tabindex="-1" role="dialog" aria-labelledby="signinmodalLabel"
  aria-hidden="true">
  <div class="modal-dialog modal-md modal-dialog-centered" role="document">
    <div class="modal-content border20 headerHeight">
      <div class="modal-header onlyClose">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true"><img src="{{ asset('img/frontend/close.png') }}" alt="Close"></span>
        </button>
      </div>
      <div class="modal-body">
        <form method="POST" class="edit-form" id="requestaccess" enctype="multipart/form-data">
          @csrf
          <div class="signin-block registration">
              <div class="NoAccess text-center">
                <div class="alert alert-danger tc errmsg" style="display: none"></div>
                <img class="mb-2" src="{{ asset('img/frontend/warning.png') }}" alt="Close">
                <h1 class="mb-4">No Access</h1>
                <p class="mb-4">You don't have access to this File<!-- </br>Please Request Access --></p>
                <button type="button" class="btn btn-primary w-100" id="submitbutton" onclick="closeModal();">OK</button>
              </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<div class="modal fade signin-up" id="successModal" tabindex="-1" role="dialog" aria-labelledby="signinmodalLabel"
  aria-hidden="true">
  <div class="modal-dialog modal-md modal-dialog-centered" role="document">
    <div class="modal-content border20 headerHeight">
      <div class="modal-header onlyClose">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true"><img src="{{ asset('img/frontend/close.png') }}" alt="Close"></span>
        </button>
      </div>
      <div class="modal-body">
        <form method="POST" class="edit-form" id="requestaccess" enctype="multipart/form-data">
          @csrf
          <div class="signin-block registration">
              <div class="NoAccess text-center">
                <div class="alert alert-danger tc errmsg" style="display: none"></div>
                <img class="mb-2" src="{{ asset('img/frontend/success.png') }}" alt="Close">
                <h1 class="mb-4">Successful</h1>
                <p class="mb-4">Your request Succesfully Submitted.</br> We will send an email regarding permission </br>access in  shortly. </p>
                <button type="button" class="btn btn-primary w-100" id="" >OK</button>
              </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<div class="modal fade signin-up" id="submitaccessModal" tabindex="-1" role="dialog" aria-labelledby="signinmodalLabel"
  aria-hidden="true">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content border20 headerHeight">
      <div class="modal-header onlyClose">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true"><img src="{{ asset('img/frontend/close.png') }}" alt="Close"></span>
        </button>
      </div>
      <div class="modal-body" id="appendform">
    
        </form>
      </div>
    </div>
  </div>
</div>
  </section>
  <script type="text/javascript">
  function showAlert(id){
      $('#noaccessModal').modal('show');
      //$("#submitbutton").attr('onclick','requestAcess('+id+')');
  }
  function showLoginAlert(){
    swal("Please sign in to the system to access this file.");
  }
  function showfilterModal(){
        $("#filterModal").modal('show');
  }   
  function closeModal(){
    $('#noaccessModal').modal('hide');
  }
  function submitRequestAcess(id){
    $.ajax({
                type: "POST",
                url: "{{ URL::to('/filerequestaccess')}}",
                data: 
                {'formdata': $("#submitaccess").serialize(),'datarepo_id':id,"_token": "{{ csrf_token() }}"},
                dataType : 'json',
                success: function(data) {
                    if(data.success==1)
                    {
                        $("#successModal").modal('show');
                        
                        $("#submitaccessModal").modal('hide');
                    }

                    }
                });
    $('#noaccessModal').modal('hide');
    $("#submitaccessModal").modal('show');
  }  

  function requestAcess(id){
    $.ajax({
                type: "POST",
                url: "{{ URL::to('/requestaccessform')}}",
                data: 
                {'datarepo_id':id,"_token": "{{ csrf_token() }}"},
                dataType : 'json',
                success: function(data) {
                    if(data.success==1)
                    {
                        $('#appendform').html(data.view);
                    }

                    }
                });
    $('#noaccessModal').modal('hide');
    $("#submitaccessModal").modal('show');
  }
  function readMore(id){
    $("#description_"+id).hide();
    $("#fulldescripton_"+id).show();
  }
  function readLess(id){
    $("#fulldescripton_"+id).hide();
    $("#description_"+id).show();
    
  }
  </script> 
  <script>
    $(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
  </script>
    @stop