<?php
/**
 * Admin gallery edit template
 */

if (!defined('ABSPATH')) {
    exit;
}

$is_edit = !empty($gallery);
$title = $is_edit ? __('Edit Gallery', 'bw-gallery') : __('Add New Gallery', 'bw-gallery');

// Get available tabs from taxonomy
$taxonomy = new BW_Gallery_Taxonomy();
$available_tabs = $taxonomy->get_all_tabs();
?>

<div class="wrap">
    <h1><?php echo esc_html($title); ?></h1>
    
    <form id="bwg-gallery-form" method="post">
        <input type="hidden" id="gallery_id" name="gallery_id" value="<?php echo esc_attr($gallery_id); ?>">
        
        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="gallery_name"><?php esc_html_e('Gallery Name', 'bw-gallery'); ?></label>
                </th>
                <td>
                    <input type="text" id="gallery_name" name="name" class="regular-text" 
                           value="<?php echo $is_edit ? esc_attr($gallery['name']) : ''; ?>" required>
                </td>
            </tr>
            
            <tr>
                <th scope="row">
                    <label for="gallery_description"><?php esc_html_e('Description', 'bw-gallery'); ?></label>
                </th>
                <td>
                    <textarea id="gallery_description" name="description" rows="3" class="large-text"><?php 
                        echo $is_edit ? esc_textarea($gallery['description']) : ''; 
                    ?></textarea>
                </td>
            </tr>
            
            <tr>
                <th scope="row"><?php esc_html_e('Gallery Settings', 'bw-gallery'); ?></th>
                <td>
                    <fieldset>
                        <label>
                            <input type="checkbox" name="enable_tabs" value="1" 
                                   <?php checked($is_edit && !empty($gallery['settings']['enable_tabs'])); ?>>
                            <?php esc_html_e('Enable tabbed gallery', 'bw-gallery'); ?>
                        </label>
                        <br>
                        
                        <label>
                            <input type="checkbox" name="lightbox" value="1" 
                                   <?php checked(!$is_edit || !empty($gallery['settings']['lightbox'])); ?>>
                            <?php esc_html_e('Enable lightbox', 'bw-gallery'); ?>
                        </label>
                        <br><br>
                        
                        <label for="gallery_columns">
                            <?php esc_html_e('Columns:', 'bw-gallery'); ?>
                            <input type="number" id="gallery_columns" name="columns" min="1" max="6" 
                                   value="<?php echo $is_edit && isset($gallery['settings']['columns']) ? esc_attr($gallery['settings']['columns']) : '3'; ?>" 
                                   class="small-text">
                        </label>
                        <br><br>
                        
                        <label for="gallery_gap">
                            <?php esc_html_e('Gap (px):', 'bw-gallery'); ?>
                            <input type="number" id="gallery_gap" name="gap" min="0" max="50" 
                                   value="<?php echo $is_edit && isset($gallery['settings']['gap']) ? esc_attr($gallery['settings']['gap']) : '10'; ?>" 
                                   class="small-text">
                        </label>
                    </fieldset>
                </td>
            </tr>
        </table>
        
        <h2><?php esc_html_e('Gallery Images', 'bw-gallery'); ?></h2>
        
        <div id="bwg-images-container">
            <div id="bwg-images-list" class="bwg-images-grid"></div>
            <button type="button" id="bwg-add-images" class="button button-primary">
                <?php esc_html_e('Add Images', 'bw-gallery'); ?>
            </button>
        </div>
        
        <div id="bwg-tab-notice" style="display:none;" class="notice notice-info">
            <p><?php esc_html_e('With tabbed galleries enabled, you can assign images to different tabs by selecting from the dropdown menu.', 'bw-gallery'); ?></p>
            <p><a href="<?php echo esc_url(admin_url('admin.php?page=bw-gallery-tabs')); ?>"><?php esc_html_e('Manage Gallery Tabs', 'bw-gallery'); ?></a></p>
        </div>
        
        <p class="submit">
            <button type="submit" class="button button-primary">
                <?php esc_html_e('Save Gallery', 'bw-gallery'); ?>
            </button>
            <a href="<?php echo esc_url(admin_url('admin.php?page=bw-gallery')); ?>" class="button">
                <?php esc_html_e('Cancel', 'bw-gallery'); ?>
            </a>
        </p>
    </form>
</div>

<script type="text/template" id="bwg-image-template">
    <div class="bwg-image-item" data-id="{{ id }}">
        <div class="bwg-image-preview">
            <img src="{{ url }}" alt="">
            <div class="bwg-image-actions">
                <button type="button" class="bwg-remove-image" title="<?php esc_attr_e('Remove', 'bw-gallery'); ?>">
                    <span class="dashicons dashicons-no"></span>
                </button>
            </div>
        </div>
        <div class="bwg-image-meta">
            <select class="bwg-image-tab">
                <option value=""><?php esc_html_e('Select tab...', 'bw-gallery'); ?></option>
                <?php foreach ($available_tabs as $tab) : ?>
                    <option value="<?php echo esc_attr($tab->slug); ?>"><?php echo esc_html($tab->name); ?></option>
                <?php endforeach; ?>
            </select>
            <input type="text" class="bwg-image-caption" placeholder="<?php esc_attr_e('Caption', 'bw-gallery'); ?>" value="{{ caption }}">
        </div>
    </div>
</script>