<?php
namespace WTC_CHECKOUT\Modules\Contracts\Admin;
defined('ABSPATH') || exit;
class ContractsSettings {
    private $page_slug = 'wtc-contracts';
    private $option_group = 'wtc_contracts';
    private $tabs = [];
    private $active_tab = '';
    public function __construct() {
        add_action('admin_init', array($this, 'init_settings'));
        $this->tabs = [
            'settings' => __('Ayarlar', 'wtc-checkout'),
            'company_info' => __('Firma Bilgileri', 'wtc-checkout'),
            'mss_template' => __('Mesafeli Satış Sözleşmesi', 'wtc-checkout'),
            'obf_template' => __('Ön Bilgilendirme Formu', 'wtc-checkout'),
            'custom_template' => __('Özel Sözleşme', 'wtc-checkout'),
        ];
    }
    public function init_settings() {
        register_setting(
            'wtc_contract_settings_group',
            'wtc_contracts_enabled',
            array(
                'type' => 'string',
                'default' => 'no',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            'wtc_company_info_group',
            'wtc_company_info',
            array(
                'type' => 'array',
                'sanitize_callback' => array($this, 'sanitize_company_info')
            )
        );
        add_settings_section(
            'wtc_company_info_section',
            __('Firma Bilgileri', 'wtc-checkout'),
            function() {
                echo '<p>' . esc_html__('Sözleşmelerde kullanılacak firma bilgilerini giriniz.', 'wtc-checkout') . '</p>';
            },
            'wtc_company_info'
        );
        $company_fields = array(
            'f_company_name' => __('Firma Adı', 'wtc-checkout'),
            'f_company_address' => __('Firma Adresi', 'wtc-checkout'),
            'f_tax_office' => __('Vergi Dairesi', 'wtc-checkout'),
            'f_tax_number' => __('Vergi No', 'wtc-checkout'),
            'f_phone' => __('Telefon', 'wtc-checkout'),
            'f_email' => __('E-posta', 'wtc-checkout'),
            'f_website' => __('Website', 'wtc-checkout'),
            'f_return_address' => __('İade Adresi', 'wtc-checkout'),
            'f_mersis_no' => __('MERSİS No', 'wtc-checkout'),
            'f_trade_chamber' => __('Ticaret Odası', 'wtc-checkout'),
            'f_trade_registry_no' => __('Ticaret Sicil No', 'wtc-checkout'),
        );
        foreach ($company_fields as $field_id => $field_label) {
            add_settings_field(
                'wtc_company_' . $field_id,
                $field_label,
                array($this, 'render_company_field'),
                'wtc_company_info',
                'wtc_company_info_section',
                array(
                    'field_id' => $field_id,
                    'label_for' => 'wtc_company_' . $field_id
                )
            );
        }
        register_setting(
            'wtc_contract_templates_group',
            'wtc_contract_templates',
            array(
                'type' => 'array',
                'sanitize_callback' => array($this, 'sanitize_templates')
            )
        );
        register_setting(
            'wtc_contract_settings_group',
            'wtc_contract_settings',
            array(
                'type' => 'array',
                'sanitize_callback' => array($this, 'sanitize_contract_settings')
            )
        );
    }
    private function render_tabs() {
        echo '<h2 class="wtc-nav-tab-wrapper nav-tab-wrapper">';
        $nonce = wp_create_nonce('wtc_contracts_tab_action');
        foreach ($this->tabs as $tab_id => $tab_name) {
            $active_class = $this->active_tab === $tab_id ? ' nav-tab-active wtc-nav-tab-active' : '';
            $url = admin_url('admin.php?page=' . $this->page_slug . '&tab=' . $tab_id . '&wtc_tab_nonce=' . $nonce);
            echo '<a href="' . esc_url($url) . '" class="wtc-nav-tab nav-tab' . esc_attr($active_class) . '">' . esc_html($tab_name) . '</a>';
        }
echo '<style>
        .wtc-nav-tab-wrapper {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        gap: 10px;
        position: relative;
    }
    .wtc-right-tabs {
        margin-left: auto;
        display: flex;
        gap: 10px;
    }
    .wtc-right-tabs a {
        display: inline-flex;
        align-items: center;
        font-weight: bold;
        border-radius: 4px;
        text-decoration: none;
        padding: 10px 20px;
        transition: background-color 0.3s ease;
        font-size: 16px;
    }
    .wtc-premium-link {
        background-color: #0AA000;
        color: #fff !important;
    }
    .wtc-premium-link:hover {
        background-color:rgb(8, 97, 2);
    }
    .wtc-support-link {
        background-color: #FFDD00;
        color: #000 !important;
    }
    .wtc-support-link:hover {
        background-color: #f2cc00;
    }
    </style>';
echo '<span class="wtc-right-tabs">';
echo '<a href="https://coff.ee/woosoft" target="_blank" class="wtc-support-link">☕&nbsp;&nbsp;Buy me a coffee</a>';
echo '</span>';
        echo '</h2>';
    }
    public function render_settings_page() {
        $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : '';
        $nonce = isset($_GET['wtc_tab_nonce']) ? sanitize_text_field(wp_unslash($_GET['wtc_tab_nonce'])) : '';
        if ($tab && $nonce && wp_verify_nonce($nonce, 'wtc_contracts_tab_action') && array_key_exists($tab, $this->tabs)) {
            $this->active_tab = $tab;
        } else {
            $this->active_tab = 'settings';
        }
        ?>
        <div class="wrap wtc-admin-page">
            <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
            <?php $this->render_tabs(); ?>
            <div class="wtc-card">
                <?php if ($this->active_tab == 'company_info'): ?>
                    <form method="post" action="options.php">
                        <?php
                        settings_fields('wtc_company_info_group');
                        do_settings_sections('wtc_company_info');
                        submit_button(esc_html__('Değişiklikleri kaydet', 'wtc-checkout'), 'primary wtc-button wtc-button-primary');
                        ?>
                    </form>
                <?php elseif ($this->active_tab == 'settings'): ?>
                    <form method="post" action="options.php">
                        <?php
                        settings_fields('wtc_contract_settings_group');
                        $settings = get_option('wtc_contract_settings', array());
                        ?>
                        <div class="wtc-settings-section">
                            <table class="form-table wtc-form-table">
                                <tr>
                                    <th scope="row"><?php esc_html_e('Sözleşme Sistemi', 'wtc-checkout'); ?></th>
                                    <td>
                                        <div class="wtc-toggle-wrapper">
                                            <input type="checkbox" id="wtc_contracts_enabled" name="wtc_contracts_enabled" 
                                                value="yes" <?php checked(get_option('wtc_contracts_enabled', 'no') === 'yes'); ?>
                                                class="wtc-toggle-input">
                                            <label for="wtc_contracts_enabled" class="wtc-toggle-label"></label>
                                            <span class="toggle-label"><?php echo esc_html__('Sözleşme sistemini aktif et', 'wtc-checkout'); ?></span>
                                        </div>
                                        <p class="description">
                                            <?php esc_html_e('Bu seçenek aktif edildiğinde, sözleşme sistemi çalışır durumda olacaktır.', 'wtc-checkout'); ?>
                                        </p>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="wtc-settings-section">
                        <h3><?php esc_html_e('Ödeme Sayfası', 'wtc-checkout'); ?></h3>
                            <table class="form-table wtc-form-table">
                                <tr>
                                    <th scope="row"><?php esc_html_e('Mesafeli Satış Sözleşmesi', 'wtc-checkout'); ?></th>
                                    <td>
                                        <div class="wtc-toggle-wrapper">
                                            <input type="checkbox" id="wtc_mss_active" name="wtc_contract_settings[mss_active]" 
                                                value="1" <?php checked(isset($settings['mss_active']) && $settings['mss_active']); ?>
                                                class="wtc-toggle-input">
                                            <label for="wtc_mss_active" class="wtc-toggle-label"></label>
                                        </div>
                                        <p class="description">
                                            <?php esc_html_e('MSS için gösterim türünü seç.', 'wtc-checkout'); ?>
                                        </p>
                                        <select name="wtc_contract_settings[mss_display_mode]" class="wtc-select">
                                            <option value="popup" <?php selected(isset($settings['mss_display_mode']) && $settings['mss_display_mode'] == 'popup'); ?>>
                                                <?php esc_html_e('Popup', 'wtc-checkout'); ?>
                                            </option>
                                            <option value="text" <?php selected(isset($settings['mss_display_mode']) && $settings['mss_display_mode'] == 'text'); ?>>
                                                <?php esc_html_e('Metin', 'wtc-checkout'); ?>
                                            </option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row"><?php esc_html_e('Ön Bilgilendirme Formu', 'wtc-checkout'); ?></th>
                                    <td>
                                        <div class="wtc-toggle-wrapper">
                                            <input type="checkbox" id="wtc_obf_active" name="wtc_contract_settings[obf_active]" 
                                                value="1" <?php checked(isset($settings['obf_active']) && $settings['obf_active']); ?>
                                                class="wtc-toggle-input">
                                            <label for="wtc_obf_active" class="wtc-toggle-label"></label>
                                        </div>
                                        <p class="description">
                                            <?php esc_html_e('OBF için gösterim türünü seç.', 'wtc-checkout'); ?>
                                        </p>
                                        <select name="wtc_contract_settings[obf_display_mode]" class="wtc-select">
                                            <option value="popup" <?php selected(isset($settings['obf_display_mode']) && $settings['obf_display_mode'] == 'popup'); ?>>
                                                <?php esc_html_e('Popup', 'wtc-checkout'); ?>
                                            </option>
                                            <option value="text" <?php selected(isset($settings['obf_display_mode']) && $settings['obf_display_mode'] == 'text'); ?>>
                                                <?php esc_html_e('Metin', 'wtc-checkout'); ?>
                                            </option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row"><?php esc_html_e('Özel Sözleşme', 'wtc-checkout'); ?></th>
                                    <td>
                                        <div class="wtc-toggle-wrapper">
                                            <input type="checkbox" id="wtc_custom_active" name="wtc_contract_settings[custom_active]" 
                                                value="1" <?php checked(isset($settings['custom_active']) && $settings['custom_active']); ?>
                                                class="wtc-toggle-input">
                                            <label for="wtc_custom_active" class="wtc-toggle-label"></label>
                                        </div>
                                        <p class="description">
                                            <?php esc_html_e('Özel sözleşme için gösterim türünü seç.', 'wtc-checkout'); ?>
                                        </p>
                                        <select name="wtc_contract_settings[custom_display_mode]" class="wtc-select">
                                            <option value="popup" <?php selected(isset($settings['custom_display_mode']) && $settings['custom_display_mode'] == 'popup'); ?>>
                                                <?php esc_html_e('Popup', 'wtc-checkout'); ?>
                                            </option>
                                            <option value="text" <?php selected(isset($settings['custom_display_mode']) && $settings['custom_display_mode'] == 'text'); ?>>
                                                <?php esc_html_e('Metin', 'wtc-checkout'); ?>
                                            </option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row"><?php esc_html_e('Özel Sözleşme Başlığı', 'wtc-checkout'); ?></th>
                                    <td>
                                        <input type="text" name="wtc_contract_settings[custom_title]" 
                                            value="<?php echo esc_attr($settings['custom_title'] ?? esc_html__('Özel Sözleşme', 'wtc-checkout')); ?>" 
                                            class="regular-text" placeholder="<?php esc_attr_e('Özel Sözleşme Başlığı', 'wtc-checkout'); ?>" />
                                        <p class="description">
                                            <?php esc_html_e('Özel sözleşmenin başlığını belirleyin', 'wtc-checkout'); ?>
                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row"><?php esc_html_e('Sözleşme Konumu', 'wtc-checkout'); ?></th>
                                    <td>
                                        <select name="wtc_contract_settings[position]" class="wtc-select">
                                            <option value="before_terms" <?php selected(isset($settings['position']) && $settings['position'] == 'before_terms'); ?>>
                                                <?php esc_html_e('Şartlar ve Koşullar Öncesi', 'wtc-checkout'); ?>
                                            </option>
                                            <option value="after_terms" <?php selected(isset($settings['position']) && $settings['position'] == 'after_terms'); ?>>
                                                <?php esc_html_e('Şartlar ve Koşullar Sonrası', 'wtc-checkout'); ?>
                                            </option>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="wtc-settings-section">
                            <h3><?php esc_html_e('PDF Yönetimi', 'wtc-checkout'); ?></h3>
                            <table class="form-table wtc-form-table">
                                <tr>
                                    <th scope="row"><?php esc_html_e('PDF Gösterimi', 'wtc-checkout'); ?></th>
                                    <td>
                                    <div class="wtc-toggle-wrapper">
                                        <input type="checkbox" id="wtc_show_pdf_thankyou" name="wtc_contract_settings[show_pdf_thankyou]" 
                                               value="1" <?php checked(isset($settings['show_pdf_thankyou']) && $settings['show_pdf_thankyou']); ?>
                                               class="wtc-toggle-input">
                                        <label for="wtc_show_pdf_thankyou" class="wtc-toggle-label"></label>
                                    </div>
                                    <p class="description"><?php esc_html_e('Teşekkür sayfasında PDF bağlantılarını göster.', 'wtc-checkout'); ?></p>
                                    <div class="wtc-toggle-wrapper">
                                        <input type="checkbox" id="wtc_show_pdf_myaccount" name="wtc_contract_settings[show_pdf_myaccount]" 
                                               value="1" <?php checked(isset($settings['show_pdf_myaccount']) && $settings['show_pdf_myaccount']); ?>
                                               class="wtc-toggle-input">
                                        <label for="wtc_show_pdf_myaccount" class="wtc-toggle-label"></label>
                                    </div>
                                    <p class="description"><?php esc_html_e('Sipariş detaylarında PDF bağlantılarını göster.', 'wtc-checkout'); ?></p>
                                    <div class="wtc-toggle-wrapper">
                                        <input type="checkbox" id="wtc_show_pdf_email" name="wtc_contract_settings[show_pdf_email]" 
                                               value="1" <?php checked(isset($settings['show_pdf_email']) && $settings['show_pdf_email']); ?>
                                               class="wtc-toggle-input">
                                        <label for="wtc_show_pdf_email" class="wtc-toggle-label"></label>
                                    </div>
                                    <p class="description"><?php esc_html_e('Sipariş e-postalarında PDF bağlantılarını göster.', 'wtc-checkout'); ?></p>
                                </td>
                            </tr>
                            <tr>
                                <th scope="row"><?php esc_html_e('PDF Gönderimi', 'wtc-checkout'); ?></th>
                                <td>
                                    <div class="wtc-toggle-wrapper">
                                        <input type="checkbox" id="wtc_attach_pdf_email" name="wtc_contract_settings[attach_pdf_email]" 
                                               value="1" <?php checked(isset($settings['attach_pdf_email']) && $settings['attach_pdf_email']); ?>
                                               class="wtc-toggle-input">
                                        <label for="wtc_attach_pdf_email" class="wtc-toggle-label"></label>
                                        <span class="toggle-label"><?php echo esc_html__('PDF Dosyalarını E-posta Eki Olarak Gönder', 'wtc-checkout'); ?></span>
                                    </div>
                                    <p class="description">
                                        <?php esc_html_e('Sözleşme PDF\'lerini sipariş durumu "TAMAMLANDI" e-postasına ek olarak ekler.', 'wtc-checkout'); ?>
                                    </p>
                                </td>
                            </tr>
                        </table>
                        <?php submit_button(esc_html__('Değişiklikleri kaydet', 'wtc-checkout'), 'primary wtc-button wtc-button-primary'); ?>
                    </form>
                <?php elseif ($this->active_tab == 'mss_template'): ?>
                    <div class="contract-templates-wrapper">
                        <div class="template-variables">
                            <?php
                            $variables = $this->get_template_variables();
                            foreach ($variables as $category => $vars): ?>
                                <div class="variable-category">
                                    <h3><?php echo esc_html($category); ?></h3>
                                    <div class="variable-buttons">
                                        <?php foreach ($vars as $variable => $description): ?>
                                            <button type="button" 
                                                    class="button wtc-button wtc-button-secondary" 
                                                    data-variable="<?php echo esc_attr($variable); ?>" 
                                                    title="<?php echo esc_attr($description); ?>">
                                                <?php echo esc_html($description); ?>
                                            </button>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <form method="post" action="options.php">
                            <?php
                            settings_fields('wtc_contract_templates_group');
                            $templates = get_option('wtc_contract_templates', array());
                            $obf_template = isset($templates['obf_template']) ? $templates['obf_template'] : '';
                            $custom_template = isset($templates['custom_template']) ? $templates['custom_template'] : '';  
                            ?>
                            <input type="hidden" name="wtc_contract_templates[obf_template]" value="<?php echo esc_attr($obf_template); ?>">
                            <input type="hidden" name="wtc_contract_templates[custom_template]" value="<?php echo esc_attr($custom_template); ?>">
                            <div class="contract-template-editor">
                                <h3><?php esc_html_e('Mesafeli Satış Sözleşmesi', 'wtc-checkout'); ?></h3>
                                <?php
                                wp_editor(
                                    $templates['mss_template'] ?? '',
                                    'mss_template',
                                    array(
                                        'textarea_name' => 'wtc_contract_templates[mss_template]',
                                        'textarea_rows' => 20,
                                        'media_buttons' => false,
                                        'tinymce' => array(
                                            'add_unload_trigger' => false,
                                            'setup' => 'function(editor) {
                                                editor.on("focus", function() {
                                                    tinymce.activeEditor = editor;
                                                });
                                            }'
                                        ),
                                        'quicktags' => true
                                    )
                                );
                                ?>
                            </div>
                            <?php submit_button(esc_html__('Değişiklikleri kaydet', 'wtc-checkout'), 'primary wtc-button wtc-button-primary'); ?>
                        </form>
                    </div>
                <?php elseif ($this->active_tab == 'obf_template'): ?>
                    <div class="contract-templates-wrapper">
                        <div class="template-variables">
                            <?php
                            $variables = $this->get_template_variables();
                            foreach ($variables as $category => $vars): ?>
                                <div class="variable-category">
                                    <h3><?php echo esc_html($category); ?></h3>
                                    <div class="variable-buttons">
                                        <?php foreach ($vars as $variable => $description): ?>
                                            <button type="button" 
                                                    class="button wtc-button wtc-button-secondary" 
                                                    data-variable="<?php echo esc_attr($variable); ?>" 
                                                    title="<?php echo esc_attr($description); ?>">
                                                <?php echo esc_html($description); ?>
                                            </button>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <form method="post" action="options.php">
                            <?php
                            settings_fields('wtc_contract_templates_group');
                            $templates = get_option('wtc_contract_templates', array());
                            $mss_template = isset($templates['mss_template']) ? $templates['mss_template'] : '';
                            $custom_template = isset($templates['custom_template']) ? $templates['custom_template'] : '';
                            ?>
                            <input type="hidden" name="wtc_contract_templates[mss_template]" value="<?php echo esc_attr($mss_template); ?>">
                            <input type="hidden" name="wtc_contract_templates[custom_template]" value="<?php echo esc_attr($custom_template); ?>">
                            <div class="contract-template-editor">
                                <h3><?php esc_html_e('Ön Bilgilendirme Formu', 'wtc-checkout'); ?></h3>
                                <?php
                                wp_editor(
                                    $templates['obf_template'] ?? '',
                                    'obf_template',
                                    array(
                                        'textarea_name' => 'wtc_contract_templates[obf_template]',
                                        'textarea_rows' => 20,
                                        'media_buttons' => false,
                                        'tinymce' => array(
                                            'add_unload_trigger' => false,
                                            'setup' => 'function(editor) {
                                                editor.on("focus", function() {
                                                    tinymce.activeEditor = editor;
                                                });
                                            }'
                                        ),
                                        'quicktags' => true
                                    )
                                );
                                ?>
                            </div>
                            <?php submit_button(esc_html__('Değişiklikleri kaydet', 'wtc-checkout'), 'primary wtc-button wtc-button-primary'); ?>
                        </form>
                    </div>
                <?php elseif ($this->active_tab == 'custom_template'): ?>
                    <div class="contract-templates-wrapper">
                        <div class="template-variables">
                            <?php
                            $variables = $this->get_template_variables();
                            foreach ($variables as $category => $vars): ?>
                                <div class="variable-category">
                                    <h3><?php echo esc_html($category); ?></h3>
                                    <div class="variable-buttons">
                                        <?php foreach ($vars as $variable => $description): ?>
                                            <button type="button" 
                                                    class="button wtc-button wtc-button-secondary" 
                                                    data-variable="<?php echo esc_attr($variable); ?>" 
                                                    title="<?php echo esc_attr($description); ?>">
                                                <?php echo esc_html($description); ?>
                                            </button>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <form method="post" action="options.php">
                            <?php
                            settings_fields('wtc_contract_templates_group');
                            $templates = get_option('wtc_contract_templates', array());
                            $mss_template = isset($templates['mss_template']) ? $templates['mss_template'] : '';
                            $obf_template = isset($templates['obf_template']) ? $templates['obf_template'] : '';
                            ?>
                            <input type="hidden" name="wtc_contract_templates[mss_template]" value="<?php echo esc_attr($mss_template); ?>">
                            <input type="hidden" name="wtc_contract_templates[obf_template]" value="<?php echo esc_attr($obf_template); ?>">
                            <div class="contract-template-editor">
                                <h3><?php esc_html_e('Özel Sözleşme', 'wtc-checkout'); ?></h3>
                                <?php
                                wp_editor(
                                    $templates['custom_template'] ?? '',
                                    'custom_template',
                                    array(
                                        'textarea_name' => 'wtc_contract_templates[custom_template]',
                                        'textarea_rows' => 20,
                                        'media_buttons' => false,
                                        'tinymce' => array(
                                            'add_unload_trigger' => false,
                                            'setup' => 'function(editor) {
                                                editor.on("focus", function() {
                                                    tinymce.activeEditor = editor;
                                                });
                                            }'
                                        ),
                                        'quicktags' => true
                                    )
                                );
                                ?>
                            </div>
                            <?php submit_button(esc_html__('Değişiklikleri kaydet', 'wtc-checkout'), 'primary wtc-button wtc-button-primary'); ?>
                        </form>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
    public function render_company_field($args) {
        $field_id = $args['field_id'];
        $option_name = 'wtc_company_info';
        $options = get_option($option_name, array());
        $value = isset($options[$field_id]) ? $options[$field_id] : '';
        if (in_array($field_id, array('f_company_address', 'f_return_address'))) {
            ?>
            <textarea 
                id="wtc_company_<?php echo esc_attr($field_id); ?>"
                name="<?php echo esc_attr($option_name); ?>[<?php echo esc_attr($field_id); ?>]"
                rows="4"
                cols="50"
                class="regular-text"
            ><?php echo esc_textarea($value); ?></textarea>
            <?php
        } else {
            ?>
            <input 
                type="text"
                id="wtc_company_<?php echo esc_attr($field_id); ?>"
                name="<?php echo esc_attr($option_name); ?>[<?php echo esc_attr($field_id); ?>]"
                value="<?php echo esc_attr($value); ?>"
                class="regular-text"
            />
            <?php
        }
        $descriptions = array(
            'f_company_name' => __('Firmanızın tam ticari unvanı', 'wtc-checkout'),
            'f_company_address' => __('Firmanızın açık adresi', 'wtc-checkout'),
            'f_tax_office' => __('Vergi dairesi adı', 'wtc-checkout'),
            'f_tax_number' => __('Vergi numarası', 'wtc-checkout'),   
            'f_phone' => __('Firma telefon numarası', 'wtc-checkout'),
            'f_email' => __('Firma e-posta adresi', 'wtc-checkout'),
            'f_website' => __('Firma web sitesi adresi', 'wtc-checkout'),
            'f_return_address' => __('Ürün iade adresi', 'wtc-checkout'),
            'f_mersis_no' => __('MERSİS numarası', 'wtc-checkout'),
            'f_trade_chamber' => __('Bağlı olduğunuz ticaret odası', 'wtc-checkout'),
            'f_trade_registry_no' => __('Ticaret sicil numarası', 'wtc-checkout')
        );
        if (isset($descriptions[$field_id])) {
            echo '<p class="description">' . esc_html($descriptions[$field_id]) . '</p>';
        }
    }
    public function sanitize_company_info($input) {
        if (!is_array($input)) return array();
        $sanitized = array();
        $text_fields = array(
            'f_company_name',
            'f_company_address',  
            'f_tax_office',    
            'f_tax_number',   
            'f_phone',
            'f_email',
            'f_website',
            'f_return_address',
            'f_mersis_no',
            'f_trade_chamber',
            'f_trade_registry_no'
        );
        foreach ($text_fields as $field) {
            if (isset($input[$field])) {
                if (in_array($field, array('f_company_address', 'f_return_address'))) {
                    $sanitized[$field] = sanitize_textarea_field($input[$field]);
                } else {
                    $sanitized[$field] = sanitize_text_field($input[$field]);
                }
            }
        }
        return $sanitized;
    }
    public function sanitize_templates($input) {
        if (!is_array($input)) {
            return array();
        }
        $sanitized = array();
        $current_templates = get_option('wtc_contract_templates', array());
        if (isset($input['mss_template'])) {
            $sanitized['mss_template'] = wp_kses_post($input['mss_template']);
        } else {
            $sanitized['mss_template'] = isset($current_templates['mss_template']) ? $current_templates['mss_template'] : '';
        }
        if (isset($input['obf_template'])) {
            $sanitized['obf_template'] = wp_kses_post($input['obf_template']);
        } else {
            $sanitized['obf_template'] = isset($current_templates['obf_template']) ? $current_templates['obf_template'] : '';
        }
        if (isset($input['custom_template'])) {
            $sanitized['custom_template'] = wp_kses_post($input['custom_template']);
        } else {
            $sanitized['custom_template'] = isset($current_templates['custom_template']) ? $current_templates['custom_template'] : '';
        }
        return $sanitized;
    }
    public function sanitize_contract_settings($input) {
        if (!is_array($input)) {
            return array();
        }
        $sanitized = array();
        $sanitized['mss_active'] = isset($input['mss_active']) ? (bool)$input['mss_active'] : false;
        $sanitized['obf_active'] = isset($input['obf_active']) ? (bool)$input['obf_active'] : false;
        $sanitized['custom_active'] = isset($input['custom_active']) ? (bool)$input['custom_active'] : false;
        if (isset($input['custom_title'])) {
            $sanitized['custom_title'] = sanitize_text_field($input['custom_title']);
        }
        $checkbox_fields = array(
            'show_pdf_thankyou',
            'show_pdf_email',
            'show_pdf_myaccount',
            'attach_pdf_email'
        );
        foreach ($checkbox_fields as $field) {
            $sanitized[$field] = isset($input[$field]) ? (bool)$input[$field] : false;
        }
        $text_fields = array(
            'position',
            'mss_display_mode',
            'obf_display_mode',
            'custom_display_mode',
        );
        foreach ($text_fields as $field) {
            if (isset($input[$field])) {
                $sanitized[$field] = sanitize_text_field($input[$field]);
            }
        }
        $current_settings = get_option('wtc_contract_settings', array());
        $sanitized = array_merge($current_settings, $sanitized);
        return $sanitized;
    }
    private function get_template_variables() {
        return array(
            'Firma Bilgileri' => array(
                '{F_ADI}' => 'Firma Adı',
                '{F_ADRES}' => 'Firma Adresi',
                '{F_VD}' => 'Vergi Dairesi',
                '{F_VNO}' => 'Vergi Numarası',
                '{F_TEL}' => 'Firma Telefon',
                '{F_EPOSTA}' => 'Firma E-posta',
                '{F_WEB}' => 'Firma Web Sitesi',
                '{F_IADE}' => 'İade Adresi',
                '{F_MERSIS}' => 'MERSİS No',
                '{F_TO}' => 'Ticaret Odası',
                '{F_TSN}' => 'Ticaret Sicil No'
            ),
            'Müşteri Bilgileri' => array(
                '{FATURA_AD_SOYAD}' => 'Müşteri Adı Soyadı',
                '{FATURA_FIRMA}' => 'Firma Adı',
                '{FATURA_TC_NO}' => 'TC Kimlik No',
                '{FATURA_VERGI_NO}' => 'Vergi No',
                '{FATURA_VERGI_DAIRESI}' => 'Vergi Dairesi',
                '{FATURA_TELEFON}' => 'Telefon',
                '{FATURA_EMAIL}' => 'E-posta',
                '{FATURA_ADRES_TAMAMI}' => 'Fatura Adresi',
                '{TESLIMAT_AD_SOYAD}' => 'Teslimat Adı Soyadı',
                '{TESLIMAT_FIRMA}' => 'Teslimat Firma Adı',
                '{TESLIMAT_ADRES_TAMAMI}' => 'Teslimat Adresi',
				'{MUSTERI_IP}' => 'Müşteri IP Adresi'
            ),
            'Sipariş Bilgileri' => array(
                '{SIPARIS_TARIHI}' => 'Sipariş Tarihi',
                '{ODEME_YONTEMI}' => 'Ödeme Yöntemi',
                '{SIPARIS_TOPLAMI}' => 'Sipariş Toplamı',
                '{ARA_TOPLAM}' => 'Ara Toplam',
                '{KARGO_UCRETI}' => 'Kargo Ücreti',
                '{VERGI_TUTARI}' => 'Vergi Tutarı',
                '{INDIRIM_TUTARI}' => 'İndirim Tutarı',
                '{TOPLAM_URUN}' => 'Toplam Ürün Adedi',
				'{URUN_ADLARI}' => 'Ürün Adları',
                '{URUN_LISTESI}' => 'Ürün Listesi',
                '{SIPARIS_TABLOSU}' => 'Sipariş Tablosu',
                '{KUPON_KODU}' => 'Kupon Kodu'
            )
        );
    }
} 