@extends('admin.layouts.app')
@section('title')
    {{ __('users_list') }}
@endsection

<style>
    .ph-arrow-left{
        content: "<"
    }
</style>
@section('content')
    @php
        $userr = auth()->user();
    @endphp

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="d-flex align-items-center justify-content-between">
                            <div class="d-flex">
                                <h3 class="card-title line-height-36 " >{{ __('users_list') }}</h3>
                                {{-- <span class=""> --}}
                                <form class="ml-3" id="formSubmit" action="{{ route('user.index') }}" method="GET" onchange="this.submit();">                        
                                        {{-- <div class="col-12"> --}}
                                            {{-- <label>{{ __('search') }}</label> --}}
                                            <input name="keyword" type="text" placeholder="{{ __('search') }} by Name Or Email" class="form-control"
                                                value="{{ request('keyword') }}">
                                        {{-- </div> --}}
                                </form>
                                @if (request('keyword'))
                                <a href="{{ route('user.index') }}"
                                    class="btn btn-danger align-items-center justify-content-center ml-3" style="height: 37px"><i
                                        class="fas fa-times"></i>
                                </a>
                                @endif
                            </div>
                            
                            
                            {{-- </span> --}}
                            <div class="d-flex align-items center">
                                <a href="{{ route('role.index') }}" class="btn btn-outline-dark mr-2">
                                    <i class="fas fa-lock mr-1"></i>
                                    {{ __('all_roles') }}
                                </a>
                                <a href="{{ route('user.create') }}" class="btn bg-primary">
                                    <i class="fas fa-plus"></i>
                                    {{ __('create') }}
                                </a>
                            </div>
                        </div>

                    </div>

                    <div class="card-body table-responsive p-0">
                        <table class="table table-hover text-nowrap">
                            <thead>
                                <tr>
                                    <th width="5%">#</th>
                                    <th>{{ __('name') }}</th>
                                    <th>{{ __('email') }}</th>
                                    <th>{{ __('roles') }}</th>
                                    <th>{{ __('status') }}</th>
                                    @if ($userr->can('admin.edit') || $userr->can('admin.delete'))
                                        <th width="10%">{{ __('action') }}</th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($users as $user)
                                    <tr>
                                        <td>{{ $loop->index + 1 }}</td>
                                        <td>{{ $user->name }}</td>
                                        <td>{{ $user->email }}</td>
                                        <td>
                                            @foreach ($user->roles as $role)
                                                <span class="badge badge-primary">{{ $role->name }}</span>
                                            @endforeach
                                        </td>
                                        <td tabindex="0"  class="center">
                                            <div class="d-flex">
                                                <a href="javascript:void(0)" class="active-status">
                                                    <label class="switch ">
                                                        <input data-id="{{ $user->id }}" data-class="status" type="checkbox"
                                                            class="success status-switch change-active-status"
                                                            {{ $user->status == 1 ? 'checked' : '' }}>
                                                        <span class="slider round"></span>
                                                    </label>
                                                    <p class="{{ $user->status == 1 ? 'active' : '' }}" id="status_{{ $user->id }}">
                                                        {{ $user->status == 1 ? __('activated') : __('deactivated') }}</p>
                                                </a>
                                            </div>
                                        </td>
                                        <td>
                                            <a href="{{ route('user.edit', $user->id) }}" class="btn bg-info"><i
                                                    class="fas fa-edit"></i></a>
                                            <form action="{{ route('user.destroy', $user->id) }}" method="POST"
                                                class="d-inline">
                                                @method('DELETE')
                                                @csrf
                                                <button
                                                    onclick="return confirm(__('are_you_sure_want_to_delete_this_item'));"
                                                    class="btn bg-danger"><i class="fas fa-trash"></i></button>
                                            </form>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="10" class="text-center">
                                            <x-admin.not-found route="user.create" />
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                      
                        <div class="mt-3 d-flex justify-content-center">
                            {{ $users->links() }}
                        </div>
                      
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('style')
    <style>
        /* Extra small devices (phones, 600px and down) */
        @media only screen and (max-width: 600px) {
            .customRow1 {
                margin-top: 10px;
            }

            .customRow2 {
                margin-top: 10px;
                padding-left: 15px;
                padding-right: 15px;
            }

            .customdiv1 {
                margin-top: 5px;
                margin-right: .5rem !important;
            }

            .customdiv2 {
                margin-top: 5px;
                margin-right: .5rem !important;
            }

            .customdiv3 {
                margin-top: 5px;
                margin-bottom: 5px;
                margin-right: .5rem !important;
            }
        }


    /* on of button */
    .switch {
    position: relative;
    display: inline-block;
    width: 35px;
    height: 19px;
    }

    /* Hide default HTML checkbox */
    .switch input {
        display: none;
    }

    /* The slider */
    .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ccc;
        -webkit-transition: .4s;
        transition: .4s;
    }

    .slider:before {
        position: absolute;
        content: "";
        height: 15px;
        width: 15px;
        left: 3px;
        bottom: 2px;
        background-color: white;
        -webkit-transition: .4s;
        transition: .4s;
    }

    input.success:checked+.slider {
        background-color: #28a745;
    }

    input:checked+.slider:before {
        -webkit-transform: translateX(15px);
        -ms-transform: translateX(15px);
        transform: translateX(15px);
    }

    /* Rounded sliders */
    .slider.round {
        border-radius: 34px;
    }

    .slider.round:before {
        border-radius: 50%;
    }
    </style>
    {{-- Flat Icon Css Link --}}
    <link rel="stylesheet" href="{{ asset('backend') }}/plugins/flagicon/dist/css/flag-icon.min.css" />
    <link rel="stylesheet" href="{{ asset('backend') }}/plugins/flagicon/dist/css/bootstrap-iconpicker.min.css" />
    <link rel="stylesheet" href="{{ asset('backend') }}/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
