@extends('admin.layout')
@section('content')
<style>
/* 🌈 Animasi Fade In */
.fade-section {
  opacity: 0;
  transform: translateY(20px);
  transition: opacity .7s ease-out, transform .7s ease-out;
}
.fade-section.show {
  opacity: 1;
  transform: translateY(0);
}
</style>

<div class="max-w-7xl mx-auto space-y-6 px-3 sm:px-6 fade-section">

  {{-- 📋 Header --}}
  <div class="bg-white border border-gray-200 rounded-xl p-5 flex items-center justify-between shadow-sm hover:shadow-md transition fade-section">
    <div>
      <h2 class="text-xl font-semibold text-gray-800 flex items-center gap-2">
       <svg width="40" height="40" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g fill="none"><path fill="#fff" d="m12 3.052l2.045 1.534l2.557-1.534l-2.557 5.624l4.09 5.114v5.113l-4.09 2.045h-4.09l-4.09-2.045V13.79l4.09-5.113l-2.557-5.625l2.557 1.534z"/><path fill="#bbd8ff" d="m6.376 19.925l.59-.472l-1.102-.55V13.79l1.593-1.99l-2.104-2.1l2.045-3.58l-2.556 1.023L2.285 6.12L4.33 9.7L.751 14.811v3.068l2.557 2.045zm11.248 0l-.59-.472l1.102-.55V13.79l-1.592-1.99l2.103-2.1l-2.045-3.58l2.556 1.023l2.557-1.023L19.67 9.7l3.579 5.112v3.068l-2.557 2.045zM14.045 8.677l1.278-2.814l-1.278.768L12 5.097L9.955 6.631l-1.278-.768l1.278 2.814z"/><path fill="#bbd8ff" d="m9.955 10.72l-4.09 5.114v3.069l4.09 2.045h4.09l4.09-2.045v-3.07l-4.09-5.112z"/><path stroke="#092f63" stroke-linecap="round" stroke-linejoin="round" d="M2.797 9.7h3.579m11.76.51l.51-.51l-2.044-3.58l2.556 1.023l2.557-1.023L19.67 9.7l3.579 5.112v3.068l-2.557 2.045h-.511M21.204 9.7h-3.58m-4.601 4.09h-2.046l-.51 1.022l.51 1.023h2.046l.51 1.022l-.51 1.023h-2.046M12 13.79v-1.023m0 6.136V17.88m4.09-9.203H7.91"/><path stroke="#092f63" stroke-linejoin="round" d="m12 3.052l2.045 1.534l2.557-1.534l-2.557 5.624l4.09 5.114v5.113l-4.09 2.045h-4.09l-4.09-2.045V13.79l4.09-5.113l-2.557-5.625l2.557 1.534z"/><path stroke="#092f63" stroke-linecap="round" stroke-linejoin="round" d="m5.864 10.21l-.51-.51l2.044-3.58l-2.556 1.023L2.285 6.12L4.33 9.7L.751 14.811v3.068l2.557 2.045h.511"/></g></svg>
        Riwayat Mutasi Saldo
      </h2>
      <p class="text-sm text-gray-500">Lihat semua perubahan saldo user (DEBIT / CREDIT)</p>
    </div>
  </div>

  {{-- 📊 Statistik Singkat --}}
  <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 fade-section">
    <div class="bg-white border border-gray-200 rounded-xl p-4 shadow-sm hover:shadow-md transition">
      <div class="text-xs text-gray-500">Total Debit (Pemotongan)</div>
      <div class="text-2xl font-bold text-red-600">
        - Rp {{ number_format($totalDebit, 0, ',', '.') }}
      </div>
    </div>

    <div class="bg-white border border-gray-200 rounded-xl p-4 shadow-sm hover:shadow-md transition">
      <div class="text-xs text-gray-500">Total Credit (Penambahan)</div>
      <div class="text-2xl font-bold text-green-600">
        + Rp {{ number_format($totalCredit, 0, ',', '.') }}
      </div>
    </div>

    <div class="bg-white border border-gray-200 rounded-xl p-4 shadow-sm hover:shadow-md transition">
      <div class="text-xs text-gray-500">Total Transaksi Log</div>
      <div class="text-2xl font-bold text-gray-800">
        {{ number_format($logs->total(), 0, ',', '.') }}
      </div>
    </div>
  </div>

{{-- 🔍 Form Pencarian --}}
  <form method="get" class="flex flex-col sm:flex-row gap-2 fade-section">
    <input type="text" name="q" value="{{ $q }}" placeholder="Cari USER ID / Nama / Alasan..." 
           class="w-full p-2 border rounded-lg focus:ring-2 focus:ring-blue-100 focus:border-blue-500 text-sm">
    <button class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition">
      Cari
    </button>
  </form>

  {{-- 📄 Tabel Riwayat --}}
  <div class="bg-white border border-gray-200 rounded-xl overflow-x-auto shadow-sm fade-section">
    <table class="w-full text-sm">
      <thead class="bg-gray-100 text-gray-700">
        <tr>
          <th class="p-3 text-left font-semibold">ID</th>
          <th class="p-3 text-left font-semibold">User</th>
          <th class="p-3 text-left font-semibold">Tipe</th>
          <th class="p-3 text-left font-semibold">Jumlah</th>
          <th class="p-3 text-left font-semibold">Alasan</th>
          <th class="p-3 text-left font-semibold">Tanggal</th>
        </tr>
      </thead>
      <tbody class="divide-y">
        @forelse($logs as $log)
          <tr class="hover:bg-gray-50 transition">
            <td class="p-3 font-mono text-gray-800">{{ $log->id }}</td>
            <td class="p-3 text-gray-700">
              <div class="font-medium">{{ $log->user->name ?? 'User Dihapus' }}</div>
              <div class="text-xs text-gray-500">ID: {{ $log->user_id }}</div>
            </td>
            <td class="p-3">
              @if($log->type === 'DEBIT')
                <span class="bg-red-100 text-red-600 px-2 py-1 text-xs rounded-md font-semibold">DEBIT</span>
              @else
                <span class="bg-green-100 text-green-600 px-2 py-1 text-xs rounded-md font-semibold">CREDIT</span>
              @endif
            </td>
            <td class="p-3 font-semibold {{ $log->type === 'DEBIT' ? 'text-red-600' : 'text-green-600' }}">
              {{ $log->type === 'DEBIT' ? '-' : '+' }} Rp {{ number_format($log->amount, 0, ',', '.') }}
            </td>
            <td class="p-3 text-gray-600">{{ $log->reason ?? '-' }}</td>
            <td class="p-3 text-gray-600">
              {{ $log->created_at->timezone('Asia/Jakarta')->format('Y-m-d H:i:s') }}
            </td>
          </tr>
        @empty
          <tr>
            <td colspan="6" class="p-4 text-center text-gray-500">Belum ada data mutasi saldo.</td>
          </tr>
        @endforelse
      </tbody>
    </table>
  </div>

  {{-- 📄 Pagination --}}
  <div class="fade-section">{{ $logs->links() }}</div>

</div>

<script>
// 🎬 Animasi fade in saat scroll
const fadeEls = document.querySelectorAll('.fade-section');
const observer = new IntersectionObserver(entries => {
  entries.forEach(e => {
    if (e.isIntersecting) e.target.classList.add('show');
  });
}, { threshold: 0.15 });
fadeEls.forEach(el => observer.observe(el));
</script>
@endsection
