<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Create Process Template') }}
        </h2>
    </x-slot>

    <div class="py-12" x-data="{ tasks: [{title: '', description: '', due_days_offset: 0, required: true}] }">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">
                    <form action="{{ route('process-templates.store') }}" method="POST">
                        @csrf
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div class="mb-4">
                                <x-input-label for="name" :value="__('Template Name')" />
                                <x-text-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus />
                                <x-input-error :messages="$errors->get('name')" class="mt-2" />
                            </div>
                            <div class="mb-4">
                                <x-input-label for="type" :value="__('Type')" />
                                <select id="type" name="type" class="block mt-1 w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm" required>
                                    <option value="onboarding">Onboarding</option>
                                    <option value="offboarding">Offboarding</option>
                                </select>
                                <x-input-error :messages="$errors->get('type')" class="mt-2" />
                            </div>
                        </div>

                        <div class="mt-6">
                            <h3 class="text-lg font-medium text-gray-900 mb-4">Tasks</h3>
                            <template x-for="(task, index) in tasks" :key="index">
                                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-4">
                                    <div>
                                        <x-input-label :value="__('Title')" />
                                        <input type="text" class="block mt-1 w-full border-gray-300 rounded-md shadow-sm" x-model="task.title" :name="'tasks['+index+'][title]'" required>
                                    </div>
                                    <div class="md:col-span-2">
                                        <x-input-label :value="__('Description')" />
                                        <textarea class="block mt-1 w-full border-gray-300 rounded-md shadow-sm" x-model="task.description" :name="'tasks['+index+'][description]'"></textarea>
                                    </div>
                                    <div>
                                        <x-input-label :value="__('Due Days Offset')" />
                                        <input type="number" class="block mt-1 w-full border-gray-300 rounded-md shadow-sm" x-model="task.due_days_offset" :name="'tasks['+index+'][due_days_offset]'" value="0">
                                        <label class="inline-flex items-center mt-2">
                                            <input type="checkbox" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500" x-model="task.required" :name="'tasks['+index+'][required]'" checked>
                                            <span class="ml-2 text-sm text-gray-600">Required</span>
                                        </label>
                                    </div>
                                </div>
                            </template>
                            <button type="button" class="px-3 py-2 bg-gray-200 rounded" @click="tasks.push({title: '', description: '', due_days_offset: 0, required: true})">Add Task</button>
                        </div>

                        <div class="flex items-center justify-end mt-6">
                            <a href="{{ route('process-templates.index') }}" class="mr-4 text-sm text-gray-600 hover:text-gray-900">Cancel</a>
                            <x-primary-button>
                                {{ __('Create Template') }}
                            </x-primary-button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
