@extends('layouts.admin')
@section('content')

<div class="card mb-3">
  <div class="card-body">
    <h5>User Payments Report</h5>
    <form method="POST" action="{{ url('/user-payments-report') }}">
      @csrf
      <div class="row">
        <div class="col-md-4">
          <label>Select User:</label>
          <select name="user_id" class="form-control">
            <option value="all" {{ isset($userId) && $userId == 'all' ? 'selected' : '' }}>All Users</option>
            @foreach ($users as $user)
            <option value="{{ $user->id }}" {{ (isset($userId) && $userId == $user->id) ? 'selected' : '' }}>
              {{ $user->name }}
            </option>
            @endforeach
          </select>
        </div>

        <div class="col-md-6">
          <label>From:</label>
          <div class="d-flex">
            <input type="date" name="date1" class="form-control me-2"
              value="{{ $date1 ?? date('Y-m-d') }}" required>
            <input type="time" name="time1" class="form-control"
              value="{{ $time1 ?? '00:00' }}" required>
          </div>
        </div>

        <div class="col-md-6">
          <label>To:</label>
          <div class="d-flex">
            <input type="date" name="date2" class="form-control me-2"
              value="{{ $date2 ?? date('Y-m-d') }}" required> <br>
            <input type="time" name="time2" class="form-control"
              value="{{ $time2 ?? '23:59' }}" required>
          </div>
        </div>
      </div>

      <div class="row mt-3">
        <div class="col-md-2 ms-auto">
          <button type="submit" class="btn btn-primary w-100">
            <i class="fas fa-search"></i> Generate
          </button>
        </div>
      </div>
    </form>
  </div>
</div>

@if (!empty($payments) && count($payments) > 0)
<div class="card">
  <div class="card-body">
    <h6>Results:</h6>
    <p>
      User: {{ $userId == 'all' ? 'All Users' : $users->firstWhere('id', $userId)->name }}<br>
      Period: {{ $date1 }} {{ $time1 }} → {{ $date2 }} {{ $time2 }} <br>
      Total Collected: E {{ number_format($total, 2) }}
    </p>

    <div class="table-responsive">
      <table id="basic-btn" class="table table-bordered table-striped">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th>User</th>
            <th>Member</th>
            <th>Scheme Number</th>
            <th>Amount Paid</th>
            <th>Payment Date</th>
            <th>Payment Method</th>
          </tr>
        </thead>
        <tbody>
          @foreach ($payments as $key => $payment)
          <tr>
            <td>{{ $key + 1 }}</td>
            <td>{{ $payment->user_name ?? 'N/A' }}</td>
            <td>{{ $payment->member_surname ?? '' }} {{ $payment->member_name ?? '' }}</td>
            <td>{{ $payment->scheme}}</td>
            <td>E {{ number_format($payment->amount_paid, 2) }}</td>
            <td>{{ $payment->payment_date }}</td>
           <td>{{ $payment->payment_mode_name ?? 'N/A' }}</td>

          </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </div>
</div>
@endif

@endsection