@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>Proveedores</h3>
        </div>
        <div class="col-sm-5">
          <div class="pull-right">
            {!! Form::open(['method'=>'GET','url'=>'supplier','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('supplier?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('supplier?field=user_id&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Usuario</a></th>
                {{request()->session()->get('field')=='user_id'?(request()->session()->get('sort')=='asc'?'':''):''}}

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

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

            <th style="vertical-align: middle"><a href="{{url('supplier?field=cover_photo&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Foto de portada</a></th>
                {{request()->session()->get('field')=='cover_photo'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <th style="vertical-align: middle"><a href="#">Categorías</a></th>

            <th style="vertical-align: middle"><a href="#">Categorías (ingles)</a></th>

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

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

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

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

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

            <th style="vertical-align: middle"><a href="{{url('supplier?field=minimun_fee&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Monto Mínimo</a></th>
                {{request()->session()->get('field')=='minimun_fee'?(request()->session()->get('sort')=='asc'?'':''):''}}

            <!--
            <th style="vertical-align: middle"><a href="{{url('supplier?field=IVA&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Regimen</a></th>
                {{request()->session()->get('field')=='regimen'?(request()->session()->get('sort')=='asc'?'':''):''}}
            -->

            <th style="vertical-align: middle"><a href="#">Calificación</a></th>

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

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

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

            <th style="vertical-align: middle"><a href="{{url('supplier?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('supplier?field=updated_at&sort='.(request()->session()->get('sort')=='asc'?'desc':'asc'))}}">Ultima actualización</a></th>
                {{request()->session()->get('field')=='updated_at'?(request()->session()->get('sort')=='asc'?'':''):''}}
        </tr>
        </thead>
        <tbody>
        @php
            $i=1;
        @endphp
        @foreach ($suppliers as $supplier)
          <tr>
            <th>{{ $supplier->id }}</th>
            <td>{{ $supplier->user->name }}</td>
            <td >{{ ($supplier->membership != 0) ? "Miembro" : "No miembro" }}</td>
            <td >{{ $supplier->company_name }}</td>
            <td ><img src="{{ $supplier->cover_photo }}" style="width:60px; height:auto;"></td>
            <td >{{ str_replace(",",", ", str_replace(array('[',']','"'),"",$supplier->categories->pluck('name'))) }}</td>
            <td >{{ str_replace(",",", ", str_replace(array('[',']','"'),"",$supplier->categories->pluck('en_name'))) }}</td>
            <td >{{ $supplier->tags }}</td>
            <!--
            <td >{{ $supplier->RFC }}</td>
            <td >{{ $supplier->CURP }}</td>
            <td >{{ $supplier->clabe }}</td>
            <td >{{ (isset($supplier->banks->name)) ? $supplier->banks->name : null }}</td>
            -->
            <td >{{ $supplier->minimun_fee }}</td>
            <!-- <td >{{ $supplier->regimen }}</td> -->
            <td >{{ $supplier->total_score/$supplier->finished_jobs }}</td>
            <td >{{ $supplier->address }}</td>
            <td >{{ $supplier->location->getLat() }}, {{ $supplier->location->getLng() }}</td>
            <!-- <td >{{ ($supplier->en != 0) ? "Si" : "No" }}</td> -->
            <td>{{ $supplier->references }}</td>
            <td>{{ $supplier->created_at }}</td>
            <td>{{ $supplier->updated_at }}</td>
            <td>
              <a class="btn btn-secondary btn-xs" title="Edit"
                href="{{url('supplier/update/'.$supplier->id)}}">
                <i class="fa fa-pencil"></i>
              </a>
              <a class="btn btn-primary btn-xs" title="Certificate"
                href="javascript:if(confirm('¿Estás seguro de que quieres agregar/quitar la certificación a este proveedor?')) ajaxRedirect('{{url('supplier/certificate/'.$supplier->id)}}','{{csrf_token()}}')">
                <i class="fa fa-star"></i>
              </a>
              <input type="hidden" name="_method" value="delete"/>
                <a class="btn btn-danger btn-xs" title="Delete"
                   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('supplier/delete/'.$supplier->id)}}','{{csrf_token()}}')">
                   <i class="fa fa-trash"></i>
                </a>
            </td>
        </tr>
        @endforeach

        </tbody>
    </table>

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