@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
  <div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
      <h5 class="mb-0">Edit Joined Subscription</h5>
      <a href="{{ route('dashboard.my-subscriptions') }}" class="btn btn-sm btn-outline-secondary">Back</a>
    </div>

    <div class="card-body">
      @if($errors->any())
        <div class="alert alert-danger">
          <ul class="mb-0">
            @foreach($errors->all() as $error)
              <li>{{ $error }}</li>
            @endforeach
          </ul>
        </div>
      @endif

      <div class="row mb-4">
        <div class="col-md-4">
          <small class="text-muted d-block">Member</small>
          <strong>{{ $member->user?->name ?? 'N/A' }}</strong>
        </div>
        <div class="col-md-4">
          <small class="text-muted d-block">Service</small>
          <strong>{{ $member->sharedSubscription?->service?->name ?? 'N/A' }}</strong>
        </div>
        <div class="col-md-4">
          <small class="text-muted d-block">Plan</small>
          <strong>{{ $member->sharedSubscription?->servicePlan?->name ?? 'N/A' }}</strong>
        </div>
      </div>

      <form method="POST" action="{{ route('dashboard.subscription-members.update', $member) }}">
        @csrf
        @method('PUT')

        <div class="row g-3">
          <div class="col-md-4">
            <label class="form-label">Status</label>
            <select name="status" class="form-select" required>
              @foreach($statuses as $status)
                <option value="{{ $status }}" @selected(old('status', $member->status) === $status)>
                  {{ ucfirst(str_replace('_', ' ', $status)) }}
                </option>
              @endforeach
            </select>
          </div>

          <div class="col-md-4">
            <label class="form-label">Slots Taken</label>
            <input type="number" min="1" name="slots_taken" class="form-control" value="{{ old('slots_taken', $member->slots_taken) }}" required>
          </div>

          <div class="col-md-4">
            <label class="form-label">Subscription Amount</label>
            <input type="number" min="0" step="0.01" name="subscription_amount" class="form-control" value="{{ old('subscription_amount', $member->subscription_amount) }}" required>
          </div>

          <div class="col-md-4">
            <label class="form-label">Payment Verified</label>
            <select name="payment_verified" class="form-select" required>
              <option value="1" @selected((string) old('payment_verified', (int) $member->payment_verified) === '1')>Yes</option>
              <option value="0" @selected((string) old('payment_verified', (int) $member->payment_verified) === '0')>No</option>
            </select>
          </div>

          <div class="col-md-4">
            <label class="form-label">Joined At</label>
            <input type="datetime-local" name="joined_at" class="form-control" value="{{ old('joined_at', $member->joined_at?->format('Y-m-d\TH:i')) }}">
          </div>

          <div class="col-md-4">
            <label class="form-label">Expires At</label>
            <input type="datetime-local" name="expires_at" class="form-control" value="{{ old('expires_at', $member->expires_at?->format('Y-m-d\TH:i')) }}">
          </div>

          <div class="col-md-4">
            <label class="form-label">Cancel Requested At</label>
            <input type="datetime-local" name="cancel_requested_at" class="form-control" value="{{ old('cancel_requested_at', $member->cancel_requested_at?->format('Y-m-d\TH:i')) }}">
          </div>
        </div>

        <div class="mt-4 d-flex gap-2">
          <button type="submit" class="btn btn-primary">Save Changes</button>
          <a href="{{ route('dashboard.my-subscriptions') }}" class="btn btn-outline-secondary">Cancel</a>
        </div>
      </form>
    </div>
  </div>
</div>
@endsection
