<?php
namespace WTC_CHECKOUT\Core\General;
defined('ABSPATH') || exit;
class GeneralSettings {
    private $page_slug = 'wtc-general-settings';
    private $register_fields_option = 'wtc_register_name_fields_enabled';
    private $register_phone_option = 'wtc_register_phone_field_enabled';
    private $tckn_validation_option = 'wtc_tckn_validation_enabled';
    private $corporate_tax_validation_option = 'wtc_corporate_tax_validation_enabled';
    private $tckn_required_option = 'wtc_tckn_required';
    private $hide_order_notes_option = 'wtc_hide_order_notes';
    private $password_strength_option = 'wtc_password_strength';
    private $tabs = [];
    private $active_tab = '';
    public function __construct() {
        $this->tabs = [
            'settings' => __('Ayarlar', 'wtc-checkout'),
        ];
        add_action('admin_init', array($this, 'register_settings'));
    }
    public function register_settings() {
        register_setting(
            $this->page_slug,
            $this->register_fields_option,
            array(
                'type' => 'string',
                'default' => 'no',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            $this->register_phone_option,
            array(
                'type' => 'string',
                'default' => 'no',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            $this->tckn_validation_option,
            array(
                'type' => 'string',
                'default' => 'no',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            $this->corporate_tax_validation_option,
            array(
                'type' => 'string',
                'default' => 'no',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            $this->tckn_required_option,
            array(
                'type' => 'string',
                'default' => 'yes',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            'wtc_shipping_visibility',
            array(
                'type' => 'string',
                'default' => 'show_all',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            'wtc_hide_cart_shipping_address',
            array(
                'type' => 'boolean',
                'default' => false,
                'sanitize_callback' => 'rest_sanitize_boolean'
            )
        );
        register_setting(
            $this->page_slug,
            'wtc_use_tl_symbol',
            array(
                'type' => 'boolean',
                'default' => false,
                'sanitize_callback' => 'rest_sanitize_boolean'
            )
        );
        register_setting(
            $this->page_slug, 
            $this->hide_order_notes_option,
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        register_setting(
            $this->page_slug,
            $this->password_strength_option,
            array(
                'type' => 'integer',
                'default' => 3,
                'sanitize_callback' => array($this, 'sanitize_password_strength')
            )
        );
        register_setting(
            $this->page_slug, 
            'wtc_corporate_names_required',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
    }
    public function sanitize_password_strength($value) {
        return min(4, max(0, intval($value)));
    }
    private function render_tabs() {
        echo '<h2 class="wtc-nav-tab-wrapper nav-tab-wrapper">';
        $nonce = wp_create_nonce('wtc_general_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() {
        if (!current_user_can('manage_woocommerce')) {
            wp_die(esc_html__('Bu sayfaya erişim yetkiniz yok.', 'wtc-checkout'));
        }
        $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_general_tab_action') && array_key_exists($tab, $this->tabs)) {
            $this->active_tab = $tab;
        } else {
            $this->active_tab = 'settings';
        }
        echo '<div class="wrap wtc-admin-page">';
        echo '<h1>' . esc_html(get_admin_page_title()) . '</h1>';
        $this->render_tabs();
        echo '<div class="wtc-card">';
        switch ($this->active_tab) {
            case 'settings':
            default:
                $this->render_general_settings();
                break;
        }
        echo '</div>';
        echo '</div>';
    }
    private function render_general_settings() {
        $register_fields_enabled = get_option($this->register_fields_option, 'no');
        $register_phone_enabled = get_option($this->register_phone_option, 'no');
        $tckn_validation_enabled = get_option($this->tckn_validation_option, 'no');
        $corporate_tax_validation_enabled = get_option($this->corporate_tax_validation_option, 'no');
        $tckn_required = get_option($this->tckn_required_option, 'yes');
        $shipping_visibility = get_option('wtc_shipping_visibility', 'show_all');
        $hide_cart_shipping_address = get_option('wtc_hide_cart_shipping_address', false);
        $use_tl_symbol = get_option('wtc_use_tl_symbol', false);
        $hide_order_notes = get_option($this->hide_order_notes_option, 'no');
        $password_strength = get_option($this->password_strength_option, 3);
        ?>
        <form method="post" action="options.php">
            <?php settings_fields($this->page_slug); ?>
            <div class="wtc-settings-section">
            <table class="form-table wtc-form-table">
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Üye Kayıt Formu Ad/Soyad Alanları', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_register_fields" 
                                   name="<?php echo esc_attr($this->register_fields_option); ?>" 
                                   value="yes" 
                                   <?php checked($register_fields_enabled, 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_register_fields" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('WooCommerce üye kayıt formunda ad ve soyad alanlarını göster/gizle', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Üye Kayıt Formu Telefon Alanı', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_register_phone" 
                                   name="<?php echo esc_attr($this->register_phone_option); ?>" 
                                   value="yes" 
                                   <?php checked($register_phone_enabled, 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_register_phone" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('WooCommerce üye kayıt formunda telefon numarası alanını göster/gizle', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Kargo Metodu Görünürlüğü', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <select name="wtc_shipping_visibility" class="wtc-select">
                            <option value="show_all" <?php selected($shipping_visibility, 'show_all'); ?>>
                                <?php esc_html_e('Tüm kargo seçeneklerini göster', 'wtc-checkout'); ?>
                            </option>
                            <option value="only_free" <?php selected($shipping_visibility, 'only_free'); ?>>
                                <?php esc_html_e('Sadece ücretsiz kargoyu göster', 'wtc-checkout'); ?>
                            </option>
                            <option value="all_free" <?php selected($shipping_visibility, 'all_free'); ?>>
                                <?php esc_html_e('Tüm ücretsiz seçenekleri göster', 'wtc-checkout'); ?>
                            </option>
                        </select>
                        <p class="description"><?php esc_html_e('Ücretsiz kargo seçeneği aktif olduğunda ve minimum sepet tutarı sağlandığında bu ayarlar geçerli olur.', 'wtc-checkout'); ?></p>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Sepet Sayfası Teslimat Adresi', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_hide_cart_shipping_address" 
                                   name="wtc_hide_cart_shipping_address" 
                                   value="1" 
                                   <?php checked($hide_cart_shipping_address, true); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_hide_cart_shipping_address" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('Sepet sayfasındaki teslimat adresi özetini gizle', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('TC Kimlik No Zorunlu', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_tckn_required" 
                                   name="<?php echo esc_attr($this->tckn_required_option); ?>" 
                                   value="yes" 
                                   <?php checked($tckn_required, 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_tckn_required" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('Bireysel fatura tipinde TC Kimlik No alanını zorunlu yap', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Bireysel TC Kimlik No Doğrulama', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_tckn_validation" 
                                   name="<?php echo esc_attr($this->tckn_validation_option); ?>" 
                                   value="yes" 
                                   <?php checked($tckn_validation_enabled, 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_tckn_validation" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('Bireysel fatura tipinde TC Kimlik Numarası doğrulama sistemini aktifleştir', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Kurumsal Vergi/TC Kimlik No Doğrulama', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_corporate_tax_validation" 
                                   name="<?php echo esc_attr($this->corporate_tax_validation_option); ?>" 
                                   value="yes" 
                                   <?php checked($corporate_tax_validation_enabled, 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_corporate_tax_validation" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('Kurumsal fatura tipinde Vergi Numarası/TC Kimlik Numarası doğrulama sistemini aktifleştir', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Kurumsal Faturada Ad/Soyad', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_corporate_names_required" 
                                   name="wtc_corporate_names_required" 
                                   value="yes" 
                                   <?php checked(get_option('wtc_corporate_names_required', 'no'), 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_corporate_names_required" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('Kurumsal fatura seçildiğinde Ad ve Soyad alanlarını göster ve zorunlu kıl (Firma adından otomatik oluşturma devre dışı kalır).', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Para Birimi Simgesi', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_use_tl_symbol" 
                                   name="wtc_use_tl_symbol" 
                                   value="1" 
                                   <?php checked($use_tl_symbol, true); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_use_tl_symbol" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('WooCommerce\'in standart "₺" simgesi yerine "TL" kullan', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Sipariş Notu Gizle', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <div class="wtc-toggle-wrapper-settings">
                            <input type="checkbox" 
                                   id="wtc_hide_order_notes" 
                                   name="<?php echo esc_attr($this->hide_order_notes_option); ?>" 
                                   value="yes" 
                                   <?php checked($hide_order_notes, 'yes'); ?>
                                   class="wtc-toggle-input" />
                            <label for="wtc_hide_order_notes" class="wtc-toggle-label"></label>
                            <p class="description"><?php esc_html_e('Ödeme sayfasındaki sipariş notu alanını tamamen gizle', 'wtc-checkout'); ?></p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <?php esc_html_e('Şifre Gücü Gereksinimi', 'wtc-checkout'); ?>
                    </th>
                    <td>
                        <select name="<?php echo esc_attr($this->password_strength_option); ?>" class="wtc-select">
                            <option value="0" <?php selected($password_strength, 0); ?>>
                                <?php esc_html_e('Çok Zayıf', 'wtc-checkout'); ?>
                            </option>
                            <option value="1" <?php selected($password_strength, 1); ?>>
                                <?php esc_html_e('Zayıf', 'wtc-checkout'); ?>
                            </option>
                            <option value="3" <?php selected($password_strength, 3); ?>>
                                <?php esc_html_e('Orta (Varsayılan)', 'wtc-checkout'); ?>
                            </option>
                            <option value="4" <?php selected($password_strength, 4); ?>>
                                <?php esc_html_e('Güçlü', 'wtc-checkout'); ?>
                            </option>
                        </select>
                        <p class="description"><?php esc_html_e('Kullanıcıların kayıt olurken kullanması gereken minimum şifre gücü seviyesini belirleyin.', 'wtc-checkout'); ?></p>
                    </td>
                </tr>
            </table>
            </div>
            <?php submit_button(esc_html__('Değişiklikleri kaydet', 'wtc-checkout'), 'primary wtc-button wtc-button-primary'); ?>
        </form>
        <?php
    }
} 