@extends('layouts.app')

@section('title', 'Riwayat Pengiriman WhatsApp')

@section('content')
<div class="row">
    <div class="col-12">
        <div class="card card-primary card-outline">
            <div class="card-header">
                <h3 class="card-title">Filter Riwayat</h3>
            </div>
            <div class="card-body">
                <form action="{{ route('laporan.riwayat-wa') }}" method="GET">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>Pencarian</label>
                                <input type="text" name="search" class="form-control" placeholder="Nama / No WA / NIS" value="{{ request('search') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>Tanggal Mulai</label>
                                <input type="date" name="tanggal_mulai" class="form-control" value="{{ request('tanggal_mulai') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>Tanggal Sampai</label>
                                <input type="date" name="tanggal_sampai" class="form-control" value="{{ request('tanggal_sampai') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>Status</label>
                                <select name="status" class="form-control">
                                    <option value="">-- Semua Status --</option>
                                    <option value="berhasil" {{ request('status') == 'berhasil' ? 'selected' : '' }}>Berhasil</option>
                                    <option value="gagal" {{ request('status') == 'gagal' ? 'selected' : '' }}>Gagal</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>Jenis Pesan</label>
                                <select name="jenis_pesan" class="form-control">
                                    <option value="">-- Semua Jenis --</option>
                                    <option value="Bukti Transaksi" {{ request('jenis_pesan') == 'Bukti Transaksi' ? 'selected' : '' }}>Bukti Transaksi</option>
                                    <option value="Tagihan Tunggakan" {{ request('jenis_pesan') == 'Tagihan Tunggakan' ? 'selected' : '' }}>Tagihan Tunggakan</option>
                                    <option value="Tagihan Tunggakan massal" {{ request('jenis_pesan') == 'Tagihan Tunggakan massal' ? 'selected' : '' }}>Tagihan Tunggakan massal</option>
                                    <option value="Rekap Pembayaran" {{ request('jenis_pesan') == 'Rekap Pembayaran' ? 'selected' : '' }}>Rekap Pembayaran</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-9 d-flex align-items-end mb-3">
                            <button type="submit" class="btn btn-primary mr-2"><i class="fas fa-search"></i> Cari</button>
                            <a href="{{ route('laporan.riwayat-wa') }}" class="btn btn-default mr-2">Reset</a>
                            
                            <a href="{{ route('laporan.riwayat-wa.cetak', request()->all()) }}" target="_blank" class="btn btn-success ml-auto">
                                <i class="fas fa-print"></i> Cetak Riwayat
                            </a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>Tanggal</th>
                                <th>NIS</th>
                                <th>Nama Siswa</th>
                                <th>Nomor WhatsApp</th>
                                <th>Jenis Pesan</th>
                                <th>Dikirim Oleh</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($riwayats as $index => $riwayat)
                                <tr>
                                    <td>{{ $riwayats->firstItem() + $index }}</td>
                                    <td>{{ $riwayat->created_at->format('d-m-Y H:i') }}</td>
                                    <td>{{ $riwayat->nis ?? '-' }}</td>
                                    <td>{{ $riwayat->nama_siswa ?? '-' }}</td>
                                    <td>{{ $riwayat->no_wa }}</td>
                                    <td>{{ $riwayat->jenis_pesan }}</td>
                                    <td>{{ $riwayat->dikirimOleh->name ?? '-' }}</td>
                                    <td>
                                        @if($riwayat->status == 'berhasil')
                                            <span class="badge badge-success">Berhasil</span>
                                        @else
                                            <span class="badge badge-danger" title="{{ $riwayat->keterangan }}">Gagal</span>
                                            @if($riwayat->keterangan)
                                                <small class="d-block text-muted">{{ $riwayat->keterangan }}</small>
                                            @endif
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center">Tidak ada data riwayat pengiriman WhatsApp.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($riwayats->hasPages())
            <div class="card-footer">
                {{ $riwayats->links('pagination::bootstrap-4') }}
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
