@extends('layouts.app')

@section('content')
  @if (Auth::user()->role_id >= 5)
    <div class="container-fluid" style="height:100%">
    @include('sidebar')
    <div style="width:90%; float: left; margin:1em 0 0 1em">
  @else
    <div class="container" style="margin:0 1em">
  @endif

    <div class="row">
        <div class="col-sm-7">
          <h3>Usuarios</h3>
        </div>
        <div class="col-sm-5">
          <div class="pull-right">
            {!! Form::open(['method'=>'GET','url'=>'users','class'=>'navbar-form navbar-left','role'=>'search'])  !!}
            <div class="input-group">
                <input class="form-control" id="search"
                       value="{{ request()->session()->get('search') }}"
                       placeholder="Buscar" name="search"
                       type="text" id="search"/>
                <div class="input-group-btn">
                    <button type="submit" class="btn btn-primary">
                            <i class="fa fa-search" aria-hidden="true"></i>
                    </button>
                </div>
            </div>
            {!! Form::close() !!}
          </div>
        </div>
    </div>
    <table class="table">
        <thead>
        <tr>

            <th style="vertical-align: middle"><a href="{{url('users?field=id&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">ID</a></th>
                {{request()->session()->get('field')=='id'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=name&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Nombre</a></th>
                {{request()->session()->get('field')=='name'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=email&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Correo electrónico</a></th>
                {{request()->session()->get('field')=='email'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=role_id&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Rol de usuario</a></th>
                {{request()->session()->get('field')=='role_id'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=social_id&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">ID de red social</a></th>
                {{request()->session()->get('field')=='social_id'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=phone&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Teléfono</a></th>
                {{request()->session()->get('field')=='phone'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=reported&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Reportes</a></th>
                {{request()->session()->get('field')=='reported'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=phone_verified_at&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Fecha de verificación de teléfono</a></th>
                {{request()->session()->get('field')=='phone_verified_at'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=created_at&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Fecha de creación</a></th>
                {{request()->session()->get('field')=='created_at'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="{{url('users?field=updated_at&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Ultima modificación</a></th>
                {{request()->session()->get('field')=='updated_at'?(request()->session()->get('sort')=='asc'?'':''):''}}
        </tr>
        </thead>
        <tbody>
        @php
            $i=1;
        @endphp
        @foreach ($users as $user)
          <tr>
            <th>{{ $user->id }}</th>
            <td>{{ $user->name }}</td>
            <td >{{ $user->email }}</td>
            <td >{{ $user->roles->name }}</td>
            <td >{{ $user->social_id }}</td>
            <td >{{ $user->phone }}</td>
            <td >{{ ($user->reported != 0) ? "Reportado" : "No reportado" }}</td>
            <td >{{ $user->phone_verified_at }}</td>
            <td>{{ $user->created_at }}</td>
            <td>{{ $user->updated_at }}</td>
            <td>
              <a class="btn btn-secondary btn-xs" title="Editar"
                href="{{url('users/update/'.$user->id)}}">
                <i class="fa fa-pencil"></i>
              </a>
              @php
              if ($user->role_id >= 2) {
                echo ('
                <a class="btn btn-primary btn-xs" title="Crear Proveedor"
                  href="supplier/create/' . $user->id . '">
                  <i class="fa fa-briefcase"></i>
                </a>');
              }
              @endphp
              <input type="hidden" name="_method" value="delete"/>
                <a class="btn btn-danger btn-xs" title="Eliminar"
                   href="javascript:if(confirm('¿Estás seguro de que quieres eliminar este usuario?')) javascript:if(confirm('Usualmente no se deben eliminar usuarios, solo bloquearlos ¿Estás seguro?')) ajaxDelete('{{url('users/delete/'.$user->id)}}','{{csrf_token()}}')">
                   <i class="fa fa-trash"></i>
                </a>
            </td>
        </tr>
        @endforeach

        </tbody>
    </table>

        <ul class="pagination">
            {{ $users->links() }}
        </ul>
</div>
@endsection
