<?php
use Illuminate\Support\Facades\Auth;
?>

@extends('layouts/layoutMaster')

@section('title', 'Prima Nota')

@section('vendor-style')
@vite([
'resources/assets/vendor/libs/datatables-bs5/datatables.bootstrap5.scss',
'resources/assets/vendor/libs/datatables-responsive-bs5/responsive.bootstrap5.scss',
'resources/assets/vendor/libs/datatables-buttons-bs5/buttons.bootstrap5.scss',
'resources/assets/vendor/libs/flatpickr/flatpickr.scss',
'resources/assets/vendor/libs/@form-validation/form-validation.scss'

])
@endsection

<!-- Vendor Scripts -->
@section('vendor-script')
@vite([
'resources/assets/vendor/libs/moment/moment.js',
'resources/assets/vendor/libs/flatpickr/flatpickr.js',
'resources/assets/vendor/libs/@form-validation/popular.js',
'resources/assets/vendor/libs/@form-validation/bootstrap5.js',
'resources/assets/vendor/libs/@form-validation/auto-focus.js',
'resources/assets/vendor/libs/@form-validation/popular.js',
'resources/assets/vendor/libs/@form-validation/bootstrap5.js',
'resources/assets/vendor/libs/@form-validation/auto-focus.js',
'resources/assets/vendor/libs/datatables-bs5/datatables-bootstrap5.js',
'resources/assets/vendor/libs/select2/select2.js',
])
@endsection

@section('pageTitle')
<div class="d-flex flex-column">
  <h4 class="mb-1 text-sm-small mt-md-4 mt-lg-4"> 
    <i class="bx bx-receipt d-none d-md-inline-flex d-lg-inline-flex"></i> Prima Nota</h4>
    <nav aria-label="breadcrumb" style="font-size: smaller;" class="d-none d-md-block d-lg-block">
            <ol class="breadcrumb breadcrumb-custom-icon">
      
              <li class="breadcrumb-item">
                <a href="#">Contabilità</a>
                <i class="breadcrumb-icon icon-base bx bx-chevron-right align-middle"></i>
              </li>
              <li class="breadcrumb-item active text-primary">
                <a href="{{ route('prima-nota.index') }}">Prima Nota</a>
              </li>
            </ol>
          </nav>
</div>
@endsection

@section('content')
<style>
  div.upload button:first-child{
    display: none !important;
  }

.bx-chef-hat{
  --svg: url("data:image/svg+xml,%3csvg width='24' height='24' fill='currentColor' viewBox='0 0 24 24' transform='' xmlns='http://www.w3.org/2000/svg'%3e%3c!--Boxicons v3.0.8 https://boxicons.com %7c License https://docs.boxicons.com/free--%3e%3cpath d='M17.13 5.54C16.33 3.42 14.32 2 12 2S7.67 3.42 6.87 5.54A5.506 5.506 0 0 0 2 11c0 2.07 1.18 3.95 3 4.88V18c0 .55.45 1 1 1h12c.55 0 1-.45 1-1v-2.12c1.82-.93 3-2.81 3-4.88 0-2.82-2.13-5.15-4.87-5.46m.53 8.75c-.4.14-.67.52-.67.94v1.78H7v-1.78c0-.42-.27-.8-.67-.94-1.4-.5-2.33-1.82-2.33-3.28 0-1.93 1.57-3.5 3.42-3.5.04 0 .14.01.18.02.49 0 .9-.31 1-.78.36-1.61 1.76-2.73 3.41-2.73s3.05 1.12 3.41 2.73c.1.47.51.81 1 .78.06 0 .12 0 .09-.01 1.93 0 3.5 1.57 3.5 3.5 0 1.47-.94 2.79-2.33 3.28ZM5 20h14v2H5z'%3e%3c/path%3e%3c/svg%3e");
}
</style>

@include('_partials.status')

@include('prima_nota._partials.statistiche')

<div class="row justify-content-center">
  <div class="col-xxl mb-4 mt-2">
    <div class="card">
      <div class="card-body">

      <div class=" mb-4">
    <form id="filtri_prima_nota" class="dt_adv_search" method="GET">
      <div class="row">
        <div class="col-12">
          <div class="row g-3 align-items-end">
            <div class="col-12 col-sm-3 col-lg-3">
              <label class="form-label" for="filtro_data_inizio">Data inizio</label>
              <input type="date" id="filtro_data_inizio" name="data_inizio" class="form-control">
            </div>
            <div class="col-12 col-sm-3 col-lg-3">
              <label class="form-label" for="filtro_data_fine">Data fine</label>
              <input type="date" id="filtro_data_fine" name="data_fine" class="form-control">
            </div>
            <div class="col-12 col-sm-3 col-lg-3">
              <label class="form-label" for="categoria_contabile_id">Categoria contabile</label>
              <select name="categoria_contabile_id" id="categoria_contabile_id" class="form-control form-select">
                <option value="">Tutte le categorie</option>
                @foreach($categorie as $categoria)
                  <option value="{{ $categoria->id }}">{{ $categoria->nome }}</option>
                @endforeach
              </select>
            </div>
            <div class="col-12 col-sm-2 col-lg-2">
              <label class="form-label" for="tipo_movimento">Tipo movimento</label>
              <select name="tipo_movimento" id="tipo_movimento" class="form-control form-select">
                <option value="">Tutti i tipi</option>
                @foreach(\App\Models\PrimaNota::getTipiMovimento() as $tipo)
                  <option value="{{ $tipo['value'] }}">{{ $tipo['label'] }}</option>
                @endforeach
              </select>
            </div>
            <div class="col-12 col-sm-auto">
              <button
                type="button"
                id="btn_reset_filtri_prima_nota"
                class="btn btn-icon btn-outline-secondary"
                title="Azzera filtri"
                aria-label="Azzera filtri">
                <i class="bx bx-revision"></i>
              </button>
            </div>
            
          </div>
        </div>
      </div>
    </form>
  </div>

        {{ $dataTable_primanota->table(['class' => 'table table-bordered']) }}
      </div>
    </div>
  </div>
