<?php
/**
 * Admin gallery tabs management template
 */

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

if (isset($_GET['deleted'])) {
    add_settings_error('bwg_tabs', 'tab_deleted', __('Tab deleted successfully.', 'bw-gallery'), 'updated');
}

if (isset($_GET['error'])) {
    add_settings_error('bwg_tabs', 'tab_error', __('Error deleting tab. Please try again.', 'bw-gallery'), 'error');
}

settings_errors('bwg_tabs');
?>

<div class="wrap">
    <h1><?php esc_html_e('Gallery Tabs', 'bw-gallery'); ?></h1>
    
    <div id="col-container" class="wp-clearfix">
        
        <!-- Add New Tab Form -->
        <div id="col-left">
            <div class="col-wrap">
                <div class="form-wrap">
                    <h2><?php esc_html_e('Add New Gallery Tab', 'bw-gallery'); ?></h2>
                    
                    <form method="post" action="">
                        <?php wp_nonce_field('bwg_save_tabs', 'bwg_tabs_nonce'); ?>
                        
                        <div class="form-field form-required">
                            <label for="tab_name"><?php esc_html_e('Name', 'bw-gallery'); ?></label>
                            <input type="text" name="tab_name" id="tab_name" value="" required>
                            <p><?php esc_html_e('The name is how it appears in your gallery.', 'bw-gallery'); ?></p>
                        </div>
                        
                        <div class="form-field">
                            <label for="tab_slug"><?php esc_html_e('Slug', 'bw-gallery'); ?></label>
                            <input type="text" name="tab_slug" id="tab_slug" value="">
                            <p><?php esc_html_e('The "slug" is the URL-friendly version of the name. Leave blank to auto-generate.', 'bw-gallery'); ?></p>
                        </div>
                        
                        <?php submit_button(__('Add New Tab', 'bw-gallery')); ?>
                    </form>
                </div>
            </div>
        </div>
        
        <!-- Tabs List -->
        <div id="col-right">
            <div class="col-wrap">
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Name', 'bw-gallery'); ?></th>
                            <th><?php esc_html_e('Slug', 'bw-gallery'); ?></th>
                            <th><?php esc_html_e('Count', 'bw-gallery'); ?></th>
                            <th><?php esc_html_e('Actions', 'bw-gallery'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($tabs)) : ?>
                            <tr>
                                <td colspan="4"><?php esc_html_e('No tabs found.', 'bw-gallery'); ?></td>
                            </tr>
                        <?php else : ?>
                            <?php foreach ($tabs as $tab) : 
                                $delete_link = wp_nonce_url(
                                    admin_url('admin.php?page=bw-gallery-tabs&action=delete&tab_id=' . $tab->term_id),
                                    'delete_tab_' . $tab->term_id
                                );
                            ?>
                                <tr>
                                    <td><strong><?php echo esc_html($tab->name); ?></strong></td>
                                    <td><?php echo esc_html($tab->slug); ?></td>
                                    <td><?php echo esc_html($tab->count); ?></td>
                                    <td>
                                        <a href="<?php echo esc_url($delete_link); ?>" 
                                           class="button button-small"
                                           onclick="return confirm('<?php esc_attr_e('Are you sure you want to delete this tab?', 'bw-gallery'); ?>');">
                                            <?php esc_html_e('Delete', 'bw-gallery'); ?>
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
                
                <div class="tablenav bottom">
                    <div class="alignleft actions">
                        <p class="description">
                            <?php esc_html_e('Note: These tabs will be available as dropdown options when creating or editing galleries.', 'bw-gallery'); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        
    </div>
</div>