@extends('layouts.app')

@php use Illuminate\Support\Str; @endphp

@section('content')
<!-- Background seluruh halaman -->
<div style="background-color: #e0e0e0; width: 100%; min-height: 100vh; border-radius: 15px; padding: 1rem;">

    <!-- Header -->
    <div style="background-color: #000; color: #fff; padding: 0.1rem; border-radius: 0.5rem;">
        <h1 class="m-0">Pemasukan</h1>
    </div>

    <!-- Konten utama -->
    <div class="container mt-4">
        <div class="card p-4 border border-secondary" style="border-radius: 15px; background-color: #fff;">

            <!-- Tombol atas -->
            <div class="d-flex justify-content-between mb-3">
                <a href="{{ route('dashboard') }}" class="btn btn-secondary">Kembali</a>
                <a href="{{ route('pemasukan.create') }}" class="btn btn-primary">Tambah Pemasukan</a>
            </div>

            <!-- Filter waktu -->
            <form method="GET" action="{{ route('pemasukan.index') }}" class="mb-4">
                <div class="row g-2">
                    <div class="col-md-3">
                        <select name="filter" class="form-control">
                            <option value="">-- Filter Waktu --</option>
                            <option value="hari" {{ request('filter') == 'hari' ? 'selected' : '' }}>Hari Ini</option>
                            <option value="minggu" {{ request('filter') == 'minggu' ? 'selected' : '' }}>Minggu Ini</option>
                            <option value="bulan" {{ request('filter') == 'bulan' ? 'selected' : '' }}>Bulan Ini</option>
                            <option value="tahun" {{ request('filter') == 'tahun' ? 'selected' : '' }}>Tahun Ini</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-success">Terapkan</button>
                    </div>
                </div>
            </form>

            <!-- Tabel data -->
            <div class="table-responsive">
                <table class="table table-bordered table-hover mb-0">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Tanggal</th>
                            <th>Nama Donatur</th>
                            <th>Kategori</th>
                            <th>Quantity</th>
                            <th>Harga Satuan</th>
                            <th>Jumlah</th>
                            <th>Keterangan</th>
                            <th>Upload Kuitansi</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($pemasukans as $index => $item)
                            <tr>
                                <td>{{ $pemasukans->firstItem() + $index }}</td>
                                <td>{{ \Carbon\Carbon::parse($item->tanggal)->format('d-m-Y') }}</td>
                                <td>{{ $item->nama }}</td>
                                <td>{{ ucfirst($item->kategori) }}</td>
                                <td>{{ $item->kategori === 'barang' ? $item->quantity : '-' }}</td>
                                <td>
                                    @if($item->kategori === 'barang' && $item->harga)
                                        Rp {{ number_format($item->harga, 0, ',', '.') }}
                                    @else
                                        -
                                    @endif
                                </td>
                                <td>
                                    @if($item->kategori === 'uang')
                                        Rp {{ number_format($item->jumlah, 0, ',', '.') }}
                                    @elseif($item->kategori === 'barang')
                                        Rp {{ number_format(($item->harga ?? 0) * ($item->quantity ?? 0), 0, ',', '.') }}
                                    @else
                                        -
                                    @endif
                                </td>
                                <td>{{ $item->keterangan ?? '-' }}</td>

                                <!-- Upload Kuitansi -->
                                <td class="text-center">
                                    @if ($item->bukti)
                                        @php
                                            $buktiPath = Str::contains($item->bukti, 'bukti_sumbangan') ? $item->bukti : 'bukti_sumbangan/' . $item->bukti;
                                        @endphp
                                        <img src="{{ asset('storage/' . $buktiPath) }}"
                                            alt="Bukti"
                                            class="img-thumbnail"
                                            style="max-height: 80px;">
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </td>

                                <!-- Aksi -->
                                <!-- Aksi -->
<td class="text-center">
    <a href="{{ route('pemasukan.edit', $item->id) }}" class="btn btn-sm btn-warning mb-1" title="Edit">✏️</a>

    <form action="{{ route('pemasukan.destroy', $item->id) }}" method="POST" onsubmit="return confirm('Hapus data ini?')" style="display:inline;">
        @csrf
        @method('DELETE')
        <button class="btn btn-sm btn-danger" title="Hapus">🗑️</button>
    </form>
</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="10" class="text-center">Belum ada data sumbangan.</td>
                            </tr>
                        @endforelse
                    </tbody>

                    @if($pemasukans->count() > 0)
                        <tfoot>
                            <tr class="fw-bold">
                                <td colspan="6" class="text-end">Total Sumbangan</td>
                                <td colspan="4">Rp {{ number_format($total, 0, ',', '.') }}</td>
                            </tr>
                        </tfoot>
                    @endif
                </table>
            </div>

            <!-- Pagination -->
            <div class="mt-3 d-flex justify-content-between align-items-center">
                <div>
                    {{ $pemasukans->appends(['filter' => request('filter')])->links('pagination::bootstrap-4') }}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- SweetAlert jika ada session success -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
@if(session('success'))
<script>
    Swal.fire({
        toast: true,
        position: 'top-end',
        icon: 'success',
        title: @json(session('success')),
        showConfirmButton: false,
        timer: 3000,
        timerProgressBar: true,
    });
</script>
@endif

@endsection
