<?php

namespace MovieEngine\Admin\Design\Tabs;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class ArchiveTab {

    public static function render($options = [], $standalone = true) {
        if (empty($options)) {
            $options = get_option('movie_engine_design_settings', []);
        }
?>
        <?php if ($standalone) : ?>
            <input type="hidden" name="movie_engine_design_settings[active_sub_tab]" value="archive">
            <div class="movie-engine-settings-section">
                <div class="movie-engine-section-header">
                    <h2><?php esc_html_e('Archive & Listings', 'cine-movie-engine'); ?></h2>
                    <p class="description"><?php esc_html_e('Configure how movies and TV shows are displayed in lists.', 'cine-movie-engine'); ?></p>
                </div>
            <?php endif; ?>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="dashicons dashicons-grid-view"></span> <?php esc_html_e('Grid Layout', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <div class="movie-engine-form-group">
                        <label><?php esc_html_e('Columns on Desktop', 'cine-movie-engine'); ?></label>
                        <div class="movie-engine-column-selector">
                            <?php
                            $curr_desk = $options['archive_columns_desktop'] ?? '5';
                            $cols_desk = [3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six'];
                            foreach ($cols_desk as $num => $label) :
                            ?>
                                <label class="movie-engine-col-option">
                                    <input type="radio" name="movie_engine_design_settings[archive_columns_desktop]" value="<?php echo esc_attr($num); ?>" <?php checked($curr_desk, $num); ?>>
                                    <div class="movie-engine-col-box">
                                        <div class="col-bars">
                                            <?php for ($i = 0; $i < $num; $i++): ?><span></span><?php endfor; ?>
                                        </div>
                                        <span class="col-label"><?php echo esc_html($label); ?></span>
                                    </div>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    <div class="movie-engine-form-group">
                        <label><?php esc_html_e('Columns on Laptop', 'cine-movie-engine'); ?></label>
                        <div class="movie-engine-column-selector">
                            <?php
                            $curr_laptop = $options['archive_columns_laptop'] ?? '4';
                            $cols_laptop = [2 => 'Two', 3 => 'Three', 4 => 'Four', 5 => 'Five'];
                            foreach ($cols_laptop as $num => $label) :
                            ?>
                                <label class="movie-engine-col-option">
                                    <input type="radio" name="movie_engine_design_settings[archive_columns_laptop]" value="<?php echo esc_attr($num); ?>" <?php checked($curr_laptop, $num); ?>>
                                    <div class="movie-engine-col-box">
                                        <div class="col-bars">
                                            <?php for ($i = 0; $i < $num; $i++): ?><span></span><?php endfor; ?>
                                        </div>
                                        <span class="col-label"><?php echo esc_html($label); ?></span>
                                    </div>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </div>

                    <div class="movie-engine-form-group">
                        <label><?php esc_html_e('Columns on Tablet', 'cine-movie-engine'); ?></label>
                        <div class="movie-engine-column-selector">
                            <?php
                            $curr_tab = $options['archive_columns_tablet'] ?? '3';
                            $cols_tab = [2 => 'Two', 3 => 'Three', 4 => 'Four'];
                            foreach ($cols_tab as $num => $label) :
                            ?>
                                <label class="movie-engine-col-option">
                                    <input type="radio" name="movie_engine_design_settings[archive_columns_tablet]" value="<?php echo esc_attr($num); ?>" <?php checked($curr_tab, $num); ?>>
                                    <div class="movie-engine-col-box">
                                        <div class="col-bars">
                                            <?php for ($i = 0; $i < $num; $i++): ?><span></span><?php endfor; ?>
                                        </div>
                                        <span class="col-label"><?php echo esc_html($label); ?></span>
                                    </div>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </div>

                    <div class="movie-engine-form-group">
                        <label><?php esc_html_e('Columns on Mobile', 'cine-movie-engine'); ?></label>
                        <div class="movie-engine-column-selector">
                            <?php
                            $curr_mob = $options['archive_columns_mobile'] ?? '2';
                            $cols_mob = [1 => 'One', 2 => 'Two'];
                            foreach ($cols_mob as $num => $label) :
                            ?>
                                <label class="movie-engine-col-option">
                                    <input type="radio" name="movie_engine_design_settings[archive_columns_mobile]" value="<?php echo esc_attr($num); ?>" <?php checked($curr_mob, $num); ?>>
                                    <div class="movie-engine-col-box">
                                        <div class="col-bars">
                                            <?php for ($i = 0; $i < $num; $i++): ?><span></span><?php endfor; ?>
                                        </div>
                                        <span class="col-label"><?php echo esc_html($label); ?></span>
                                    </div>
                                </label>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="dashicons dashicons-list-view"></span> <?php esc_html_e('Display Options', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_design_settings[archive_show_year]" value="0">
                            <input type="checkbox"
                                name="movie_engine_design_settings[archive_show_year]"
                                value="1"
                                <?php checked(isset($options['archive_show_year']) ? $options['archive_show_year'] : 1); ?>>
                            <span><?php esc_html_e('Show Year in Card', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>

                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_design_settings[archive_show_rating]" value="0">
                            <input type="checkbox"
                                name="movie_engine_design_settings[archive_show_rating]"
                                value="1"
                                <?php checked(isset($options['archive_show_rating']) ? $options['archive_show_rating'] : 1); ?>>
                            <span><?php esc_html_e('Show Rating in Card', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>

                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_design_settings[archive_show_quality]" value="0">
                            <input type="checkbox"
                                name="movie_engine_design_settings[archive_show_quality]"
                                value="1"
                                <?php checked(isset($options['archive_show_quality']) ? $options['archive_show_quality'] : 1); ?>>
                            <span><?php esc_html_e('Show Quality Badge (HD/4K)', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>
                </div>
            </div>

            <?php if ($standalone) : ?>
                <div class="movie-engine-form-actions">
                    <?php submit_button(esc_html__('Save Changes', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false); ?>
                    <button type="button" class="button button-link me-reset-section-btn" data-tab="design_archive"><?php esc_html_e('Reset Section to Defaults', 'cine-movie-engine'); ?></button>
                </div>
            </div>
        <?php endif; ?>
<?php
    }
}