@endsection
@section('script')
    {{-- Flat Icon Css Link --}}
    <script src="{{ asset('backend') }}/plugins/flagicon/dist/js/bootstrap-iconpicker.bundle.min.js"></script>

    <script>
        $("#checkboxAll").on('click', function() {
            $('input:checkbox').not(this).prop('checked', this.checked);
            SelectIds();
        });
        $(".sub_chk").on('click', function() {
            SelectIds();
        });

        function SelectIds() {

            var Ids = [];
            $(".sub_chk:checked").each(function() {
                Ids.push($(this).attr('data-id'));
            });
            $('#selectedCount').html(parseInt(Ids.length));
            if (Ids != 0) {
                // Show Delete Button
                $('#DeleteButton').removeClass('d-none');
            } else {
                // Hide Delete Button
                $('#DeleteButton').addClass('d-none');
            }
        }

        function MultiDelete() {

            var Ids = [];
            $(".sub_chk:checked").each(function() {
                Ids.push($(this).attr('data-id'));
            });

            if (Ids != 0) {

                if (confirm("{{ __('are_you_sure_you_want_to_delete_this_item') }}") == true) {
                    AjaxCall(Ids);
                } else {
                    return false;
                }

            } else {
                alert('Please Select First');
            }
        }

        function AjaxCall(value) {
            $.ajax({
                url: "{{ route('module.country.multiple.delete') }}",
                type: "Delete",
                data: {
                    ids: value,
                    _token: '{{ csrf_token() }}'
                },
                dataType: 'json',
                success: function(result) {
                    location.reload();
                }
            });
        }


        $('.status-switch').on('change', function() {
            var status = $(this).prop('checked') == true ? 1 : 0;
            var id = $(this).data('id');
            var type = $(this).data('class');

            $.ajax({
                type: "GET",
                dataType: "json",
                url: '{{ route('admin.userStatusChange') }}',
                data: {
                    'status': status,
                    'type': type,
                    'id': id
                },
                success: function(response) {
                    toastr.success(response.message, 'Success');
                }
            });   

            if(type == 'status'){
            if (status == 1 ) {
                $(`#status_${id}`).text("{{ __('activated') }}")
            }else{
                $(`#status_${id}`).text("{{ __('deactivated') }}")
            }
            }   
      
        });
         
    </script>
    
@endsection

