@extends('layouts.app')

@section('content')
<div class="container-fluid">

<h3 class="mb-3">Reporte de Ventas</h3>

<form method="GET" action="{{ route('reports.sales') }}">
<div class="card mb-3">
<div class="card-body row">

    <div class="col-md-3">
        <label>Fecha desde</label>
        <input type="date" name="from" value="{{ $from }}" class="form-control">
    </div>

    <div class="col-md-3">
        <label>Fecha hasta</label>
        <input type="date" name="to" value="{{ $to }}" class="form-control">
    </div>

    <div class="col-md-2 d-flex align-items-end">
        <button class="btn btn-primary w-100">Generar reporte</button>
    </div>

    <div class="col-md-2 d-flex align-items-end">
        <a href="{{ route('reports.sales.pdf', ['from'=>$from,'to'=>$to]) }}"
           class="btn btn-danger w-100">
           Exportar PDF
        </a>
    </div>

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

{{-- Tarjetas resumen --}}
<div class="row mb-3">

    <div class="col-md-3">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <h6>Total vendido</h6>
                <h3>${{ number_format($totalSales,2) }}</h3>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card bg-success text-white">
            <div class="card-body">
                <h6>Total cobrado</h6>
                <h3>${{ number_format($totalPaid,2) }}</h3>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card bg-danger text-white">
            <div class="card-body">
                <h6>Total pendiente</h6>
                <h3>${{ number_format($totalDue,2) }}</h3>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card bg-info text-white">
            <div class="card-body">
                <h6>Cantidad de ventas</h6>
                <h3>{{ $totalCount }}</h3>
            </div>
        </div>
    </div>

</div>

{{-- Tabla --}}
<div class="card">
<div class="card-body table-responsive">

<table class="table table-bordered">
    <thead class="table-light">
        <tr>
            <th>No. Cotización</th>
            <th>Fecha</th>
            <th>Cliente</th>
            <th>Total</th>
            <th>Estado</th>
        </tr>
    </thead>
    <tbody>
        @foreach($cotizaciones as $c)
        <tr>
            <td>#{{ $c->id }}</td>
            <td>{{ $c->created_at->format('d/m/Y') }}</td>
            <td>{{ $c->cliente->nombre ?? '' }}</td>
            <td>${{ number_format($c->total,2) }}</td>
            <td>
                @if($c->estado=='aprobada')
                    <span class="badge bg-success">Pagada</span>
                @else
                    <span class="badge bg-warning text-dark">Pendiente</span>
                @endif
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

</div>
</div>

</div>
@endsection