@extends('layouts.app', ['title' => 'Ordens de Separaçãp'])
@section('content')
<div class="mt-1">
    <div class="row">
        <div class="card">
            <div class="card-body">

                <div style="text-align: right; margin-top: 5px;">
                    <a href="{{ route('orcamentos.index') }}" class="btn btn-danger btn-sm px-3">
                        <i class="ri-arrow-left-double-fill"></i>Voltar para orçamentos
                    </a>
                </div>
                <div class="col-lg-12">
                    {!!Form::open()->fill(request()->all())
                    ->get()
                    !!}
                    <div class="row mt-3">
                        <div class="col-md-3">
                            {!!Form::select('cliente_id', 'Cliente')
                            ->attrs(['class' => 'select2'])
                            !!}
                        </div>
                        <div class="col-md-2">
                            {!!Form::date('start_date', 'Data inicial')
                            !!}
                        </div>
                        <div class="col-md-2">
                            {!!Form::date('end_date', 'Data final')
                            !!}
                        </div>
                        
                        <div class="col-lg-3 col-12">
                            <br>
                            <button class="btn btn-primary" type="submit"> <i class="ri-search-line"></i>Pesquisar</button>
                            <a id="clear-filter" class="btn btn-danger" href="{{ route('ordem-separacao.index') }}"><i class="ri-eraser-fill"></i>Limpar</a>
                        </div>
                    </div>
                    {!!Form::close()!!}
                </div>
                <div class="col-md-12 mt-3">
                    <div class="table-responsive">
                        <table class="table table-striped table-centered mb-0">
                            <thead class="table-dark">
                                <tr>
                                    <th>#</th>
                                    <th>Cliente</th>
                                    <th>CPF/CNPJ</th>
                                    <th>Código do orçamento</th>
                                    <th>Total de itens</th>
                                    <th>Data</th>
                                    <th>Estado</th>
                                    <th>Usuário</th>
                                    <th>Ações</th>
                                </tr>
                            </thead>
                            <tbody>

                                @foreach($data as $item)

                                @php
                                $statusMap = [
                                'em_separacao' => ['label' => 'Em Separação', 'class' => 'warning'],
                                'finalizado'   => ['label' => 'Finalizado',   'class' => 'success'],
                                'cancelado'    => ['label' => 'Cancelado',    'class' => 'danger'],
                                ];

                                $status = $statusMap[$item->status] ?? ['label'=>'Indefinido','class'=>'secondary'];
                                @endphp

                                <tr>
                                    <td data-label="#"> {{ $item->numero_sequencial }} </td>
                                    <td data-label="Cliente"> {{ $item->cliente ? $item->cliente->razao_social : "--" }} </td>
                                    <td data-label="CPF/CNPJ"> {{ $item->cliente ? $item->cliente->cpf_cnpj : "--" }} </td>
                                    <td data-label="Código do orçamento"> {{ $item->orcamento->numero_sequencial }} </td>
                                    <td data-label="Valor"> {{ sizeof($item->itens) }} </td>
                                    <td data-label="Data"> {{ __data_pt($item->created_at) }} </td>
                                    <td data-label="Estado">
                                        <span class="badge bg-{{ $status['class'] }} p-1">
                                            {{ $status['label'] }}
                                        </span>
                                    </td>
                                    <td data-label="Usuário">
                                        {{ $item->usuarioInicia->name }}
                                    </td>
                                    <td>
                                        <form style="width:200px;" action="{{ route('ordem-separacao.destroy', $item->id) }}" method="post" id="form-{{$item->id}}">
                                            @method('delete')
                                            @csrf
                                            <a title="Imprimir" class="btn btn-primary btn-sm" target="_blank" onclick="imprimir('{{$item->id}}')">
                                                <i class="ri-printer-line"></i>
                                            </a>
                                            
                                            @can('ordem_separacao_delete')
                                            @if($item->status != 'finalizado')
                                            <button type="button" class="btn btn-danger btn-sm btn-delete">
                                                <i class="ri-delete-bin-line"></i>
                                            </button>
                                            @endif
                                            @endcan

                                            @can('ordem_separacao_view')
                                            <a title="Ver ordem de separação" class="btn btn-dark btn-sm" href="{{ route('ordem-separacao.show', $item->id) }}">
                                                <i class="ri-file-line"></i>
                                            </a>
                                            @endcan

                                        </form>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>

                    </div>
                    {!! $data->appends(request()->all())->links() !!}
                </div>

                
            </div>
        </div>
    </div>
</div>

@endsection

@section('js')
<script type="text/javascript">
    function imprimir(id){
        var disp_setting="toolbar=yes,location=no,";
        disp_setting+="directories=yes,menubar=yes,";
        disp_setting+="scrollbars=yes,width=850, height=600, left=100, top=25";

        var docprint=window.open(path_url+"ordem-separacao-imprimir/"+id, "",disp_setting);

        docprint.focus();
    }
</script>
@endsection

