@extends('backend.layouts.app')
@section('content')
<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6 pb-2 pb-sm-0"><h1>User Subscriptions</h1></div>
        <div class="col-sm-6 text-sm-right">
          <a href="{{ url('admin/subscription-plans') }}" class="btn btn-secondary">← Plans</a>
        </div>
      </div>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">
      @if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif

      {{-- Grant subscription form --}}
      <div class="card mb-4">
        <div class="card-header"><strong>Grant Subscription to User</strong></div>
        <form action="{{ route('admin.user-subscriptions.grant') }}" method="post">
          @csrf
          <div class="card-body">
            @if($errors->any())
              <div class="alert alert-danger"><ul class="mb-0">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul></div>
            @endif
            <div class="row">
              <div class="col-md-5 form-group">
                <label>User</label>
                <select name="user_id" class="form-control select2" style="width:100%">
                  <option selected disabled>Select User</option>
                  @foreach($users as $u)
                    <option value="{{ $u->id }}">{{ $u->name }} ({{ $u->email }})</option>
                  @endforeach
                </select>
              </div>
              <div class="col-md-5 form-group">
                <label>Plan</label>
                <select name="plan_id" class="form-control" style="width:100%">
                  <option selected disabled>Select Plan</option>
                  @foreach($plans as $p)
                    <option value="{{ $p->id }}">{{ $p->name }} – ${{ number_format($p->price, 2) }} / {{ $p->duration_days }} days</option>
                  @endforeach
                </select>
              </div>
              <div class="col-md-2 form-group d-flex align-items-end">
                <button type="submit" class="btn btn-success w-100">Grant</button>
              </div>
            </div>
          </div>
        </form>
      </div>

      <div class="row">
        <div class="col-md-12">
          <div class="card">
            <div class="card-body table-responsive p-3">
              <table id="subTable" style="width:100%;" class="table table-striped">
                <thead>
                  <tr>
                    <th>S.No.</th><th>User</th><th>Email</th><th>Plan</th>
                    <th>Expires</th><th>Status</th><th>Actions</th>
                  </tr>
                </thead>
                <tbody></tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>
@endsection
@push('scripts')
<script>
$(document).ready(function () {
  $('#subTable').DataTable({
    processing: true, serverSide: true,
    ajax: { url: '{{ route('admin.user-subscriptions.index') }}' },
    columns: [
      { data: null, orderable: false, searchable: false, render: (d,t,r,m) => m.row+1 },
      { data: 'user_name', name: 'user_name', orderable: false, searchable: false },
      { data: 'user_email', name: 'user_email', orderable: false, searchable: false },
      { data: 'plan_name', name: 'plan_name', orderable: false, searchable: false },
      { data: 'expires_at', name: 'expires_at' },
      { data: 'status', name: 'status' },
      { data: 'actions', name: 'actions', orderable: false, searchable: false },
    ],
    responsive: true,
  });
});
</script>
@endpush