</div>

<!-- <div class="modal fade" id="basicModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabel1">Modal title</h5>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                  <div class="row">
                    <div class="col mb-6">
                      <label for="nameBasic" class="form-label">Name</label>
                      <input type="text" id="nameBasic" class="form-control" placeholder="Enter Name">
                    </div>
                  </div>
                  <div class="row g-6">
                    <div class="col mb-0">
                      <label for="emailBasic" class="form-label">Email</label>
                      <input type="email" id="emailBasic" class="form-control" placeholder="xxxx@xxx.xx">
                    </div>
                    <div class="col mb-0">
                      <label for="dobBasic" class="form-label">DOB</label>
                      <input type="date" id="dobBasic" class="form-control">
                    </div>
                  </div>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-label-secondary" data-bs-dismiss="modal">Chiudi</button>
                </div>
              </div>
            </div>
          </div> -->

@endsection

@section('page-script')

{{$dataTable_primanota->scripts(attributes: ['type' => 'module'])}}


<script type="module">
  function formatEuroImporto(value) {
    return new Intl.NumberFormat('it-IT', {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2,
    }).format(Number(value) || 0) + ' €';
  }

  function aggiornaStatistichePrimaNota() {
    $.get('{{ route('prima-nota.statistiche') }}', {
      data_inizio: $('#filtro_data_inizio').val(),
      data_fine: $('#filtro_data_fine').val(),
      categoria_contabile_id: $('#categoria_contabile_id').val(),
      tipo_movimento: $('#tipo_movimento').val(),
    }).done(function (data) {
      $('#stat_totale_operazioni').text(data.totale_operazioni ?? 0);
      $('#stat_importo_entrate').text(formatEuroImporto(data.importo_entrate));
      $('#stat_importo_uscite').text(formatEuroImporto(data.importo_uscite));
      $('#stat_saldo').text(formatEuroImporto(data.saldo));
    });
  }

  $(document).ready(function(){
    // Editor edit
    $("#dataTable_primanota").on('click', 'a.editor_edit', function (e) {
      e.preventDefault();
      window.LaravelDataTables["dataTable_primanota-editor"].edit( $(this).closest('tr'), {
        title: 'Modifica',
        buttons: 'Aggiorna',
      });
    });

    // Editor delete
    $("#dataTable_primanota").on('click', 'a.editor_delete', function (e) {
      e.preventDefault();
      window.LaravelDataTables["dataTable_primanota-editor"].remove($(this).closest('tr'), {
        title: 'Cancella record',
        message: 'Sei sicuro di voler eliminare il record selezionato?',
        buttons: 'Cancella record'
      });
    } );

    $("#dataTable_primanota").on('dblclick', 'tbody td', function (e) {
      window.LaravelDataTables["dataTable_primanota-editor"].edit( $(this).closest('tr'), {
        title: 'Modifica',
        buttons: 'Aggiorna',
      });
    });

    $('#filtri_prima_nota').on('change', 'input, select', function () {
      window.LaravelDataTables['dataTable_primanota'].draw();
    });

    $('#btn_reset_filtri_prima_nota').on('click', function () {
      $('#filtri_prima_nota')[0].reset();
      window.LaravelDataTables['dataTable_primanota'].draw();
    });

    $('#dataTable_primanota').on('draw.dt', aggiornaStatistichePrimaNota);

  });

</script>

<script>
  function initComplete_primanota(){
    $('div.dt-buttons button').removeClass('btn-secondary');
    $('div.dt-search').addClass('mt-0 mb-4');
    $('div.dt-buttons').removeClass('btn-group');
  }


function showEntita(id,tipo_entita){

  switch(tipo_entita){
    case 'pagamento':
      $('#basicModal div.modal-body').load("{{ route('pagamento.show') }}?pagamento_id=" + id);
      break;
    case 'ordine':
      $('#basicModal div.modal-body').load("{{ route('ordine.show_modal') }}?ordine_id=" + id);
      break;
    case 'prenotazione':
      $('#basicModal div.modal-body').load("{{ route('prenotazione.show') }}?prenotazione_id=" + id);
      break;
    case 'evento':
      $('#basicModal div.modal-body').load("{{ route('evento.show') }}?evento_id=" + id);
      break;
    case 'fornitore':
      $('#basicModal div.modal-body').load("{{ route('fornitore.show_modal') }}?fornitore_id=" + id);
      break;
    default:
      return;
  }
  $('#basicModal div.modal-header').html('<h5 class="modal-title" id="exampleModalLabel1">Dettaglio ' + tipo_entita + '</h5>');
  // $('#basicModal div.modal-footer').html('<button type="button" class="btn btn-label-secondary" data-bs-dismiss="modal">Chiudi</button>');
  $('#basicModal').modal('show');

}
</script>
@endsection
