@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: Importação de Planilhas da Dívida Ativa")
@section('content')


    <div class="container mt-5 pb-5">
        <div class="row">
            <div class="col-12">
                <div class="card shadow">
                    
                    @include('components.buttons._headLineButtonsBackAdd', [
                        'headLine' => __("Importação de Planilhas da Dívida Ativa"),
                        'routeBack' => 'admin.dividaAtiva.index',
                        'routeBackParams' => [], 
                        'nameBack' => __("Voltar"),
                    ])
                    
                    <div class="card-body">
                        <form action="{{ route('admin.dividaAtiva.planilhas.store') }}" method="post" enctype="multipart/form-data">
                            @csrf
                            @method('POST')
                            @include("errors.messageFlash")

                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <div class="mb-3">
                                        <label for="tipo" class="form-label text-bold">Arquivo tipo: <span class="span-admin-create">*</span></label>
                                        <select class="form-control @error('tipo') is-invalid @enderror" name="tipo" id="tipo">
                                            <option value="" {{ old("tipo") == "" ? "selected" : "" }}></option>
                                            <option value=".xlsx" {{ old("tipo") == ".xlsx" ? "selected" : "" }}>.xlsx</option>
                                            <option value=".xls" {{ old("tipo") == ".xls" ? "selected" : "" }}>.xls</option>
                                            <option value=".csv" {{ old("tipo") == ".csv" ? "selected" : "" }}>.csv</option>
                                        </select>
                                        @error('tipo')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <div class="mb-3">
                                        <label for="nome" class="form-label text-bold">Nome do arquivo: <span class="span-admin-create">*</span></label>
                                        <input type="text" class="form-control @error('nome') is-invalid @enderror" name="nome" id="nome" value="{{ old('nome') }}" oninput="this.value = this.value.toUpperCase()" autocomplete="off">
                                        @error('nome')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="link" class="form-label text-bold">Arquivo: <span class="span-admin-create">*</span></label>
                                    <input type="file" 
                                       class="form-control @error('link') is-invalid @enderror" 
                                       name="link" 
                                       id="link" 
                                       accept=".xls,.xlsx,.csv" />
                                    @error('link')
                                        <small class="text-danger text-bold">{{ $message }}</small>
                                    @enderror
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <div class="mb-3">
                                        <label for="status" class="form-label text-bold">Status: <span class="span-admin-create">*</span></label>
                                        <select class="form-control @error('status') is-invalid @enderror" name="status" id="status">
                                            <option value="" {{ old("tipo") == "" ? "selected" : "" }}></option>
                                            @foreach(App\Enums\StatusEnum::cases() as $status)
                                                <option value="{{ $status->name }}" {{ old("status", "ATIVADO") == $status->name ? "selected" : "" }}>{{ $status->value }}</option>
                                            @endforeach
                                        </select>
                                        @error('status')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                            </div>

                            @include('components.buttons._backAndSend', [
//                                    'textBack' => 'Voltar', 
//                                    'routeBack' => 'admin.dividaAtiva.index'
                                    'textSend' => 'Importar', 
                                    ])
                        </form>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12">
                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-striped align-middle text-center">
                                        @include('components.tables._thead', ['thead' => [
                                                'Criado em', 
                                                'Tipo de arquivo', 
                                                'Nome do arquivo', 
                                                'Arquivo', 
                                                'Status', 
                                                'Ações',
                                             ]])
                                        <tbody>
                                            @php $index = 0; @endphp
                                                @forelse($planilhas as $planilha)
                                                <tr>
                                                    <td>{{ date("d/m/Y H:m", strtotime($planilha->created_at)) }}</td>
                                                    <td>{{ $planilha->tipo ?? "Não informado" }}</td>
                                                    <td>{{ $planilha->nome ?? "Não informado" }}</td>
                                                    <td>
                                                        @include('components.tables._downloadFile',[
                                                                'url' => $planilha->link,
                                                                'text' => 'Baixar planilha',
                                                                'fontawesome' => null,
                                                            ])
                                                    </td>


                                                    <td class="text-center align-middle">

                                                        @include('components/buttons/_status', [
                                                                "index" => $index,
                                                                "route" => "admin.dividaAtiva.planilhas.status",
                                                                "id" => $planilha->id,
                                                                "status" => $planilha->status,
                                                            ])

                                                    </td>
                                                    <td>
                                                        <div class="d-flex gap-2 d-flex justify-content-center align-items-center">
                                                            
                                                            @if(!$planilha->importada)
                                                                @include('components/buttons/_editButton', [
                                                                        "route" => "admin.dividaAtiva.planilhas.dados",
                                                                        "id" => $planilha->id,
                                                                        "fontawesome" => '<i class="fa-solid fa-file-export"></i>',
                                                                        "title" => "Importar dados",
                                                                    ])
                                                            @endif

                                                            @include('components/buttons/_deleteButton', [
                                                                    "message" => __("* Este registro será excluído e esta ação não poderá ser desfeita, deseja continuar?"),
                                                                    "route" => "admin.dividaAtiva.planilhas.destroy",
                                                                    "id" => $planilha->id,
                                                                    "fontawesome" => null,
                                                                    "title" => "Excluir",
                                                                    "permission" => "GERENCIADOR_REGISTROS_DE_DIVIDA_ATIVA",
                                                                ])

                                                        </div>
                                                    </td>

                                                </tr>
                                                @php $index++; @endphp
                                            @empty
                                                <tr>
                                                    <td colspan="6">* Nenhum registro encontrado</td>
                                                </tr>
                                            @endforelse
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12 my-3">
                                <div class="d-flex justify-content-center">{!! $planilhas->links() !!} </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


@endsection
