@extends('layouts.app')

@section('title', '')

@section('content')
<div class="container-fluid">
    <h3 class="mb-4 text-dark">Daftar Detail Penjualan</h3>

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

    <div class="card shadow-sm">
        <div class="card-body">
            {{-- Tombol Tambah dan Cetak PDF --}}
            <div class="mb-3 d-flex justify-content-between">
                <a href="{{ route('detailpenjualan.create') }}" 
                   class="btn" 
                   style="background-color: #ff69b4; color: white;">
                    + Tambah Detail Penjualan
                </a>

                <a href="{{ route('detailpenjualan.cetak.pdf') }}" 
                   class="btn" 
                   style="background-color: #4a235a; color: white;" 
                   target="_blank">
                    <i class="bi bi-file-earmark-pdf"></i> Cetak PDF
                </a>
            </div>

            {{-- Tabel daftar detail penjualan --}}
            <div class="table-responsive">
                <table class="table table-bordered align-middle" style="border-color: #f8bbd0;">
                    <thead style="background-color: #f8bbd0; color: #4a235a;">
                        <tr>
                            <th>ID</th>
                            <th>Penjualan</th>
                            <th>Produk</th>
                            <th>Jumlah</th>
                            <th>Subtotal</th>
                            <th width="180">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($detailPenjualan as $d)
                            <tr>
                                <td>{{ $d->detail_id }}</td>
                                <td>
                                    {{ $d->penjualan->penjualan_id ?? '-' }} <br>
                                    <small class="text-muted">
                                        {{ $d->penjualan->tanggal_penjualan ?? '-' }}
                                    </small>
                                </td>
                                <td>{{ $d->produk->nama_produk ?? '-' }}</td>
                                <td>{{ $d->jumlah_produk }}</td>
                                <td>Rp {{ number_format($d->sub_total, 0, ',', '.') }}</td>
                                <td>
                                    <a href="{{ route('detailpenjualan.edit', $d->detail_id) }}" 
                                       class="btn btn-sm" 
                                       style="background-color: #87CEFA; color: white;">
                                        Edit
                                    </a>
                                    <form action="{{ route('detailpenjualan.destroy', $d->detail_id) }}" 
                                          method="POST" 
                                          class="d-inline">
                                        @csrf
                                        @method('DELETE')
                                        <button onclick="return confirm('Yakin hapus data ini?')" 
                                                class="btn btn-sm" 
                                                style="background-color: #ff69b4; color: white;">
                                            Hapus
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center">Belum ada detail penjualan</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
