@extends('backend.layouts.app')

@section('content')
<div class="row page-titles">
   <div class="col-md-5 align-self-center">
      <h4 class="text-themecolor">State </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 "><a href="{{ route('country')}}">Country </a></li>

            <li class="breadcrumb-item active">State </li>
         </ol>
         @can('State-create')                      

         <a href="{{ route('state.create', ['country_id' => request()->get('country_id')]) }}">
             <button type="button" class="btn btn-info d-none d-lg-block m-l-15"><i class="fa fa-plus-circle"></i> Create New</button></a>
             @endcan
      </div>
   </div>
</div>
<div class="container-fluid">
    <div id="deleteModal" class="modal fade" role='dialog'>
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <p>Are you sure you want to delete this?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <span id='deleteButton'></span>
                </div>
            </div>
        </div>
    </div>
    {{-- 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>
    {{-- end --}}

    <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="form-inline">
                            <form action="{{ route('state') }}" method="get" id="form">
                                <div class="input-group">
                                    <input type="text" name="search" value="{{ $search }}" class="form-control" placeholder="Search...">
                                    <div class="input-group-append">
                                        <button class="btn btn-secondary" type="submit"><i class="fas fa-search fa-fw"></i></button>
                                        <a href="{{ route('state') }}" class="btn btn-secondary" title="Reload"><i class="fas fa-sync"></i></a>
                                    </div>
                                </div>
                            </form>
                        </div>
                                <br>
                                <div class="table-responsive">
                            <table id="state" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                    <th>S. No.</th>
                                    <th>@sortablelink('country.name', 'Country')</th>
                                    <th>@sortablelink('short_code', 'Short Code')</th>
                                    <th>@sortablelink('name', 'State Name')</th>
                                    <th>City</th>

                                    <!-- <th>@sortablelink('latitude', 'Latitude')</th>
                                    <th>@sortablelink('longitude', 'Longitude')</th> -->
                                    <th>@sortablelink('created_at', 'Created At')</th>
                                    <th>@sortablelink('updated_at', 'Updated At')</th>
                                    <th>Status</th>
                                    <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if(count($states ) > 0)
                                    @php 
                                                $i = ($states->perPage() * ($states->currentPage() - 1));
                                            @endphp
                                        @foreach ($states  as $key => $state)
                                            <tr>
                                                <td>{{ ++$i }}</td>
                                                <td>{{ optional($state->country)->name }}</td>

                                                <td>{{ $state->short_code }}</td>
                                                
                                                <td>{{ $state->name }}</td>
                                                <!-- <td>{{ $state->cities_count }}</td> -->
                                                <td>
                                                    <a href="{{ route('city', ['state_id' => $state->id]) }}">
                                                    {{ $state->cities->count() }}
                                                    </a>
                                                </td>
                                                <!-- <td>{{ $state->latitude }}</td>
                                                <td>{{ $state->longitude }}</td> -->

                                                <td>{{ $state->created_at->format(config('GET.admin_date_time_format') ?? 'Y-m-d ') }}</td>
                                                <td>{{ $state->updated_at->format(config('GET.admin_date_time_format') ?? 'Y-m-d ') }}</td>
                                                <td onclick="myFunction({{ $state->id }});">
                                                    @if ($state->status == 1)
                                                        <button class="btn btn-success btn-sm"><span>Active</span></button>
                                                    @else
                                                        <button class="btn btn-danger btn-sm"><span>Inactive</span></button>
                                                    @endif
                                                </td>
                                                <td>
                                                @can('State-list')                      

                                                    <a href="{{ route('state.view',['id' => $state->id]) }}">
                                                        <button class="btn btn-info btn-sm" title="view"><i class="fas fa-eye"></i></button>
                                                    </a>
                                                    @endcan
                                                    @can('State-edit')                      

                                                    <a href="{{ route('state.edit',['id' => $state->id] + request()->query()) }}">
                                                        <button class="btn btn-primary btn-sm" title="edit"><i class="fas fa-edit"></i></button>
                                                    </a>
                                                    @endcan
                                                    @can('State-delete')                      

                                                    <button class="btn btn-danger btn-sm" title="delete"
                                                    onclick="confirmDeleteModal(@php echo $state->id; @endphp)"><i
                                                        class="fas fa-trash"></i> </button>
                                                        @endcan
                                                </td>
                                            </tr>
                                        @endforeach
                                    @else
                                        <tr>
                                            <td colspan="12">
                                                <h6><center>No Records Found</center></h6>
                                            </td>
                                        </tr>
                                    @endif
                                </tbody>
                            </table>
</div>
                            @if ($states->total() > 0)
                                <div class="d-flex justify-content-between align-items-center">
                                    <!-- Display custom number of entries -->
                                    <div class="entries-info">
                                        Showing {{ $states->firstItem() }} to {{ $states->lastItem() }} of {{ $states->total() }} entries
                                    </div>

                                    <!-- Pagination -->
                                    <div class="pagination-wrapper float-right">
                                        {{ $states->links('vendor.pagination.bootstrap-4') }}
                                    </div>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>

{{-- Delete modal --}}
<script type="text/javascript">
    function confirmDeleteModal(id) {
        $('#deleteModal').modal();
        $('#deleteButton').html('<a class="btn btn-danger" style="color:white;" onclick="deleteData(' + id + ')">Delete</a>');
    }

    function deleteData(id) {
            location.replace("{{ route('state.delete') }}?id=" + id  + "{{ request()->getQueryString() ? '&' . request()->getQueryString() : '' }}");
        }
</script>
{{-- end --}}

{{-- change status --}}
<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('state.changestatus') }}?id=" + id  + "{{ request()->getQueryString() ? '&' . request()->getQueryString() : '' }}");
        }
</script>
{{-- end --}}
@stop
