@extends('coach.layouts.app')

@section('title', $plan->name)

@section('content')
    <div class="flex items-center justify-between mb-2">
        <h1 class="text-xl font-bold">{{ $plan->name }}</h1>
        <a href="{{ route('coach.nutrition-plans.edit', $plan) }}" class="text-sm text-blue-600 hover:underline">ویرایش اطلاعات برنامه</a>
    </div>
    <p class="text-sm text-gray-500 mb-6">
        برای {{ $plan->athlete->user->fullName() }} · وضعیت: {{ $plan->status->label() }}
    </p>

    {{-- افزودن روز --}}
    <form method="POST" action="{{ route('coach.nutrition-plans.days.store', $plan) }}" class="flex gap-2 mb-6">
        @csrf
        <input type="text" name="label" placeholder="نام روز (مثلاً «روز ۱» یا «شنبه»)" class="border rounded-lg px-3 py-2 text-sm flex-1" required>
        <button class="bg-gray-900 text-white text-sm px-4 py-2 rounded-lg">+ افزودن روز</button>
    </form>

    <div class="space-y-6 mb-8">
        @forelse($plan->days as $day)
            <div class="bg-white rounded-xl shadow-sm p-4">
                <div class="flex items-center justify-between mb-3">
                    <h2 class="font-semibold">{{ $day->label }}
                        <span class="text-xs text-gray-400 font-normal">— مجموع کالری: {{ $day->totalCalories() }}</span>
                    </h2>
                    <div class="flex items-center gap-2 text-xs">
                        <form method="POST" action="{{ route('coach.nutrition-days.move-up', $day) }}"><button class="text-gray-400 hover:text-gray-700">▲</button></form>
                        <form method="POST" action="{{ route('coach.nutrition-days.move-down', $day) }}"><button class="text-gray-400 hover:text-gray-700">▼</button></form>
                        <form method="POST" action="{{ route('coach.nutrition-days.destroy', $day) }}" onsubmit="return confirm('این روز و همه‌ی وعده‌هایش حذف شود؟');">
                            @csrf @method('DELETE')
                            <button class="text-red-600 hover:underline">حذف روز</button>
                        </form>
                    </div>
                </div>

                {{-- افزودن وعده --}}
                <form method="POST" action="{{ route('coach.nutrition-days.meals.store', $day) }}" class="flex flex-wrap gap-2 mb-3">
                    @csrf
                    <input type="text" name="name" placeholder="نام وعده (صبحانه/ناهار/...)" class="border rounded-lg px-3 py-2 text-sm flex-1 min-w-[140px]" required>
                    <input type="time" name="consumption_time" class="border rounded-lg px-3 py-2 text-sm">
                    <button class="bg-gray-100 text-sm px-4 py-2 rounded-lg">+ افزودن وعده</button>
                </form>

                <div class="space-y-3">
                    @forelse($day->meals as $meal)
                        <div class="border rounded-lg p-3">
                            <div class="flex items-center justify-between mb-2">
                                <div class="text-sm font-medium">
                                    {{ $meal->name }}
                                    @if($meal->consumption_time) <span class="text-gray-400 text-xs">({{ $meal->consumption_time->format('H:i') }})</span> @endif
                                    <span class="text-gray-400 text-xs"> — {{ $meal->totalCalories() }} کالری</span>
                                </div>
                                <form method="POST" action="{{ route('coach.nutrition-meals.destroy', $meal) }}" onsubmit="return confirm('این وعده حذف شود؟');">
                                    @csrf @method('DELETE')
                                    <button class="text-red-600 text-xs hover:underline">حذف وعده</button>
                                </form>
                            </div>

                            <div class="space-y-1 mb-2">
                                @forelse($meal->items as $item)
                                    <div class="flex items-center justify-between text-xs bg-gray-50 rounded px-2 py-1.5">
                                        <span>
                                            {{ $item->food_name }} — {{ $item->amount }} {{ $item->unit->label() }}
                                            @if($item->calories) ({{ $item->calories }} کالری) @endif
                                        </span>
                                        <form method="POST" action="{{ route('coach.nutrition-items.destroy', $item) }}">
                                            @csrf @method('DELETE')
                                            <button class="text-red-600 hover:underline">حذف</button>
                                        </form>
                                    </div>
                                @empty
                                    <p class="text-xs text-gray-400">هنوز قلم غذایی اضافه نشده.</p>
                                @endforelse
                            </div>

                            <form method="POST" action="{{ route('coach.nutrition-meals.items.store', $meal) }}" class="flex flex-wrap gap-1">
                                @csrf
                                <input type="text" name="food_name" placeholder="نام غذا" class="border rounded px-2 py-1 text-xs flex-1 min-w-[100px]" required>
                                <input type="number" step="0.1" name="amount" placeholder="مقدار" class="border rounded px-2 py-1 text-xs w-20">
                                <select name="unit" class="border rounded px-2 py-1 text-xs">
                                    <option value="gram">گرم</option>
                                    <option value="piece">عدد</option>
                                    <option value="cup">پیمانه</option>
                                    <option value="tablespoon">قاشق غذاخوری</option>
                                    <option value="ml">میلی‌لیتر</option>
                                    <option value="slice">برش</option>
                                    <option value="other">سایر</option>
                                </select>
                                <input type="number" name="calories" placeholder="کالری" class="border rounded px-2 py-1 text-xs w-20">
                                <button class="bg-gray-900 text-white text-xs rounded px-3 py-1">+ افزودن قلم</button>
                            </form>
                        </div>
                    @empty
                        <p class="text-gray-400 text-sm">هنوز وعده‌ای اضافه نشده.</p>
                    @endforelse
                </div>
            </div>
        @empty
            <p class="text-gray-400 text-center py-10">هنوز روزی اضافه نشده است.</p>
        @endforelse
    </div>

    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        {{-- مواد ممنوع --}}
        <div class="bg-white rounded-xl shadow-sm p-4">
            <h2 class="font-semibold mb-3">مواد غذایی ممنوع</h2>
            <form method="POST" action="{{ route('coach.nutrition-plans.forbidden-foods.store', $plan) }}" class="flex gap-2 mb-3">
                @csrf
                <input type="text" name="food_name" placeholder="نام ماده غذایی" class="border rounded-lg px-3 py-2 text-sm flex-1" required>
                <button class="bg-gray-100 text-sm px-3 py-2 rounded-lg">+</button>
            </form>
            <ul class="space-y-1 text-sm">
                @forelse($plan->forbiddenFoods as $food)
                    <li class="flex items-center justify-between bg-red-50 text-red-700 rounded px-2 py-1">
                        {{ $food->food_name }}
                        <form method="POST" action="{{ route('coach.forbidden-foods.destroy', $food) }}">
                            @csrf @method('DELETE')
                            <button class="text-red-500 hover:underline text-xs">حذف</button>
                        </form>
                    </li>
                @empty
                    <li class="text-gray-400 text-xs">موردی ثبت نشده.</li>
                @endforelse
            </ul>
        </div>

        {{-- مکمل‌ها --}}
        <div class="bg-white rounded-xl shadow-sm p-4">
            <h2 class="font-semibold mb-3">مکمل‌ها</h2>
            <form method="POST" action="{{ route('coach.nutrition-plans.supplements.store', $plan) }}" class="flex flex-wrap gap-2 mb-3">
                @csrf
                <input type="text" name="name" placeholder="نام مکمل" class="border rounded-lg px-3 py-2 text-sm flex-1 min-w-[100px]" required>
                <input type="text" name="amount" placeholder="مقدار" class="border rounded-lg px-3 py-2 text-sm w-24">
                <input type="text" name="consumption_time" placeholder="زمان مصرف" class="border rounded-lg px-3 py-2 text-sm w-28">
                <button class="bg-gray-100 text-sm px-3 py-2 rounded-lg">+</button>
            </form>
            <ul class="space-y-1 text-sm">
                @forelse($plan->supplements as $supplement)
                    <li class="flex items-center justify-between bg-gray-50 rounded px-2 py-1">
                        {{ $supplement->name }} — {{ $supplement->amount }} ({{ $supplement->consumption_time }})
                        <form method="POST" action="{{ route('coach.supplements.destroy', $supplement) }}">
                            @csrf @method('DELETE')
                            <button class="text-red-500 hover:underline text-xs">حذف</button>
                        </form>
                    </li>
                @empty
                    <li class="text-gray-400 text-xs">موردی ثبت نشده.</li>
                @endforelse
            </ul>
        </div>
    </div>
@endsection
