@extends('admin::layouts.master')

@section('title', __('admin/common.admin_user'))

@section('content')
  <ul class="nav-bordered nav nav-tabs mb-3">
    @hook('admin.admin_roles.index.tabs.before')
    <li class="nav-item">
      <a class="nav-link" href="{{ admin_route('admin_users.index') }}">{{ __('admin/common.admin_user') }}</a>
    </li>
    <li class="nav-item">
      <a class="nav-link active" href="{{ admin_route('admin_roles.index') }}">{{ __('admin/common.admin_role') }}</a>
    </li>
    @hook('admin.admin_roles.index.tabs.after')
  </ul>

  <div id="tax-classes-app" class="card">
    <div class="card-body h-min-600">
      @hook('admin.admin_roles.index.content.before')
      <div class="d-flex justify-content-between mb-4">
        <a href="{{ admin_route('admin_roles.create') }}"
          class="btn btn-primary">{{ __('admin/role.admin_roles_create') }}</a>
      </div>
      <div class="table-push">
        @hook('admin.admin_roles.index.before')
        <table class="table">
          <thead>
            <tr>
              <th>ID</th>
              <th>{{ __('common.name') }}</th>
              <th>{{ __('common.created_at') }}</th>
              <th>{{ __('common.updated_at') }}</th>
              @hook('admin.admin_roles.index.thead')
              <th class="text-end">{{ __('common.action') }}</th>
            </tr>
          </thead>
          <tbody>
            @if (count($roles))
              @foreach ($roles as $role)
                <tr>
                  <td>{{ $role->id }}</td>
                  <td>{{ $role->name }}</td>
                  <td>{{ $role->created_at }}</td>
                  <td>{{ $role->updated_at }}</td>
                  @hook('admin.admin_roles.index.tbody')
                  <td class="text-end">
                    @hook('admin.admin_roles.index.tbody.actions.before')
                    <a href="{{ admin_route('admin_roles.edit', [$role->id]) }}"
                      class="btn btn-outline-secondary btn-sm">{{ __('common.edit') }}</a>
                    <button class="btn btn-outline-danger btn-sm ml-1 delete-role" data-id="{{ $role->id }}"
                      type="button">{{ __('common.delete') }}</button>
                    @hook('admin.admin_roles.index.tbody.actions.after')
                  </td>
                </tr>
              @endforeach
            @else
              <tr>
                <td colspan="5" class="border-0"><x-admin-no-data /></td>
              </tr>
            @endif
          </tbody>
        </table>
        @hook('admin.admin_roles.index.after')
      </div>
      @hook('admin.admin_roles.index.content.after')
    </div>
  </div>
@endsection

@push('footer')
  <script>
    $('.delete-role').click(function(event) {
      const id = $(this).data('id');
      const self = $(this);

      layer.confirm('{{ __('common.confirm_delete') }}', {
        title: "{{ __('common.text_hint') }}",
        btn: ['{{ __('common.cancel') }}', '{{ __('common.confirm') }}'],
        area: ['400px'],
        btn2: () => {
          $http.delete(`admin_roles/${id}`).then((res) => {
            layer.msg(res.message);
            self.parents('tr').remove()
          })
        }
      })
    });
  </script>
@endpush
