<x-app-layout>
    {{-- <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Dashboard') }}
        </h2>
    </x-slot> --}}


    <div class="py-4">
        <div class="container-fluid">
            @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
            <div class="app-content content">
                <div class="details-wrapper">
                    <div class="content-body">
                        <section id="configuration">
                            <div class="row">
                                <div class="col-12">
                                    <div class="card">
                                        <div class="card-header">
                                            <h4 class="card-title">Access Requests</h4>
                                            <div class="cntrolBtn">
                                                <a class="btn btn-outline-primary @if($status == 'Pending') active @endif" href="{{ url('requestaccesslist/Pending') }}">Pending @if(isset($pendingrequestCount) && $pendingrequestCount!=0)({{$pendingrequestCount}})@endif</a>
                                                <a class="btn btn-outline-primary @if($status == 'Approved') active @endif" href="{{ url('requestaccesslist/Approved') }}">Approved @if(isset($approvedrequestCount) && $approvedrequestCount!=0)({{$approvedrequestCount}})@endif</a>
                                                <a class="btn btn-outline-primary @if($status == 'Rejected') active @endif" href="{{ url('requestaccesslist/Rejected') }}">Rejected @if(isset($rejectedrequestCount) && $rejectedrequestCount!=0)({{$rejectedrequestCount}})@endif</a>
                                            </div>
                                        </div>
                                        <div class="card-content collapse show">
                                            <div class="card-body card-dashboard">
                                                <div class="table-responsive">
                                                    <table class="table yajra-datatable">
                                                        <thead>
                                                            <tr>
                                                                <th class="slNo">Sl No.</th>
                                                                <th>User Name</th>
                                                                <th>Company Name</th>
                                                                <th class="docTh">Doc Name</th>
                                                                <th>Email</th>
                                                                <th>Current Access Level</th>
                                                                <th>Type of Access</th>
                                                                <th>Remark</th>
                                                                @if($status == 'Approved' || $status == 'Rejected')
                                                                <th>Date and time of Approval/Rejection</th>
                                                                @endif
                                                                @if($status == 'Rejected')
                                                                <th>Rejection Note</th>
                                                                @endif
                                                                @if(Auth::user()->role_id == '1')
                                                                <th class="slNo text-right">Action</th>
                                                                @endif
                                                            </tr>
                                                        </thead>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal fade signin-up" id="requestaccessmodal" 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 headerHeight RequestModel">

                                    <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 pt-2" id="requestmodalbody">
                                    </div>
                                </div>
                              </div>
                            </div>
                        </section>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript" src="{{ asset('js/datatables.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/datatable-basic.js') }}"></script>
    <script type="text/javascript">
        $(function () {
            
            var table = $('.yajra-datatable').DataTable({
                autoWidth: false,
                processing: true,
                serverSide: true,
                ajax: "{{ route('requests.requestaccesslist',$status) }}",
                columns: [
                    {data: 'DT_RowIndex', name: 'DT_RowIndex'},
                    {data: 'name', name: 'name'},
                    {data: 'companyname', name: 'companyname'},
                    {data: 'docname', name: 'docname'},
                    {data: 'email', name: 'email'},
                    {data: 'access_level', name: 'access_level'},
                    {data: 'general_access', name: 'general_access'},
                    {data: 'remark', name: 'remark'},
                    <?php if($status == 'Approved' || $status == 'Rejected') { ?>
                    {data: 'date', name: 'date'},
                    <?php } ?>
                    <?php if($status == 'Rejected') { ?>
                    {data: 'note', name: 'note'},
                    <?php } ?>
                    <?php if( Auth::user()->role_id == '1') { ?>
                    {
                        data: 'action', 
                        name: 'action', 
                        orderable: true, 
                        searchable: true,
                        class:'text-right'
                    },
                    <?php } ?>
                ]
            });
            
        });
        $(document).ready(function(){
            if( $(".alert-dismissible").length > 0 ){
                setTimeout(function(){
                    $(".alert-dismissible").fadeOut('slow');
                },3000);
            }


            var adminHeader = $("nav").height();
            $(".headerHeight").css("margin-top", adminHeader);
        
            
        });

        function viewRequest(id){
            $.ajax({
                type: "POST",
                url: "{{url('request/view')}}",
                data: 
                {'id':id,"_token": "{{ csrf_token() }}"},
                dataType : 'json',
                success: function(data) {
                    if(data.success==1)
                    {
                         $("#requestaccessmodal").modal('show');
                         $("#requestmodalbody").html(data.view);
                    }

                    }
                     });
        }
        function approveRequestAcess(id){
            $(".error").remove();
            if( $("#access_level").val() != '' ){
                swal({
                    title: "Are you sure you want to approve this request?",
                    text: "",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonClass: "btn-danger",
                    confirmButtonText: "OK",
                    closeOnConfirm: true,
                    showLoaderOnConfirm: false
              },
              function(){    
                $.ajax({
                    type: "POST",
                    url: "{{ URL::to('request/approve/{id}')}}",
                    data: 
                    {'formdata': $("#requestaccess").serialize(),'id':id,"_token": "{{ csrf_token() }}"},
                    dataType : 'json',
                    success: function(data) {
                        if(data.success==1)
                        {
                            swal("Request approved successfully",'','success');
                            window.location.reload();
                        }

                        }
                         });
                });
            }else{
                $('<label class="error">Please select file access level.</label>').insertAfter($("#access_level"));
            }
        }

        function validateAccessLevel(){
            $(".error").remove();
            if( $("#access_level").val() == '' ){
                 $('<label class="error">Please select file access level.</label>').insertAfter($("#access_level"));
            }
        }
        function approveFileRequestAcess(id,priority){
            swal({
                title: "Are you sure you want to approve this request?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-danger",
                confirmButtonText: "OK",
                closeOnConfirm: true,
                showLoaderOnConfirm: false
          },
          function(){    
            $.ajax({
                type: "POST",
                url: "{{ URL::to('filerequest/approve/{id}')}}",
                data: 
                {'formdata': $("#requestaccess").serialize(),'id':id,"_token": "{{ csrf_token() }}"},
                dataType : 'json',
                success: function(data) {
                    if(data.success==1)
                    {
                        swal("Request approved successfully",'','success');
                        window.location.reload();
                    }

                    }
                     });
            });
        
        }
        function rejectRequestAcess(id){
            swal({
                title: "Are you sure you want to reject this request?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-danger",
                confirmButtonText: "OK",
                closeOnConfirm: true,
                showLoaderOnConfirm: false
          },
          function(){    
            $.ajax({
                type: "POST",
                url: "{{ URL::to('request/reject/{id}')}}",
                data: 
                {'formdata': $("#requestaccess").serialize(),'id':id,"_token": "{{ csrf_token() }}"},
                dataType : 'json',
                success: function(data) {
                    if(data.success==1)
                    {
                        swal("Request rejected successfully",'','success');
                        window.location.reload();
                    }

                    }
                     });
            });
        }
        function rejectFileRequestAcess(id){
            swal({
                title: "Are you sure you want to reject this request?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-danger",
                confirmButtonText: "OK",
                closeOnConfirm: true,
                showLoaderOnConfirm: false
          },
          function(){    
            $.ajax({
                type: "POST",
                url: "{{ URL::to('filerequest/reject/{id}')}}",
                data: 
                {'formdata': $("#requestaccess").serialize(),'id':id,"_token": "{{ csrf_token() }}"},
                dataType : 'json',
                success: function(data) {
                    if(data.success==1)
                    {
                        swal("Request rejected successfully",'','success');
                        window.location.reload();
                    }

                    }
                     });
            });
            
        }
    </script>
</x-app-layout>