@extends('admin.layouts.app')

@section('title', $exercise->exists ? 'ویرایش حرکت' : 'حرکت جدید')

@section('content')
    <h1 class="text-xl font-bold mb-6">{{ $exercise->exists ? 'ویرایش حرکت' : 'حرکت ورزشی جدید' }}</h1>

    <form method="POST"
          action="{{ $exercise->exists ? route('admin.exercises.update', $exercise) : route('admin.exercises.store') }}"
          enctype="multipart/form-data"
          class="bg-white rounded-xl shadow-sm p-6 max-w-2xl space-y-4">
        @csrf
        @if($exercise->exists) @method('PUT') @endif

        <div class="grid grid-cols-2 gap-4">
            <div>
                <label class="block text-sm mb-1">نام حرکت *</label>
                <input type="text" name="name" value="{{ old('name', $exercise->name) }}" class="w-full border rounded-lg px-3 py-2" required>
                @error('name') <p class="text-red-600 text-xs mt-1">{{ $message }}</p> @enderror
            </div>
            <div>
                <label class="block text-sm mb-1">نام انگلیسی</label>
                <input type="text" name="name_en" value="{{ old('name_en', $exercise->name_en) }}" class="w-full border rounded-lg px-3 py-2" dir="ltr">
            </div>
        </div>

        <div class="grid grid-cols-2 gap-4">
            <div>
                <label class="block text-sm mb-1">عضله اصلی</label>
                <select name="main_muscle_id" class="w-full border rounded-lg px-3 py-2">
                    <option value="">انتخاب کنید...</option>
                    @foreach($muscles as $muscle)
                        <option value="{{ $muscle->id }}" @selected(old('main_muscle_id', $exercise->main_muscle_id) == $muscle->id)>{{ $muscle->name }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm mb-1">سطح سختی *</label>
                <select name="difficulty" class="w-full border rounded-lg px-3 py-2" required>
                    <option value="beginner" @selected(old('difficulty', $exercise->difficulty?->value) === 'beginner')>مبتدی</option>
                    <option value="intermediate" @selected(old('difficulty', $exercise->difficulty?->value) === 'intermediate')>متوسط</option>
                    <option value="advanced" @selected(old('difficulty', $exercise->difficulty?->value) === 'advanced')>پیشرفته</option>
                </select>
            </div>
        </div>

        <div>
            <label class="block text-sm mb-2">عضلات درگیر (اختیاری)</label>
            <div class="flex flex-wrap gap-2">
                @foreach($muscles as $muscle)
                    <label class="flex items-center gap-1 text-sm border rounded-lg px-3 py-1.5">
                        <input type="checkbox" name="muscle_ids[]" value="{{ $muscle->id }}"
                               @checked($exercise->involvedMuscles?->contains($muscle->id))>
                        {{ $muscle->name }}
                    </label>
                @endforeach
            </div>
        </div>

        <div>
            <label class="block text-sm mb-2">تجهیزات (اختیاری)</label>
            <div class="flex flex-wrap gap-2">
                @foreach($equipmentList as $item)
                    <label class="flex items-center gap-1 text-sm border rounded-lg px-3 py-1.5">
                        <input type="checkbox" name="equipment_ids[]" value="{{ $item->id }}"
                               @checked($exercise->equipment?->contains($item->id))>
                        {{ $item->name }}
                    </label>
                @endforeach
            </div>
        </div>

        <div>
            <label class="block text-sm mb-1">توضیحات</label>
            <textarea name="description" rows="3" class="w-full border rounded-lg px-3 py-2">{{ old('description', $exercise->description) }}</textarea>
        </div>

        <div class="grid grid-cols-2 gap-4">
            <div>
                <label class="block text-sm mb-1">فایل GIF</label>
                @if($exercise->gif_path)
                    <img src="{{ Storage::disk('public')->url($exercise->gif_path) }}" class="w-16 h-16 rounded object-cover mb-2">
                @endif
                <input type="file" name="gif" accept=".gif" class="text-sm">
                @error('gif') <p class="text-red-600 text-xs mt-1">{{ $message }}</p> @enderror
            </div>
            <div>
                <label class="block text-sm mb-1">تصویر ثابت</label>
                @if($exercise->image_path)
                    <img src="{{ Storage::disk('public')->url($exercise->image_path) }}" class="w-16 h-16 rounded object-cover mb-2">
                @endif
                <input type="file" name="image" accept="image/*" class="text-sm">
                @error('image') <p class="text-red-600 text-xs mt-1">{{ $message }}</p> @enderror
            </div>
        </div>

        <label class="flex items-center gap-2 text-sm">
            <input type="checkbox" name="is_active" value="1" @checked(old('is_active', $exercise->is_active ?? true))>
            فعال (در Workout Builder قابل انتخاب باشد)
        </label>

        <div class="flex gap-3 pt-2">
            <button class="bg-gray-900 text-white px-5 py-2 rounded-lg text-sm">ذخیره</button>
            <a href="{{ route('admin.exercises.index') }}" class="px-5 py-2 rounded-lg text-sm border">انصراف</a>
        </div>
    </form>
@endsection
