@extends('layouts.subscriptions')

@push('style')
<style>
  .join-back {
    width: 34px;
    height: 34px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #111;
    text-decoration: none;
  }

  .join-logo {
    width: 58px;
    height: 58px;
    border-radius: 18px;
    overflow: hidden;
    background: #111;
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    text-transform: uppercase;
    position: relative;
  }

  .join-logo img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .join-warning {
    border: 0;
    border-radius: 8px;
    background: #fff0dd;
    color: #292929;
    padding: 12px 14px;
    font-size: 14px;
  }

  .period-box {
    border: 1px solid #efefef;
    border-radius: 8px;
    background: #f5f5f5;
    text-align: center;
    font-weight: 700;
    padding: 10px 12px;
  }

  .seat-stepper button {
    border: 1px solid #e5e5e5;
    background: #fff;
    color: #111;
    width: 34px;
    height: 34px;
    border-radius: 8px;
  }

  .price-old {
    text-decoration: line-through;
    color: #999;
    font-size: 28px;
    font-weight: 500;
  }

  .price-new {
    color: #ff8f00;
    font-size: 46px;
    font-weight: 700;
    line-height: 1;
  }

  .price-new span {
    font-size: 20px;
  }

  .pay-btn {
    border-radius: 8px;
    border: 0;
    background: #ffc376;
    color: #fff;
    min-width: 210px;
    min-height: 46px;
    font-weight: 700;
  }

  .pay-btn:hover {
    background: #f2b05a;
    color: #fff;
  }
</style>
@endpush

@section('content')
@php
  $periodStart = $subscription->starts_at ?? now();
  $periodEnd = $subscription->ends_at ?? $periodStart->copy()->addDays(30);
  $maxSlots = max((int) $subscription->available_slots, 1);
  $initialSlots = min(max((int) old('slots_taken', 1), 1), $maxSlots);
  $monthlyPrice = (float) $subscription->price_per_slot * $initialSlots;
  $strikedPrice = (float) $subscription->total_price;
@endphp

<div class="container-xxl flex-grow-1 container-p-y">
  <div class="d-flex align-items-center gap-2 mb-3">
    <a href="{{ route('subscriptions.show', $subscription) }}" class="join-back">
      <i class="icon-base ti tabler-chevron-left icon-md"></i>
    </a>
    <div class="join-logo">
      <span>{{ strtoupper(substr($subscription->service->name, 0, 2)) }}</span>
      @if(!empty($subscription->service->logo))
        <img src="{{ asset($subscription->service->logo) }}" alt="{{ $subscription->service->name }}" onerror="this.remove()">
      @endif
    </div>
    <h2 class="mb-0 fw-bold">{{ $subscription->service->name }} {{ $subscription->servicePlan->name }}</h2>
  </div>

  @if($errors->any())
    <div class="alert alert-danger">
      {{ $errors->first() }}
    </div>
  @endif

  <div class="text-muted mb-3">Monthly</div>

  <div class="join-warning mb-4">
    <i class="icon-base ti tabler-bulb icon-sm me-1"></i>
    {{ $subscription->credentials_shared ? 'Service access is shared by credentials. After payment, details are available in your subscription panel.' : 'Service access is shared through invitation workflow after the owner approves your request.' }}
  </div>

  <form method="POST" action="{{ route('subscriptions.join.store', $subscription) }}" id="joinForm">
    @csrf
    <input type="hidden" name="slots_taken" id="slotsTakenInput" value="{{ $initialSlots }}">

    <div class="row g-3 align-items-center border-top border-bottom py-3 mb-3">
      <div class="col-lg-4 fw-semibold">Subscription period (30 days):</div>
      <div class="col-lg-3">
        <div class="period-box">{{ $periodStart->format('M.d,Y') }}</div>
      </div>
      <div class="col-lg-2 text-center fw-semibold">to</div>
      <div class="col-lg-3">
        <div class="period-box">{{ $periodEnd->format('M.d,Y') }}</div>
      </div>
    </div>

    <div class="d-flex justify-content-between align-items-center border-bottom py-3 mb-3">
      <div class="fw-semibold">Number of seats</div>
      <div class="d-flex align-items-center gap-2">
        <div class="seat-stepper d-flex align-items-center gap-2">
          <button type="button" id="decreaseSlot">-</button>
          <strong>x <span id="seatCount">{{ $initialSlots }}</span></strong>
          <button type="button" id="increaseSlot">+</button>
        </div>
      </div>
    </div>

    <div class="d-flex justify-content-between align-items-center border-bottom py-3 mb-3">
      <div class="fw-semibold">Subscription price :</div>
      <div class="text-end">
        <div class="price-old" id="oldPrice">{{ number_format($strikedPrice, 2) }}&euro; <span class="fs-6">/ month</span></div>
        <div class="price-new"><span id="newPrice">{{ number_format($monthlyPrice, 2) }}</span> <span>&euro; / month</span></div>
      </div>
    </div>

    <div class="form-check mb-3">
      <input class="form-check-input" type="checkbox" id="termsCheck" required>
      <label class="form-check-label text-muted" for="termsCheck">
        I acknowledge and agree <a href="#" class="text-warning fw-semibold">Platform Usage Instructions</a>
      </label>
    </div>

    <button class="pay-btn" type="submit">Pay now</button>
  </form>

  <div class="text-muted mt-4">
    <p class="mb-2">You will not be able to join and be charged for a subscription until the owner agrees.</p>
    <p class="mb-0">If the user accepts your request, cancellation right may not apply as per policy.</p>
  </div>
</div>

<script>
(() => {
  const pricePerSlot = {{ (float) $subscription->price_per_slot }};
  const maxSlots = {{ $maxSlots }};
  const seatCountEl = document.getElementById('seatCount');
  const seatInputEl = document.getElementById('slotsTakenInput');
  const priceEl = document.getElementById('newPrice');
  const decreaseBtn = document.getElementById('decreaseSlot');
  const increaseBtn = document.getElementById('increaseSlot');

  let slots = Number(seatInputEl.value || 1);

  const render = () => {
    slots = Math.max(1, Math.min(maxSlots, slots));
    seatInputEl.value = String(slots);
    seatCountEl.textContent = String(slots);
    priceEl.textContent = (slots * pricePerSlot).toFixed(2);
    decreaseBtn.disabled = slots <= 1;
    increaseBtn.disabled = slots >= maxSlots;
  };

  decreaseBtn.addEventListener('click', () => {
    slots -= 1;
    render();
  });

  increaseBtn.addEventListener('click', () => {
    slots += 1;
    render();
  });

  render();
})();
</script>
@endsection
