@extends('layout')
@section('content')
    <!-- begin::main content -->
    <main class="main-content">
        <div class="container">
            <!-- begin::page header -->
            <div class="page-header">
                <h3>{{ __('message.users') }}</h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ asset('dashboard') }}">{{ __('message.dashboard') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('message.list') }}</li>
                    </ol>
                </nav>
            </div>
            <!-- end::page header -->
            <div class="card">

                <div class="card-header">
                    <form action="" method="GET">
                        <div class="row">

                            <div class="col-md-2">
                                <select class="form-control" name="per_page" id="per_page">
                                    <option value="25">25</option>
                                    <option {{ isset($_GET['per_page']) && $_GET['per_page'] == 50 ? 'selected' : '' }}
                                        value="50">50</option>
                                    <option {{ isset($_GET['per_page']) && $_GET['per_page'] == 100 ? 'selected' : '' }}
                                        value="100">100</option>
                                    <option {{ isset($_GET['per_page']) && $_GET['per_page'] == 200 ? 'selected' : '' }}
                                        value="200">200</option>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <input placeholder="Date range filter" value="{{ request()->input('datefilter') }}"
                                    type="text" name="datefilter" class="form-control">
                            </div>
                            <div class="col-md-4">
                                <input type="text" class="form-control" name="query"
                                    placeholder="Search By (name, UniqueID, Email, Phone)"
                                    value="{{ request()->input('query') }}">
                                <span class="text-danger">
                                    @error('query')
                                        {{ $message }}
                                    @enderror
                                </span>
                            </div>

                            <div class="col-md-3">
                                <button data-toggle="tooltip" data-original-title="Search" type="submit"
                                    class="btn m-r-10 btn-outline-secondary pull-left"><i class="ti ti-search fa-lg"></i>
                                </button>
                                <a data-toggle="tooltip" data-original-title="Reset Filters" href="{{ asset('/users') }}"
                                    class="btn m-r-10 btn-outline-secondary pull-left">
                                    <i class="ti ti-reload fa-lg"></i>
                                </a>
                                @if (Auth::user()->can('delete-user'))
                                    <a id="deleteAll" data-toggle="tooltip" data-original-title="Multiple Delete"
                                        href="javascript:;" class="btn btn-outline-secondary pull-right">
                                        <i class="ti ti-trash fa-lg"></i>
                                    </a>
                                @endif
                            </div>
                        </div>
                    </form>

                    <div class="row m-t-10">
                        <div class="col-md-12">
                            @if (Auth::user()->can('add-user'))
                                <a data-toggle="tooltip" data-original-title="Import CSV" href="{{ asset('import-user') }}"
                                    class="btn btn-outline-primary text-uppercase">
                                    <i class="ti ti-upload pr-2"></i> {{ __('message.import') }}
                                </a>

                                <a data-toggle="tooltip" data-original-title="Add New User"
                                    href="{{ asset('add-user') }}" class="btn btn-outline-primary text-uppercase"><i
                                        class="ti ti-plus pr-2"></i>{{ __('message.users') }}</a>
                            @endif
                        </div>
                    </div>
                </div>

                <div class="card-body">

                    @if (isset($users))

                        <div class="table-responsive">
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>
                                            <input type="checkbox" id="checkAll" />
                                        </th>
                                        <th>{{ __('message.id') }}</th>
                                        <th>{{ __('message.role') }}</th>
                                        <th>{{ __('message.date') }}</th>
                                        <th>UniqueID</th>
                                        <th>{{ __('message.email') }}</th>
                                        <th>{{ __('message.phone') }}</th>
                                        <th>{{ __('message.status') }}</th>
                                        <th>{{ __('message.action') }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (!empty($users))
                                        @foreach ($users as $user)
                                            <tr>
                                                <td>
                                                    <input class="deleteall" type="checkbox" name="delete[]"
                                                        value="{{ $user->id }}" />
                                                </td>
                                                <td>{{ $user->id }}</td>

                                                <td>
                                                    @foreach ($user->roles as $role)
                                                        <span class="badge badge-info">{{ $role->name }}</span>
                                                    @endforeach
                                                </td>

                                                <td>
                                                    <span class="badge badge-info">
                                                        {{ date('d-m-Y', strtotime($user->created_at)) }}
                                                    </span>
                                                </td>

                                                <td>{{ $user->user_unique_id }}</td>
                                                <td><a
                                                        href="#">{{ $user->email }}</a>
                                                </td>
                                                <td>{{ $user->phone }}</td>
                                                <td>
                                                    @if ($user->status == 1)
                                                        <label for="" class="text-success">Active</label>
                                                    @endif
                                                    @if ($user->status == 0)
                                                        <label for="" class="text-danger">Inactive</label>
                                                    @endif
                                                </td>
                                                <td>
                                                    <div class="btn-group">
                                                        @if (Auth::user()->can('view-user-report'))
                                                            <a target="_blank" class="btn btn-sm btn-instagram text-white"
                                                                data-toggle="tooltip" data-original-title="User Report"
                                                                href="{{ asset('report-dashboard/' . $user->user_unique_id) }}">
                                                                <i class="fa fa-bar-chart-o fa-lg"></i>
                                                            </a>
                                                        @endif

                                                        <a class="btn btn-sm btn-info text-white" data-toggle="tooltip"
                                                            data-original-title="User Info"
                                                            href="{{ asset('view-user/' . $user->id) }}"><i
                                                                class="fa fa-eye fa-lg"></i>
                                                        </a>

                                                        @if (Auth::user()->can('view-user-device'))
                                                            <!-- <a class="btn btn-sm btn-dark text-white" data-toggle="tooltip"
                                                                data-original-title="Registerd Devices"
                                                                href="{{ asset('devices/' . $user->id) }}"><i
                                                                    class="fa fa-android fa-lg"></i>
                                                            </a> -->
                                                        @endif


                                                        @if (Auth::user()->can('edit-user'))
                                                            <a class="btn btn-sm btn-dribbble text-white"
                                                                data-toggle="tooltip" data-original-title="Change Password"
                                                                href="{{ asset('user-password-reset/' . $user->id) }}"><i
                                                                    class="fa fa-refresh fa-lg"></i>
                                                            </a>
                                                            <a class="btn btn-sm btn-success text-white"
                                                                data-toggle="tooltip" data-original-title="Edit User"
                                                                href="{{ asset('edit-user/' . $user->id) }}"><i
                                                                    class="fa fa-edit fa-lg"></i>
                                                            </a>

                                                           
                                                        @endif
                                                    </div>
                                                </td>
                                            </tr>
                                        @endforeach
                                    @else
                                        <h1>No Record Found!!</h1>
                                    @endif
                                </tbody>
                            </table>
                        </div>


                        <div class="pagination-block">
                            @if (isset($_GET['query']))
                                {{ $users->appends(['query' => $_GET['query'], 'per_page' => $_GET['per_page'], 'datefilter' => $_GET['datefilter']])->links('includes.paginationlinks') }}
                            @else
                                {{ $users->links('includes.paginationlinks') }}
                            @endif
                            <label>Total Record Found- <?php echo count($users); ?></label>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </main>
    <!-- end::main content -->
@endsection
@push('footer-script')
    <!-- begin::dataTable -->
    <script src="{{ asset('public/admin/js/examples/datatable.js') }}"></script>
    <!-- end::dataTable -->
    <script>
        $(document).ready(function() {
            $("#checkAll").click(function() {
                $('input:checkbox').not(this).prop('checked', this.checked);
            });

            $('#deleteAll').click(function() {
                if (confirm("Are you sure you want to delete Users? ")) {
                    var val = [];
                    $('.deleteall:checked').each(function(i) {
                        val[i] = $(this).val();
                    });

                    if (val.length > 0) {
                        $.ajax({
                            method: "POST",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            url: "{{ asset('delete-multiple-users') }}",
                            data: {
                                "ids": val
                            }
                        }).done(function(data) {
                            if (data) {
                                location.reload();
                            } else {
                                alert("Something went wrong!, Try again");
                            }
                        });
                    }
                }
                //alert({{ asset('admin/delete-multiple-users') }});
            });

        });
    </script>
@endpush
