<?php
if ( ! defined( 'ABSPATH' ) ) exit;
?>
<div class="wrap">
    <h1 class="iccb-admin-notice">ConsentPress</h1>
    <div class="iccb-dashboard-header">
        <div class="iccb-title-holder">
            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="consentpress-icon" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                <g id="surface2">
                    <path class="circle" d="M 62 32 C 62 15.427083 48.572917 2 32 2 C 15.427083 2 2 15.427083 2 32 C 2 48.572917 15.427083 62 32 62 C 48.572917 62 62 48.572917 62 32 Z M 62 32 " transform="matrix(0.375,0,0,0.375,0,0)"/>
                    <path class="dot" d="M 9.75 9 C 9.75 8.171875 9.078125 7.5 8.25 7.5 C 7.421875 7.5 6.75 8.171875 6.75 9 C 6.75 9.828125 7.421875 10.5 8.25 10.5 C 9.078125 10.5 9.75 9.828125 9.75 9 Z M 9.75 9 "/>
                    <path class="dot" d="M 15.375 6.75 C 15.375 6.128906 14.871094 5.625 14.25 5.625 C 13.628906 5.625 13.125 6.128906 13.125 6.75 C 13.125 7.371094 13.628906 7.875 14.25 7.875 C 14.871094 7.875 15.375 7.371094 15.375 6.75 Z M 15.375 6.75 "/>
                    <path class="dot" d="M 17.625 12 C 17.625 10.964844 16.785156 10.125 15.75 10.125 C 14.714844 10.125 13.875 10.964844 13.875 12 C 13.875 13.035156 14.714844 13.875 15.75 13.875 C 16.785156 13.875 17.625 13.035156 17.625 12 Z M 17.625 12 "/>
                    <path class="dot" d="M 12 15.75 C 12 14.921875 11.328125 14.25 10.5 14.25 C 9.671875 14.25 9 14.921875 9 15.75 C 9 16.578125 9.671875 17.25 10.5 17.25 C 11.328125 17.25 12 16.578125 12 15.75 Z M 12 15.75 "/>
                </g>
            </svg>
            <?php esc_html_e('Cookie Definitions', 'consentpress'); ?>
        </div>
        <p><?php esc_html_e('Manage the cookie data shown in your banner and policy table.', 'consentpress'); ?></p>
    </div>
    <div class="iccb-definitions">
        <!-- Tab navigation -->
        <h2 class="nav-tab-wrapper">
            <a href="#iccb-tab-edit" class="nav-tab nav-tab-active"><?php esc_html_e('Edit Definitions', 'consentpress'); ?></a>
            <a href="#iccb-tab-preview" class="nav-tab"><?php esc_html_e('Frontend Preview', 'consentpress'); ?></a>
        </h2>

        <!-- Tab content: Edit -->
        <div id="iccb-tab-edit" class="iccb-tab-content">
            <div class="iccb-scan-panel">
                <div>
                    <h2><?php esc_html_e('Auto-detect browser cookies', 'consentpress'); ?></h2>
                    <p><?php esc_html_e('Scan the cookies currently visible in your browser and add new matches to the definition table automatically.', 'consentpress'); ?></p>
                </div>
                <button type="button" class="button button-primary" id="iccb-scan-cookies">
                    <?php esc_html_e('Scan Browser Cookies', 'consentpress'); ?>
                </button>
            </div>
            <div id="iccb-scan-result" class="notice inline iccb-hidden" role="status" aria-live="polite"></div>

            <form method="post">
                <?php wp_nonce_field('iccb_definitions_save'); ?>
                <table class="widefat" id="cookie-definitions-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Name', 'consentpress'); ?></th>
                            <th><?php esc_html_e('Type', 'consentpress'); ?></th>
                            <th><?php esc_html_e('Domain', 'consentpress'); ?></th>
                            <th><?php esc_html_e('Path', 'consentpress'); ?></th>
                            <th><?php esc_html_e('Duration', 'consentpress'); ?></th>
                            <th><?php esc_html_e('Purpose', 'consentpress'); ?></th>
                            <th><?php esc_html_e('Provider', 'consentpress'); ?></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($definitions as $name => $def): ?>
                        <tr>
                            <td>
                                <input name="cookie_name[]" value="<?php echo esc_attr($name); ?>" />
                                <input type="hidden" name="cookie_source[]" value="<?php echo esc_attr($def['source'] ?? ''); ?>" />
                                <?php if (!empty($def['source'])): ?>
                                    <span class="description">[<?php echo esc_html($def['source']); ?>]</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <select name="cookie_type[]">
                                    <?php
                                    $iccb_type_labels = [
                                        'necessary' => __('Necessary', 'consentpress'),
                                        'functional' => __('Functional', 'consentpress'),
                                        'analytics' => __('Analytics', 'consentpress'),
                                        'performance' => __('Performance', 'consentpress'),
                                        'marketing' => __('Marketing', 'consentpress'),
                                        'uncategorized' => __('Uncategorized', 'consentpress'),
                                    ];
                                    foreach ($iccb_type_labels as $iccb_type_option => $iccb_type_label) {
                                        printf(
                                            '<option value="%s" %s>%s</option>',
                                            esc_attr($iccb_type_option),
                                            selected(strtolower($def['type']), $iccb_type_option, false),
                                            esc_html($iccb_type_label)
                                        );
                                    }
                                    ?>
                                </select>
                            </td>
                            <td><input name="cookie_domain[]" value="<?php echo esc_attr($def['domain']); ?>" /></td>
                            <td><input name="cookie_path[]" value="<?php echo esc_attr($def['path']); ?>" /></td>
                            <td><input name="cookie_duration[]" value="<?php echo esc_attr($def['duration']); ?>" /></td>
                            <td><input name="cookie_purpose[]" value="<?php echo esc_attr($def['purpose']); ?>" /></td>
                            <td><input name="cookie_provider[]" value="<?php echo esc_attr($def['provider']); ?>" /></td>
                            <td><button type="button" class="button remove-row">✕</button></td>
                        </tr>
                    <?php endforeach; ?>
                    <!-- Empty row -->
                    <tr>
                        <td><input name="cookie_name[]" /><input type="hidden" name="cookie_source[]" /></td>
                        <td>
                            <select name="cookie_type[]">
                                <option value="necessary"><?php echo esc_html__('Necessary', 'consentpress'); ?></option>
                                <option value="functional"><?php echo esc_html__('Functional', 'consentpress'); ?></option>
                                <option value="analytics"><?php echo esc_html__('Analytics', 'consentpress'); ?></option>
                                <option value="performance"><?php echo esc_html__('Performance', 'consentpress'); ?></option>
                                <option value="marketing"><?php echo esc_html__('Marketing', 'consentpress'); ?></option>
                                <option value="uncategorized"><?php echo esc_html__('Uncategorized', 'consentpress'); ?></option>
                            </select>
                        </td>
                        <td><input name="cookie_domain[]" /></td>
                        <td><input name="cookie_path[]" /></td>
                        <td><input name="cookie_duration[]" /></td>
                        <td><input name="cookie_purpose[]" /></td>
                        <td><input name="cookie_provider[]" /></td>
                        <td><button type="button" class="button remove-row">✕</button></td>
                    </tr>
                    </tbody>
                </table>

                <p><button type="button" class="button right" id="add-cookie-row"><?php esc_html_e('Add Row', 'consentpress'); ?></button></p>
                <?php submit_button(esc_html__('Save Definitions', 'consentpress'), 'primary', 'iccb_save_definitions'); ?>
            </form>
        </div>

        <!-- Tab content: Preview -->
        <div id="iccb-tab-preview" class="iccb-tab-content iccb-hidden">
            <h2><?php esc_html_e('Frontend Preview', 'consentpress'); ?></h2>
            <p><?php esc_html_e('To display the cookie definition table on your site, use the following shortcode:', 'consentpress'); ?></p>
            <code>[consentpress_cookie_table]</code>
            <p><?php esc_html_e('Place this shortcode on your privacy or cookie policy page to show the table below.', 'consentpress'); ?></p>
            <div id="iccb-definitions-table"></div>
            <div id="iccb-cookie-banner"></div>
        </div>
    </div>
</div>
