@extends('layouts.ownerLayout')

@section('title', 'Reportes')

@section('content')
<div class="d-flex align-items-center justify-content-between flex-wrap gap-3 mb-4">
  <div>
    <h4 class="mb-1">Transacciones y reportes</h4>
    <p class="mb-0 text-body-secondary">Control de ventas, costos y utilidad del periodo.</p>
  </div>
</div>

<div class="card mb-4">
  <div class="card-body">
    <form method="GET" class="row g-3 align-items-end">
      <div class="col-md-4">
        <label class="form-label">Desde</label>
        <input type="date" name="date_from" class="form-control" value="{{ $filters['date_from'] }}">
      </div>
      <div class="col-md-4">
        <label class="form-label">Hasta</label>
        <input type="date" name="date_to" class="form-control" value="{{ $filters['date_to'] }}">
      </div>
      <div class="col-md-4 d-flex gap-2">
        <button type="submit" class="btn btn-primary">Aplicar</button>
        <a href="{{ route('owner.reports.index') }}" class="btn btn-label-secondary">Restablecer</a>
      </div>
    </form>
  </div>
</div>

<div class="row g-4 mb-4">
  <div class="col-md-3">
    <div class="card h-100"><div class="card-body">
      <p class="text-body-secondary mb-1">Servicios</p>
      <h4 class="mb-0">{{ number_format($summary['services']) }}</h4>
    </div></div>
  </div>
  <div class="col-md-3">
    <div class="card h-100"><div class="card-body">
      <p class="text-body-secondary mb-1">Ventas</p>
      <h4 class="mb-0">${{ number_format($summary['sales_total'], 0, ',', '.') }}</h4>
    </div></div>
  </div>
  <div class="col-md-3">
    <div class="card h-100"><div class="card-body">
      <p class="text-body-secondary mb-1">Pagado</p>
      <h4 class="mb-0 text-success">${{ number_format($summary['payments_total'], 0, ',', '.') }}</h4>
    </div></div>
  </div>
  <div class="col-md-3">
    <div class="card h-100"><div class="card-body">
      <p class="text-body-secondary mb-1">Pendiente por cobrar</p>
      <h4 class="mb-0 text-warning">${{ number_format($summary['pending_to_collect'], 0, ',', '.') }}</h4>
    </div></div>
  </div>
</div>

<div class="row g-4 mb-4">
  <div class="col-lg-8">
    <div class="card h-100">
      <div class="card-header"><h5 class="mb-0">Resumen financiero</h5></div>
      <div class="card-body">
        <div class="row g-4">
          <div class="col-md-4">
            <small class="text-body-secondary d-block">Costo interno estimado</small>
            <span class="fs-5 fw-semibold">${{ number_format($summary['internal_cost_estimated'], 0, ',', '.') }}</span>
          </div>
          <div class="col-md-4">
            <small class="text-body-secondary d-block">Utilidad estimada</small>
            <span class="fs-5 fw-semibold {{ $summary['gross_profit_estimated'] >= 0 ? 'text-success' : 'text-danger' }}">
              ${{ number_format($summary['gross_profit_estimated'], 0, ',', '.') }}
            </span>
          </div>
          <div class="col-md-4">
            <small class="text-body-secondary d-block">Descuentos aplicados</small>
            <span class="fs-5 fw-semibold text-danger">-${{ number_format($summary['discount_total'], 0, ',', '.') }}</span>
          </div>
        </div>
        <hr>
        <div class="row g-4">
          <div class="col-md-4">
            <small class="text-body-secondary d-block">Costo obsequios (estimado)</small>
            <span class="fw-semibold">${{ number_format($summary['estimated_gift_cost'], 0, ',', '.') }}</span>
          </div>
          <div class="col-md-4">
            <small class="text-body-secondary d-block">Costo artistas (estimado)</small>
            <span class="fw-semibold">${{ number_format($summary['estimated_artist_cost'], 0, ',', '.') }}</span>
          </div>
          <div class="col-md-4">
            <small class="text-body-secondary d-block">Costo otros (estimado)</small>
            <span class="fw-semibold">${{ number_format($summary['estimated_other_cost'], 0, ',', '.') }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="card h-100">
      <div class="card-header"><h5 class="mb-0">Estado de servicios</h5></div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-sm mb-0">
            <thead>
              <tr>
                <th>Estado</th>
                <th class="text-end">Servicios</th>
                <th class="text-end">Ventas</th>
              </tr>
            </thead>
            <tbody>
              @forelse($statusSummary as $status => $row)
                <tr>
                  <td>{{ $status }}</td>
                  <td class="text-end">{{ number_format($row['count']) }}</td>
                  <td class="text-end">${{ number_format($row['sales'], 0, ',', '.') }}</td>
                </tr>
              @empty
                <tr><td colspan="3" class="text-center text-body-secondary py-3">Sin datos en el periodo.</td></tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="card">
  <div class="card-header d-flex justify-content-between align-items-center">
    <h5 class="mb-0">Transacciones recientes</h5>
    <small class="text-body-secondary">Solo pagos reales (no planificados)</small>
  </div>
  <div class="table-responsive">
    <table class="table mb-0">
      <thead>
        <tr>
          <th>Fecha</th>
          <th>Serenata</th>
          <th>Método</th>
          <th>Referencia</th>
          <th>Registrado por</th>
          <th class="text-end">Importe</th>
        </tr>
      </thead>
      <tbody>
        @forelse($recentPayments as $payment)
          <tr>
            <td>{{ optional($payment->paid_at)->format('d/m/Y H:i') }}</td>
            <td>{{ $payment->serenata?->code ?: ('#' . $payment->serenata_id) }}</td>
            <td>{{ $payment->payment_method_name ?: ($payment->method->name ?? '-') }}</td>
            <td>{{ $payment->transaction_reference ?: '-' }}</td>
            <td>{{ $payment->registeredBy->name ?? 'Sistema' }}</td>
            <td class="text-end fw-semibold">${{ number_format($payment->amount, 0, ',', '.') }}</td>
          </tr>
        @empty
          <tr>
            <td colspan="6" class="text-center text-body-secondary py-4">No hay transacciones en este periodo.</td>
          </tr>
        @endforelse
      </tbody>
    </table>
  </div>
</div>

<div class="alert alert-info mt-4 mb-0">
  Los costos son <strong>estimados</strong> con base en el costo interno actual del catálogo por código. Si cambias costos internos hoy, puede variar el histórico estimado.
</div>
@endsection
