@extends('layouts.admin')

@section('content')
  @php
    $rechargeAmounts = collect(explode(',', (string) $sitedetails->appamount))
        ->map(fn ($amount) => trim((string) $amount))
        ->filter(fn ($amount) => $amount !== '' && is_numeric($amount))
        ->values();
  @endphp

  <div class="card-header">
    <h4>App Management</h4>
  </div>
  <div class="card-body">
    @include('admin.partials.flash-messages')

    <div class="portal-page-shell">
      <div class="portal-hero-card">
        <div class="portal-hero-main">
          <div class="portal-hero-copy">
            <h5>{{ $sitedetails->appname ?: 'Just Exam League' }}</h5>
            <p>Yahan se platform ka display name, wallet recharge presets aur app branding centrally manage hoti hai.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">{{ number_format($rechargeAmounts->count()) }} recharge preset{{ $rechargeAmounts->count() === 1 ? '' : 's' }}</span>
              <span class="portal-chip">Site ID {{ $sitedetails->id }}</span>
              <span class="portal-chip">{{ Auth::user()->name }}</span>
            </div>
          </div>
          <img src="{{ $siteLogoUrl }}" alt="{{ $sitedetails->appname }}" class="portal-hero-image">
        </div>
      </div>

      <div class="portal-summary-grid">
        <div class="portal-summary-card">
          <span>Site Name</span>
          <strong>{{ $sitedetails->appname ?: '-' }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Recharge Presets</span>
          <strong>{{ number_format($rechargeAmounts->count()) }}</strong>
        </div>
        <div class="portal-summary-card">
          <span>Lowest Recharge</span>
          <strong>{{ $rechargeAmounts->isNotEmpty() ? 'Rs. '.number_format((float) $rechargeAmounts->min(), 2) : '-' }}</strong>
        </div>
      </div>

      <form action="{{ route('admin.savemanagement') }}" method="post" class="portal-form-shell">
        @csrf
        <input type="hidden" name="editid" value="{{ $sitedetails->id }}">
        <input type="hidden" name="userid" value="{{ Auth::user()->id }}">

        <div class="portal-form-section">
          <h6>Platform Identity</h6>
          <p>Student aur institute-facing branding isi section se control hoti hai.</p>
          <div class="portal-form-grid">
            <div class="form-group portal-form-group portal-form-group-full">
              <label for="pagename">Site Name</label>
              <input
                type="text"
                class="form-control"
                id="pagename"
                name="pagename"
                value="{{ old('pagename', $sitedetails->appname) }}"
                maxlength="200"
                placeholder="Site Name"
                required
              >
            </div>
          </div>
        </div>

        <div class="portal-form-section">
          <h6>Wallet Recharge Presets</h6>
          <p>Comma-separated numeric values dijiye. Example: <code>100, 250, 500, 1000</code>.</p>
          <div class="portal-form-grid">
            <div class="form-group portal-form-group portal-form-group-full">
              <label for="rechargeamt">Recharge Amount List</label>
              <input
                type="text"
                class="form-control"
                id="rechargeamt"
                name="rechargeamt"
                value="{{ old('rechargeamt', $sitedetails->appamount) }}"
                maxlength="200"
                placeholder="100, 250, 500, 1000"
                required
              >
            </div>
          </div>

          @if($rechargeAmounts->isNotEmpty())
            <div class="portal-chip-list mt-2">
              @foreach($rechargeAmounts as $amount)
                <span class="portal-chip">Rs. {{ number_format((float) $amount, 2) }}</span>
              @endforeach
            </div>
          @endif
        </div>

        <div class="portal-form-section">
          <h6>Branding</h6>
          <p>App logo update karne ke baad portal screens par latest branding reflect hogi.</p>
          <div class="portal-upload-shell">
            <div id="targetLayer" class="portal-upload-preview">
              <img src="{{ $siteLogoUrl }}" alt="{{ $sitedetails->appname }}">
              <input type="hidden" name="photoid" value="{{ $sitedetails->applogo }}">
            </div>
            <div class="loading" style="display:none; text-align:center;">
              <img src="{{ asset('public/admin/loadspinner.gif') }}" style="width:80px;" alt="Loading">
            </div>
            <div class="portal-form-actions">
              <p class="portal-inline-note">Recommended: square logo ya clean transparent background image.</p>
              <div>
                <a id="newuploadphoto" href="#" class="btn btn-outline-primary">Upload Logo</a>
                <button type="submit" class="btn btn-primary">Update App Settings</button>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>

  @include('admin.partials.attachment-upload-form')
@endsection
