@extends('backend.layouts.app')

@section('content')
<style>
    .remove-icon{
        cursor: default !important;
    }
    .ui-autocomplete {
     z-index: 1 !important;
}
</style>
    <div class="row page-titles">
        <div class="col-md-5 align-self-center">
            <h4 class="text-themecolor"> Access Limits</h4>
        </div>
        <div class="col-md-7 align-self-center text-right">
            <div class="d-flex justify-content-end align-items-center">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Ip Manager</li>
                    <li class="breadcrumb-item active">Access Limits</li>
                </ol>             
            </div>
        </div>
    </div>
    <div class="container-fluid">       
      {{-- Status model --}}
        <div id="statusModal" class="modal fade" role='dialog'>
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body">
                        <p>Are you sure you want to change the status?</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        <span id='changestatus'></span>
                    </div>
                </div>
            </div>
        </div>  



        <div id="editsModal" class="modal fade" role='dialog'>
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Edit Rule</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <form id="passwordForm" action="{{ route('ips.access.rule.update') }}" method="POST">
                            @csrf
                            <input type="hidden" name="rule_id" id="rule_id">

                            <div class="form-group">
                                <label for="limit">Limit</label>
                                <div class="input-group">
                                    <input id="limit" type="number"  class="form-control @error('limit') is-invalid @enderror"  name="limit" max="50" min="1"  onkeypress="return event.charCode >= 48 && event.charCode <= 57">                                  
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="dymic_title" id="dymic_title">Limit</label>
                                <div class="input-group">
                                    <input id="period_value" type="period_value"  class="form-control @error('period_value') is-invalid @enderror"  name="period_value" max="50" min = "1"  onkeypress="return event.charCode >= 48 && event.charCode <= 57">                                  
                                </div>
                            </div>

                            <div class="modal-footer">
                                <button type="submit" class="btn btn-primary">Update Limit</button>
                                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="content-wrapper">
            @if (session('status'))
                <div class="alert alert-success alert-dismissible fade show" role="alert"><i
                        class="far fa-check-circle"></i>
                    {{ session('status') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif
            @if (session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert"><i
                        class="far fa-check-circle"></i>
                    {{ session('error') }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            @endif

            <section class="content">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">                                               
                                <div class="table-responsive">
                                    <h4><b>Page first to second</b></h4>
                                    <table id="user" class="table table-bordered table-hover">
                                        <thead>
                                            <th>S. No</th>
                                            <th>For</th>
                                            <th>Limit</th>
                                            <th>Period</th>
                                            <th>@sortablelink('status', 'Status')</th>
                                            <th>Actions</th>
                                        </thead>
                                        <tbody id="memberTable">
                                            @if (count($pagefirstsecond) > 0)
                                                @php
                                                    $i = 0 ;
                                                @endphp
                                                @foreach ($pagefirstsecond as $index => $rule)
                                           
                                                    <tr>                                                       
                                                        <td>{{ ++$i }}</td>
                                                        
                                                        <td>{{ Str::replace('_', ' ', Str::upper($rule->type)) }}</td>
                                                        @if($rule->type!="foreign_ip")
                                                        <td>{{ $rule->limit }}</td>
                                                        <td>{{ $rule->period_value }} {{ Str::ucfirst($rule->period) }}s</td>
                                                        @else
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        @endif
                                                        <td>
                                                            <button type="button"
                                                                class="btn btn-{{ $rule->status == 1 ? 'success' : 'danger' }} btn-sm change-status-btn" data-bs-toggle="modal" data-bs-target="#statusModal"
                                                                data-ip="{{ $rule->ip }}" data-status="{{ $rule->status }}"
                                                                onclick="myFunction({{ $rule->id }});"
                                                                data-id="{{ $rule->id }}">
                                                                <span>{{ $rule->status == 1 ? 'Active' : 'In active' }}</span>
                                                            </button>
                                                        </td>
                                                          @if($rule->type!="foreign_ip")
                                                        <td>
                                                            <button class="btn btn-warning btn-sm" title="Edit Data"
                                                                    onclick='showEditModal({{ $rule->id }}, {{ $rule->limit }}, {{ $rule->period_value }}, "{{ Str::ucfirst($rule->period) }}")'><i
                                                                        class="fas fa-edit"></i></button>
                                                        </td>    
                                                        @else
                                                        <td></td>
                                                        @endif                                                    
                                                    </tr> 
                                                
                                                @endforeach
                                            @else
                                                <tr>
                                                    <td colspan="12">
                                                        <h6>
                                                            <center>No Records Found</center>
                                                        </h6>
                                                    </td>
                                                </tr>
                                            @endif
                                        </tbody>
                                    </table>

                                    <h4><b>Page second to third</b></h4>
                                    <table id="user" class="table table-bordered table-hover">
                                        <thead>
                                            <th>S. No</th>
                                            <th>For</th>
                                            <th>Limit</th>
                                            <th>Period</th>
                                            <th>@sortablelink('status', 'Status')</th>
                                            <th>Actions</th>
                                        </thead>
                                        <tbody id="memberTable">
                                            @if (count($pagesecondThird) > 0)
                                                @php
                                                    $i = 0 ;
                                                @endphp
                                                @foreach ($pagesecondThird as $index => $rule)
                                           @if($index<3)
                                                    <tr>                                                       
                                                        <td>{{ ++$i }}</td>
                                                        <td>{{ Str::upper($rule->type) }}</td>
                                                        <td>{{ $rule->limit }}</td>
                                                        <td>{{ $rule->period_value }} {{ Str::ucfirst($rule->period) }}s</td>
                                                        <td>
                                                            <button type="button"
                                                                class="btn btn-{{ $rule->status == 1 ? 'success' : 'danger' }} btn-sm change-status-btn" data-bs-toggle="modal" data-bs-target="#statusModal"
                                                                data-ip="{{ $rule->ip }}" data-status="{{ $rule->status }}"
                                                                onclick="myFunction({{ $rule->id }});"
                                                                data-id="{{ $rule->id }}">
                                                                <span>{{ $rule->status == 1 ? 'Active' : 'In active' }}</span>
                                                            </button>
                                                        </td>
                                                        <td>
                                                            <button class="btn btn-warning btn-sm" title="Edit Data"
                                                                    onclick='showEditModal({{ $rule->id }}, {{ $rule->limit }}, {{ $rule->period_value }}, "{{ Str::ucfirst($rule->period) }}")'><i
                                                                        class="fas fa-edit"></i></button>
                                                        </td>                                                        
                                                    </tr> 
                                                @endif
                                                @endforeach
                                            @else
                                                <tr>
                                                    <td colspan="12">
                                                        <h6>
                                                            <center>No Records Found</center>
                                                        </h6>
                                                    </td>
                                                </tr>
                                            @endif
                                        </tbody>
                                    </table>


                                       <h4><b>Email</b></h4>
                                    <table id="user" class="table table-bordered table-hover">
                                        <thead>
                                            <th>S. No</th>
                                            <th>For</th>
                                            <th>Limit</th>
                                            <th>Period</th>
                                            <th>@sortablelink('status', 'Status')</th>
                                            <th>Actions</th>
                                        </thead>
                                        <tbody id="memberTable">
                                            @if (count($emailData) > 0)
                                                @php
                                                    $i = 0 ;
                                                @endphp
                                                @foreach ($emailData as $index => $rule)
                                                @if($index<3)
                                                
                                                    <tr>                                                       
                                                        <td>{{ ++$i }}</td>
                                                        <td>{{ Str::replace('_', ' ', Str::upper($rule->type)) }}</td>
                                                        <td>{{ $rule->limit }}</td>
                                                        <td>{{ $rule->period_value }} {{ Str::ucfirst($rule->period) }}s</td>
                                                        <td>
                                                            <button type="button"
                                                                class="btn btn-{{ $rule->status == 1 ? 'success' : 'danger' }} btn-sm change-status-btn" data-bs-toggle="modal" data-bs-target="#statusModal"
                                                                data-ip="{{ $rule->ip }}" data-status="{{ $rule->status }}"
                                                                onclick="myFunction({{ $rule->id }});"
                                                                data-id="{{ $rule->id }}">
                                                                <span>{{ $rule->status == 1 ? 'Active' : 'In active' }}</span>
                                                            </button>
                                                        </td>
                                                        <td>
                                                            <button class="btn btn-warning btn-sm" title="Edit Data"
                                                                    onclick='showEditModal({{ $rule->id }}, {{ $rule->limit }}, {{ $rule->period_value }}, "{{ Str::ucfirst($rule->period) }}")'><i
                                                                        class="fas fa-edit"></i></button>
                                                        </td>                                                        
                                                    </tr> 
                                                @endif
                                                @endforeach
                                            @else
                                                <tr>
                                                    <td colspan="12">
                                                        <h6>
                                                            <center>No Records Found</center>
                                                        </h6>
                                                    </td>
                                                </tr>
                                            @endif
                                        </tbody>
                                    </table>

                                </div>
                            </div>
                        </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
    @push('custom-script')
        <script>
            function myFunction(id) {
                $('#statusModal').modal();
                $('#changestatus').html('<a class="btn btn-danger" style="color:white;" onclick="changedataData(' + id +
                    ')">Ok</a>')
            }

            function changedataData(id) {
                $('#statusModal').modal('hide');
                location.replace("{{ route('ips.access.limit.statuschange') }}?access_id=" + id +
                    "{{ request()->getQueryString() ? '&' . request()->getQueryString() : '' }}");
            }
            function showEditModal(id, limit, period_value, period) {
                $('#rule_id').val(id);
                $('#limit').val(limit);
                $('#period_value').val(period_value);                
                $('#dymic_title').html(period);                
                $('#editsModal').modal('show');
            }
        </script>
    @endpush
@stop
