@extends('layouts.admin')

@section('content')
  @php
    $contestType = $contest_type ?? 'paid';
    $pageTitle = $create_title ?? 'Add Competition';
    $heroTitle = $hero_create_title ?? 'Create New Competition';
    $heroDescription = $hero_create_description ?? 'Set up a fresh competition with prize pool, seat limit, join amount and rank-based rewards.';
    $formAction = isset($save_route_name) ? route($save_route_name) : route('admin.savecontest');
    $submitLabel = $submitLabel ?? 'Save Competition';
    $manageRouteName = $manage_route_name ?? 'admin.managecontest';
    $oldPrizeAmounts = old('prizedetail', ['']);
    $oldPrizeStarts = old('prizestratseat', ['']);
    $oldPrizeEnds = old('prizeendseat', ['']);
    $rowCount = max(count($oldPrizeAmounts), count($oldPrizeStarts), count($oldPrizeEnds), 1);
    $prizeRows = [];

    for ($index = 0; $index < $rowCount; $index++) {
        $prizeRows[] = [
            'amount' => $oldPrizeAmounts[$index] ?? '',
            'start' => $oldPrizeStarts[$index] ?? '',
            'end' => $oldPrizeEnds[$index] ?? '',
        ];
    }

    if ($contestType === 'free' && ! old('prizedetail')) {
        $prizeRows = [['amount' => 0, 'start' => 1, 'end' => 1]];
    }
  @endphp

  <div class="card-header">
    <h4>{{ $pageTitle }}</h4>
  </div>
  <div class="card-body">
    @include('admin.partials.flash-messages')
    @include('admin.contest.partials.contest-form', [
        'pageTitle' => $pageTitle,
        'heroTitle' => $heroTitle,
        'heroDescription' => $heroDescription,
        'formAction' => $formAction,
        'submitLabel' => $submitLabel,
        'prizeRows' => $prizeRows,
        'contestType' => $contestType,
        'contestTypeLabel' => $contest_type_label ?? 'Paid Competition',
        'manageRouteName' => $manageRouteName,
    ])
  </div>
@endsection
