<?php
ob_start();
?>

<div class="min-h-screen bg-gray-50 py-6" x-data="authorForm">
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Header -->
        <div class="mb-8">
            <div class="md:flex md:items-center md:justify-between">
                <div class="flex-1 min-w-0">
                    <h2 class="text-2xl font-bold leading-7 text-gray-900 sm:text-3xl sm:truncate">
                        <?= $action === 'create' ? 'Add New Author' : 'Edit Author' ?>
                    </h2>
                    <p class="mt-1 text-sm text-gray-500">
                        <?= $action === 'create' ? 'Create a new author profile' : 'Update author information' ?>
                    </p>
                </div>
            </div>
        </div>

        <!-- Success/Error Messages -->
        <?php if (isset($success)): ?>
            <div class="mb-6 bg-green-50 border border-green-200 rounded-md p-4">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
                        </svg>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm font-medium text-green-800"><?= htmlspecialchars($success) ?></p>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php if (isset($error)): ?>
            <div class="mb-6 bg-red-50 border border-red-200 rounded-md p-4">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
                        </svg>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm font-medium text-red-800"><?= htmlspecialchars($error) ?></p>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <!-- Form -->
        <form method="POST" enctype="multipart/form-data" class="space-y-6">
            <input type="hidden" name="action" value="<?= $action ?>">
            <?php if ($action === 'edit'): ?>
                <input type="hidden" name="id" value="<?= $author['id'] ?>">
            <?php endif; ?>

            <div class="bg-white shadow rounded-lg">
                <div class="px-6 py-4 border-b border-gray-200">
                    <h3 class="text-lg font-medium text-gray-900">Author Information</h3>
                    <p class="mt-1 text-sm text-gray-500">Basic information about the author.</p>
                </div>

                <div class="px-6 py-4 space-y-6">
                    <!-- Author Name -->
                    <div>
                        <label for="name" class="block text-sm font-medium text-gray-700 mb-2">Author Name *</label>
                        <input type="text" name="name" id="name" required
                               value="<?= htmlspecialchars($author['name'] ?? '') ?>" 
                               class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                               placeholder="Enter author name">
                    </div>

                    <!-- Image Upload -->
                    <div>
                        <label for="image" class="block text-sm font-medium text-gray-700 mb-2">Author Photo</label>
                        <div class="flex items-center space-x-4">
                            <?php if (!empty($author['image'])): ?>
                                <div class="flex-shrink-0">
                                    <img src="/<?= htmlspecialchars($author['image']) ?>" alt="Current image" class="h-20 w-20 rounded-full object-cover border border-gray-300">
                                </div>
                            <?php endif; ?>
                            <div class="flex-1">
                                <input type="file" name="image_upload" id="image_upload" accept="image/*"
                                       class="block w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0 file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100">
                                <input type="hidden" name="current_image" value="<?= htmlspecialchars($author['image'] ?? '') ?>">
                                <p class="mt-1 text-xs text-gray-500">Upload a photo for the author profile</p>
                            </div>
                        </div>
                    </div>

                    <!-- Description -->
                    <div>
                        <label for="description" class="block text-sm font-medium text-gray-700 mb-2">Description</label>
                        <textarea name="description" id="description" rows="4"
                                  class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                  placeholder="Author bio and description"><?= htmlspecialchars($author['description'] ?? '') ?></textarea>
                    </div>
                </div>
            </div>

            <!-- Social Media Links -->
            <div class="bg-white shadow rounded-lg">
                <div class="px-6 py-4 border-b border-gray-200">
                    <h3 class="text-lg font-medium text-gray-900">Social Media Links</h3>
                    <p class="mt-1 text-sm text-gray-500">Add social media profiles for this author.</p>
                </div>

                <div class="px-6 py-4">
                    <div class="space-y-4">
                        <template x-for="(link, index) in socialLinks" :key="index">
                            <div class="flex items-center space-x-4 p-4 border border-gray-200 rounded-lg">
                                <div class="flex-1">
                                    <label class="block text-sm font-medium text-gray-700 mb-1">Platform</label>
                                    <input type="text" :name="'social_links[' + index + '][platform]'" x-model="link.platform"
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                           placeholder="e.g. Twitter, Facebook, LinkedIn">
                                </div>
                                <div class="flex-1">
                                    <label class="block text-sm font-medium text-gray-700 mb-1">URL</label>
                                    <input type="url" :name="'social_links[' + index + '][url]'" x-model="link.url"
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                           placeholder="https://...">
                                </div>
                                <button type="button" @click="removeSocialLink(index)"
                                        class="mt-6 px-3 py-2 text-sm font-medium text-red-600 hover:text-red-800 border border-red-300 rounded-md hover:bg-red-50">
                                    Remove
                                </button>
                            </div>
                        </template>

                        <button type="button" @click="addSocialLink()"
                                class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                            <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                                <path fill-rule="evenodd" d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" clip-rule="evenodd"/>
                            </svg>
                            Add Social Link
                        </button>
                    </div>
                </div>
            </div>

            <!-- Form Actions -->
            <div class="flex justify-between pt-6 border-t border-gray-200">
                <a href="/admin/authors/" class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors">
                    <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                        <path fill-rule="evenodd" d="M7.707 14.707a1 1 0 01-1.414 0L2.586 11H9a1 1 0 010 2H2.586l3.707 3.707a1 1 0 01-1.414 1.414l-5.414-5.414a1 1 0 010-1.414L4.879 6.879a1 1 0 011.414 1.414L2.586 11H9a1 1 0 010 2H2.586l3.707 3.707z" clip-rule="evenodd"/>
                    </svg>
                    Back to List
                </a>

                <button type="submit" class="inline-flex items-center px-6 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors">
                    <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                        <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                    </svg>
                    <?= $action === 'create' ? 'Create Author' : 'Update Author' ?>
                </button>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('alpine:init', () => {
    Alpine.data('authorForm', () => ({
        socialLinks: <?= json_encode($author['social_links'] ?? [['platform' => '', 'url' => '']]) ?>,
        
        addSocialLink() {
            this.socialLinks.push({ platform: '', url: '' });
        },
        
        removeSocialLink(index) {
            this.socialLinks.splice(index, 1);
        }
    }));
});
</script>

<?php
$content = ob_get_clean();
include __DIR__ . '/../partials/layout.php';
?>