@extends('layouts.app')

@section('page-title', 'Service Sales')

@section('content')
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-md-6">
            <h2>Service Sales</h2>
        </div>
        <div class="col-md-6 text-end">
            <a href="{{ route('service-sales.create') }}" class="btn btn-primary">
                <i class="fas fa-plus"></i> New Service Sale
            </a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover table-sm">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Service</th>
                            <th>Customer</th>
                            <th>QTY</th>
                            <th>Total Sales</th>
                            <th>Payment</th>
                            <th>Due</th>
                            <th>COGS</th>
                            <th>Gross Profit</th>
                            <th>Invoice #</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $totalSales = $serviceSales->sum('sales');
                            $totalPayment = $serviceSales->sum('payment');
                            $totalDue = $serviceSales->sum('due');
                            $totalCogs = $serviceSales->sum('cogs');
                            $totalGross = $serviceSales->sum('gross_profit');
                        @endphp

                        @forelse($serviceSales as $record)
                            <tr>
                                <td>{{ $record->date ? $record->date->format('Y-m-d') : '-' }}</td>
                                <td>{{ $record->service->name ?? '-' }}</td>
                                <td>{{ $record->customer->name ?? '-' }}</td>
                                <td>{{ $record->qty ?? '-' }}</td>
                                <td>{{ $record->sales ? number_format($record->sales, 2) : '-' }}</td>
                                <td>{{ $record->payment ? number_format($record->payment, 2) : '-' }}</td>
                                <td>{{ $record->due ? number_format($record->due, 2) : '-' }}</td>
                                <td>{{ $record->cogs ? number_format($record->cogs, 2) : '-' }}</td>
                                <td>{{ $record->gross_profit ? number_format($record->gross_profit, 2) : '-' }}</td>
                                <td>
                                    @if($record->invoice_id)
                                        <a href="{{ route('invoices.show', $record->invoice_id) }}">
                                            {{ \App\Models\Invoice::find($record->invoice_id)?->invoice_no ?? '-' }}
                                        </a>
                                    @else
                                        -
                                    @endif
                                </td>
                                <td>
                                    <a href="{{ route('service-sales.edit', $record->id) }}" class="btn btn-sm btn-primary">Edit</a>
                                    <form action="{{ route('service-sales.destroy', $record->id) }}" method="POST" style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="11" class="text-center">No service sales found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                    <tfoot>
                        <tr class="fw-bold">
                            <td colspan="4">Total</td>
                            <td>{{ number_format($totalSales, 2) }}</td>
                            <td>{{ number_format($totalPayment, 2) }}</td>
                            <td>{{ number_format($totalDue, 2) }}</td>
                            <td>{{ number_format($totalCogs, 2) }}</td>
                            <td>{{ number_format($totalGross, 2) }}</td>
                            <td></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection

