<?php

/**
 * This file is part of JohnCMS Content Management System.
 *
 * @copyright JohnCMS Community
 * @license   https://opensource.org/licenses/GPL-3.0 GPL-3.0
 * @link      https://johncms.com JohnCMS Project
 */

/**
 * @var $title
 * @var $page_title
 * @var $data
 */

$this->layout(
    'system::layout/default',
    [
        'title'      => $title,
        'page_title' => $page_title ?? $title,
    ]
);
?>

<!-- Main Container -->
<div class="max-w-4xl mx-auto p-4 space-y-6">

    <!-- Success Message -->
    <?php if (isset($_POST['submit'])) : ?>
        <div class="bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-2xl p-6">
            <div class="flex items-center">
                <svg class="w-6 h-6 text-green-600 dark:text-green-400 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
                <div>
                    <h3 class="text-lg font-semibold text-green-800 dark:text-green-200"><?= __('Changes Saved') ?></h3>
                    <p class="text-green-700 dark:text-green-300 text-sm mt-1"><?= __('Your changes have been successfully saved.') ?></p>
                </div>
            </div>
        </div>
    <?php else : ?>

        <!-- Edit Form -->
        <div class="bg-white dark:bg-gray-800 rounded-2xl shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden">
            <!-- Form Header -->
            <div class="bg-gradient-to-r from-blue-500 to-purple-600 px-6 py-4">
                <h2 class="text-xl font-bold text-white flex items-center">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                    </svg>
                    <?= __('Edit') ?>: <?= $type === 'dir' ? __('Section') : __('Article') ?>
                </h2>
            </div>

            <form action="?act=moder&amp;type=<?= $type ?>&amp;id=<?= $id ?>" method="post" enctype="multipart/form-data" class="p-6 space-y-6">
                
                <!-- Cover Image Upload (Articles Only) -->
                <?php if ($type === 'article') : ?>
                    <div class="bg-gray-50 dark:bg-gray-700/50 rounded-xl p-5">
                        <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
                            <svg class="w-5 h-5 mr-2 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                            </svg>
                            <?= __('Cover Image') ?>
                        </h3>
                        
                        <?php if ($res['cover']): ?>
                            <div class="mb-4">
                                <div class="relative inline-block group">
                                    <img src="../upload/library/images/big/<?= $id ?>.png" 
                                         alt="Cover" 
                                         class="rounded-lg shadow-md w-48 h-32 object-cover">
                                    <div class="absolute inset-0 bg-black bg-opacity-0 group-hover:bg-opacity-20 transition duration-200 rounded-lg"></div>
                                </div>
                                <div class="mt-3">
                                    <a href="?act=del&amp;type=image&amp;id=<?= $id ?>" 
                                       class="inline-flex items-center px-3 py-1 bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-400 rounded-lg hover:bg-red-200 dark:hover:bg-red-900/50 transition duration-200 text-sm font-medium">
                                        <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                        </svg>
                                        <?= __('Delete cover') ?>
                                    </a>
                                </div>
                            </div>
                        <?php endif; ?>

                        <div class="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-xl p-4 text-center hover:border-blue-400 dark:hover:border-blue-500 transition duration-200">
                            <input type="file" 
                                   id="image" 
                                   name="image" 
                                   class="hidden"
                                   accept="image/*">
                            <label for="image" class="cursor-pointer block">
                                <svg class="w-8 h-8 text-gray-400 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                                </svg>
                                <span class="text-sm text-gray-600 dark:text-gray-400 font-medium">
                                    <?= __('Click to upload cover image') ?>
                                </span>
                                <p class="text-xs text-gray-500 dark:text-gray-500 mt-1">
                                    PNG, JPG, GIF up to 5MB
                                </p>
                            </label>
                        </div>
                    </div>
                <?php endif; ?>

                <!-- Title Field -->
                <div>
                    <label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        <?= __('Title') ?> *
                    </label>
                    <input type="text" 
                           name="name" 
                           id="name" 
                           value="<?= $res['name'] ?>" 
                           required
                           class="w-full px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition duration-200"
                           placeholder="<?= __('Enter title...') ?>">
                </div>

                <!-- Description/Announce Field -->
                <div>
                    <label for="<?= $type === 'dir' ? 'description' : 'announce' ?>" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        <?= $type === 'dir' ? __('Section description') : __('Announce') ?>
                    </label>
                    <textarea name="<?= $type === 'dir' ? 'description' : 'announce' ?>" 
                              id="<?= $type === 'dir' ? 'description' : 'announce' ?>" 
                              rows="<?= $user->config->fieldHeight ?>"
                              class="w-full px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition duration-200 resize-vertical"
                              placeholder="<?= $type === 'dir' ? __('Describe this section...') : __('Enter announcement...') ?>"><?= $type === 'dir' ? $res['description'] : $res['announce'] ?></textarea>
                </div>

                <!-- Article Content -->
                <?php if ($type === 'article') : ?>
                    <?php if (mb_strlen($res['text']) < 500000) : ?>
                        <div>
                            <label for="text" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                <?= __('Content') ?>
                            </label>
                            <div class="mb-3"><?= $bbcode ?></div>
                            <textarea name="text" 
                                      id="text" 
                                      rows="<?= $user->config->fieldHeight ?>"
                                      class="w-full px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition duration-200 resize-vertical"
                                      placeholder="<?= __('Enter article content...') ?>"><?= $res['text'] ?></textarea>
                        </div>
                    <?php else: ?>
                        <div class="bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-xl p-4">
                            <div class="flex items-center">
                                <svg class="w-5 h-5 text-yellow-600 dark:text-yellow-400 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 15.5c-.77.833.192 2.5 1.732 2.5z"/>
                                </svg>
                                <span class="text-yellow-800 dark:text-yellow-200 font-medium"><?= __('The text of the Article can not be edited, a large amount of data !!!') ?></span>
                            </div>
                            <input type="hidden" name="text" value="do_not_change">
                        </div>
                    <?php endif; ?>

                    <!-- Tags -->
                    <div>
                        <label for="tags" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                            <?= __('Tags') ?>
                        </label>
                        <input type="text" 
                               name="tags" 
                               id="tags" 
                               value="<?= $res['tags'] ?>"
                               class="w-full px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition duration-200"
                               placeholder="<?= __('Enter tags separated by commas...') ?>">
                    </div>
                <?php endif; ?>

                <!-- Admin Settings -->
                <?php if ($adm) : ?>
                    <div class="bg-gray-50 dark:bg-gray-700/50 rounded-xl p-5 space-y-6">
                        <h3 class="text-lg font-semibold text-gray-900 dark:text-white flex items-center">
                            <svg class="w-5 h-5 mr-2 text-red-600 dark:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/>
                            </svg>
                            <?= __('Administrator Settings') ?>
                        </h3>

                        <!-- Section Type -->
                        <?php if ($empty) : ?>
                            <div>
                                <h4 class="font-medium text-gray-700 dark:text-gray-300 mb-3"><?= __('Section type') ?></h4>
                                <div class="space-y-2">
                                    <label class="flex items-center">
                                        <input type="radio" name="dir" value="0" class="w-4 h-4 text-blue-600 bg-gray-100 dark:bg-gray-600 border-gray-300 dark:border-gray-500 focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2" <?= ($res['dir'] === 0 ? 'checked' : '') ?>>
                                        <span class="ml-3 text-gray-700 dark:text-gray-300"><?= __('Articles') ?></span>
                                    </label>
                                    <label class="flex items-center">
                                        <input type="radio" name="dir" value="1" class="w-4 h-4 text-blue-600 bg-gray-100 dark:bg-gray-600 border-gray-300 dark:border-gray-500 focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2" <?= ($res['dir'] === 1 ? 'checked' : '') ?>>
                                        <span class="ml-3 text-gray-700 dark:text-gray-300"><?= __('Sections') ?></span>
                                    </label>
                                </div>
                            </div>
                        <?php endif; ?>

                        <!-- User Permissions -->
                        <?php if ($type === 'dir' && $res['dir'] === 0) : ?>
                            <div>
                                <h4 class="font-medium text-gray-700 dark:text-gray-300 mb-3"><?= __('Allow users to add their Articles?') ?></h4>
                                <div class="space-y-2">
                                    <label class="flex items-center">
                                        <input type="radio" name="user_add" value="1" class="w-4 h-4 text-blue-600 bg-gray-100 dark:bg-gray-600 border-gray-300 dark:border-gray-500 focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2" <?= ($res['user_add'] === 1 ? 'checked' : '') ?>>
                                        <span class="ml-3 text-gray-700 dark:text-gray-300"><?= __('Yes') ?></span>
                                    </label>
                                    <label class="flex items-center">
                                        <input type="radio" name="user_add" value="0" class="w-4 h-4 text-blue-600 bg-gray-100 dark:bg-gray-600 border-gray-300 dark:border-gray-500 focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2" <?= ($res['user_add'] === 0 ? 'checked' : '') ?>>
                                        <span class="ml-3 text-gray-700 dark:text-gray-300"><?= __('No') ?></span>
                                    </label>
                                </div>
                            </div>
                        <?php endif; ?>

                        <!-- Move Section -->
                        <?php if ($select) : ?>
                            <div>
                                <label for="move" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <?= __('Move to Section') ?>
                                </label>
                                <select name="move" 
                                        id="move"
                                        class="w-full px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition duration-200">
                                    <?php foreach ($select as $option) : ?>
                                        <?= $option ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        <?php endif; ?>

                        <!-- Article Settings -->
                        <?php if ($type === 'article') : ?>
                            <div class="space-y-4">
                                <label class="flex items-center">
                                    <input type="checkbox" name="premod" value="1" class="w-4 h-4 text-blue-600 bg-gray-100 dark:bg-gray-600 border-gray-300 dark:border-gray-500 rounded focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2" <?= ($res['premod'] > 0 ? 'checked' : '') ?>>
                                    <span class="ml-3 text-gray-700 dark:text-gray-300 font-medium"><?= __('Verified') ?></span>
                                </label>
                                <label class="flex items-center">
                                    <input type="checkbox" name="comments" value="1" class="w-4 h-4 text-blue-600 bg-gray-100 dark:bg-gray-600 border-gray-300 dark:border-gray-500 rounded focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2" <?= ($res['comments'] > 0 ? 'checked' : '') ?>>
                                    <span class="ml-3 text-gray-700 dark:text-gray-300 font-medium"><?= __('Commenting on the Article') ?></span>
                                </label>
                                <div>
                                    <label for="count_views" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                        <?= __('Number of readings') ?>
                                    </label>
                                    <input type="number" 
                                           name="count_views" 
                                           id="count_views" 
                                           value="<?= $res['count_views'] ?>"
                                           class="w-full px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-xl bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition duration-200">
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>

                <!-- Form Actions -->
                <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 pt-6 border-t border-gray-200 dark:border-gray-600">
                    <div class="flex space-x-3">
                        <button type="submit" 
                                name="submit" 
                                value="1"
                                class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-xl shadow-sm hover:shadow-md transition duration-200 font-medium">
                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                            </svg>
                            <?= __('Save Changes') ?>
                        </button>
                        <a href="?do=<?= ($type === 'dir' ? 'dir' : 'text') ?>&amp;id=<?= $id ?>" 
                           class="inline-flex items-center px-6 py-3 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-xl transition duration-200 font-medium">
                            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                            </svg>
                            <?= __('Cancel') ?>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    <?php endif; ?>

    <!-- Back Link -->
    <div class="text-center">
        <a href="?do=<?= ($type === 'dir' ? 'dir' : 'text') ?>&amp;id=<?= $id ?>" 
           class="inline-flex items-center text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300 transition duration-200 font-medium">
            <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
            </svg>
            <?= __('Back') ?>
        </a>
    </div>

</div>