@extends('superadmin.layout')
@section('content')
    <!-- begin::main content -->
    <main class="main-content">
        <div class="container">
            <!-- begin::page header -->
            <div class="page-header">
                <h3>Users</h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Users List</li>
                    </ol>
                </nav>
            </div>
            <!-- end::page header -->
            <div class="card">
                <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('add-user')){ ?>
                <div class="card-header">
                    <a href="{{ asset('superadmin/add-user') }}" class="btn btn-primary pull-left text-white">Add
                        User</a>
                </div>
                <?php } ?>
                <div class="card-body">
                    <table id="example1"  class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Role</th>
                                <th>Username</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('edit-user')){ ?>
                                <th>Status</th>
                                <?php } ?>
                                {{-- <th>Permission</th> --}}
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (!empty($admin_users))
                                @foreach ($admin_users as $admin_user)
                                    <tr>
                                        <td>{{ $admin_user->id }}</td>
                                        <td>
                                            @if (!empty($admin_user->role))
                                                @foreach (explode(',', $admin_user->role) as $r)
                                                    <span class="btn btn-sm btn-gradient-info">
                                                        {{ $r }}
                                                    </span>
                                                @endforeach
                                            @endif
                                        </td>
                                        <td>{{ $admin_user->name }}</td>
                                        <td>{{ $admin_user->email }}</td>
                                        <td>{{ $admin_user->phone }}</td>
                                        <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('edit-user')){ ?>
                                        <td>
                                            <?php
                                            $check_status = $admin_user->status == '1' ? '' : '';
                                            if ($admin_user->status == '1') {
                                              ?>
                                            <div class="custom-control custom-switch custom-checkbox-success">
                                                <label class="switch"><input type="checkbox" name="admin_active" checked
                                                        data-id="{{ $admin_user->id }}" value="1"
                                                        class="custom-control-input status-change" id="customSwitch2_">
                                                    <span class="slider round"></span>
                                                </label>
                                            </div>
                                            <?php
                                            } else {
                                              ?>
                                            <div class="custom-control custom-switch custom-checkbox-success">
                                                <label class="switch"><input type="checkbox" name="admin_active"
                                                        data-id="{{ $admin_user->id }}" value="0"
                                                        class="custom-control-input status-change" id="customSwitch2_">
                                                    <span class="slider round"></span>
                                                </label>
                                            </div>
                                            <?php
                                            }
                                            ?>
                                        </td>
                                        <?php } ?>
                                        <td>
                                            {{-- Change User Password --}}
                                            <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('change-user-password')){ ?>
                                            <a title="Change Password"
                                                href="{{ asset('superadmin/reset-user-password/' . $admin_user->id) }}"><i
                                                    class="fa fa-undo fa-lg text-primary"></i></a>
                                            <?php }?>

                                            {{-- View User --}}
                                            <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('view-user')){ ?>
                                            <a title="View List"
                                                href="{{ asset('superadmin/view-user/' . $admin_user->id) }}"><i
                                                    class="fa fa-eye fa-lg text-primary"></i></a>
                                            <?php }?>

                                            {{-- Add User --}}
                                            <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('edit-user')){ ?>
                                            <a title="Edit User"
                                                href="{{ asset('superadmin/edit-user/' . $admin_user->id) }}"><i
                                                    class="fa fa-edit text-success fa-lg "></i></a>
                                            <?php }?>

                                            {{-- Delete user --}}
                                            <?php if(Auth::user()->hasRole('superadmin') || Auth::user()->can('delete-user')) { ?>
                                            <a title="Delete User"
                                                href="{{ asset('superadmin/delete-user/' . $admin_user->id) }}"
                                                class="delete-confirm">
                                                <i class="fa fa-trash text-danger fa-lg"></i>
                                            </a>
                                            <?php }?>
                                        </td>
                                    </tr>
                                @endforeach
                            @endif
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>
    <!-- end::main content -->
@endsection
@push('footer-script')
    <!-- begin::dataTable -->
    <script src="{{ asset('public/admin/vendors/dataTable/jquery.dataTables.min.js') }}"></script>
    <script src="{{ asset('public/admin/vendors/dataTable/dataTables.bootstrap4.min.js') }}"></script>
    <script src="{{ asset('public/admin/vendors/dataTable/dataTables.responsive.min.js') }}"></script>
    <script src="{{ asset('public/admin/js/examples/datatable.js') }}"></script>
    <script>
        $(document).ready(function() {
            $('#adminuserstb').DataTable({
                responsive: false,
                "lengthMenu": [
                    [25, 50, 75, 100, -1],
                    [25, 50, 75, 100, "All"]
                ],
                'order': [],
                'columnDefs': [{
                    "targets": [1, 3, 4, 5, 6, 7],
                    "orderable": false
                }]
            });
        });

        /* superadmin user status change */
        $('.status-change').on('change', function(e) {
            e.preventDefault();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            let id = $(this).data('id');
            let ckb = $(this).is(':checked');
            // alert(ckb);

            $.ajax({
                url: "{{ asset('superadmin/change-status') }}",
                type: "POST",
                dataType: "html",
                data: {
                    id: id,
                    ckb: ckb
                },

                success: function(result) {
                    if (result == '1') {
                        toastr.success('User Activated!');
                    } else {
                        toastr.warning('User Deactivated!');
                    }
                }
            });
        });
    </script>
    <!-- end::dataTable -->
@endpush
