<?php
namespace WTC_CHECKOUT\Modules\Points\Admin;
defined('ABSPATH') || exit;
class PointsSettings {
    private $active_tab = 'settings';
    public function __construct() {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $this->active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'settings';
        add_action('admin_init', [$this, 'register_settings']);
        add_action('wp_ajax_wtc_manual_point_transaction', [$this, 'ajax_manual_transaction']);
    }
    private function get_transaction_type_label($type) {
        $labels = [
            'manual'       => __('MANUEL İŞLEM', 'wtc-checkout'),
            'earn'         => __('SİPARİŞ KAZANCI', 'wtc-checkout'),
            'spend'        => __('HARCAMA', 'wtc-checkout'),
            'signup_bonus' => __('HOŞGELDİN BONUSU', 'wtc-checkout'),
            'first_order_bonus' => __('İLK SİPARİŞ BONUSU', 'wtc-checkout'),
            'refund_earn'  => __('İADE (GERİ ALIM)', 'wtc-checkout'),
            'refund_spend' => __('İADE (PUAN İADESİ)', 'wtc-checkout'),
        ];
        return isset($labels[$type]) ? $labels[$type] : ucfirst($type);
    }
    public function register_settings() {
        register_setting('wtc_points_options', 'wtc_points_enabled', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('wtc_points_options', 'wtc_points_coupon_compatibility', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('wtc_points_options', 'wtc_point_value', array('sanitize_callback' => 'floatval'));
        register_setting('wtc_points_options', 'wtc_point_earn_rate', array('sanitize_callback' => 'floatval'));
        register_setting('wtc_points_options', 'wtc_point_label', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('wtc_points_options', 'wtc_point_menu_title', array('sanitize_callback' => 'sanitize_text_field'));
        register_setting('wtc_points_options', 'wtc_point_endpoint', array('sanitize_callback' => 'sanitize_title'));
        register_setting('wtc_points_options', 'wtc_signup_bonus', array('sanitize_callback' => 'absint')); 
        register_setting('wtc_points_options', 'wtc_first_order_bonus', array('sanitize_callback' => 'absint')); 
        register_setting('wtc_points_options', 'wtc_min_cart_total', array('sanitize_callback' => 'absint')); 
        register_setting('wtc_points_options', 'wtc_point_expiry_days', array('sanitize_callback' => 'absint')); 
        register_setting('wtc_points_options', 'wtc_apply_to_shipping', array('sanitize_callback' => 'sanitize_text_field')); 
        register_setting('wtc_points_options', 'wtc_max_point_limit', array('sanitize_callback' => 'absint'));
        register_setting('wtc_points_options', 'wtc_min_point_limit', array('sanitize_callback' => 'absint'));
    }
    public function render_settings_page() {
        ?>
        <div class="wrap wtc-admin-page">
            <h1><?php esc_html_e('Puan Yönetimi', 'wtc-checkout'); ?></h1>
            <h2 class="wtc-nav-tab-wrapper nav-tab-wrapper">
                <a href="?page=wtc-points&tab=settings" class="wtc-nav-tab nav-tab <?php echo $this->active_tab === 'settings' ? 'nav-tab-active wtc-nav-tab-active' : ''; ?>">
                    <?php esc_html_e('Ayarlar', 'wtc-checkout'); ?>
                </a>
                <a href="?page=wtc-points&tab=manual" class="wtc-nav-tab nav-tab <?php echo $this->active_tab === 'manual' ? 'nav-tab-active wtc-nav-tab-active' : ''; ?>">
                    <?php esc_html_e('Manuel İşlem', 'wtc-checkout'); ?>
                </a>
                <a href="?page=wtc-points&tab=logs" class="wtc-nav-tab nav-tab <?php echo $this->active_tab === 'logs' ? 'nav-tab-active wtc-nav-tab-active' : ''; ?>">
                    <?php esc_html_e('Kayıtlar', 'wtc-checkout'); ?>
                </a>
                <a href="?page=wtc-points&tab=users" class="wtc-nav-tab nav-tab <?php echo $this->active_tab === 'users' ? 'nav-tab-active wtc-nav-tab-active' : ''; ?>">
                    <?php esc_html_e('Kullanıcı Puanları', 'wtc-checkout'); ?>
                </a>
<?php
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://www.woosoft.com.tr" target="_blank" class="wtc-premium-link">⭐&nbsp;&nbsp;Premium</a>';
echo '<a href="https://coff.ee/woosoft" target="_blank" class="wtc-support-link">☕&nbsp;&nbsp;Buy me a coffee</a>';
echo '</span>';
?>
            </h2>
            <div class="wtc-card">
                <?php
                if ($this->active_tab === 'settings') {
                    $this->render_general_settings();
                } elseif ($this->active_tab === 'manual') {
                    $this->render_manual_transaction_page();
                } elseif ($this->active_tab === 'users') {
                    $this->render_users_page();
                } else {
                    $this->render_logs();
                }
                ?>
            </div>
        </div>
        <?php
    }
    private function render_general_settings() { ?>
        <form method="post" class="wtc-settings-form">
        <input type="hidden" name="option_page" value="wtc_points_options">
        <input type="hidden" name="action" value="update">
        <input type="hidden" id="_wpnonce" name="_wpnonce" value="b44041f0cd">
        <input type="hidden" name="_wp_http_referer" value="/wp-admin/admin.php?page=wtc-points&amp;tab=settings&amp;settings-updated=true">
        <div class="wtc-settings-section">
            <table class="form-table wtc-form-table">
            <tbody>
                <tr>
                <th scope="row">Puan Sistemi</th>
                <td>
                    <div class="wtc-toggle-wrapper">
                    <input type="hidden" name="wtc_points_enabled" value="no">
                    <input type="checkbox" id="wtc_points_enabled" name="wtc_points_enabled" value="yes" class="wtc-toggle-input">
                    <label for="wtc_points_enabled" class="wtc-toggle-label"></label>
                    <span class="toggle-label">Puan sistemini aktif et</span>
                    </div>
                    <p class="description"> Puan sistemi ile tamamlanan siparişlerde puan kazanılır ve harcanabilir. İlk kurulumda <a href="/wp-admin/options-permalink.php" target="_blank">Ayarlar &gt; Kalıcı Bağlantılar</a> sayfasına gidip hiçbir değişiklik yapmadan "Değişiklikleri Kaydet" butonuna basınız. </p>
                </td>
                </tr>
            </tbody>
            </table>
        </div>
        <div class="wtc-settings-section">
            <h3>Genel Ayarlar</h3>
            <table class="form-table wtc-form-table">
            <tbody>
                <tr>
                <th scope="row">Puan Etiketi</th>
                <td>
                    <input type="text" name="wtc_point_label" value="Puan" class="regular-text">
                    <p class="description">Müşterilerin göreceği para birimi adı (Örn: Puan, Coin, Bonus).</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Hesabım Menü Başlığı</th>
                <td>
                    <input type="text" name="wtc_point_menu_title" value="Puanlarım" class="regular-text">
                    <p class="description">Hesabım sayfasındaki menüde görünecek başlık (Örn: Cüzdanım, Puanlarım).</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Hesabım Sayfa Linki</th>
                <td>
                    <input type="text" name="wtc_point_endpoint" value="puanlarim" class="regular-text">
                    <p class="description"> Hesabım sayfasındaki URL son eki (Örn: puanlarim). Değişiklikten sonra <a href="/wp-admin/options-permalink.php" target="_blank">Ayarlar &gt; Kalıcı Bağlantılar</a> sayfasına gidip hiçbir değişiklik yapmadan "Değişiklikleri Kaydet" butonuna basınız. </p>
                </td>
                </tr>
                <tr>
                <th scope="row">Kargo Ücreti Dahil</th>
                <td>
                    <div class="wtc-toggle-wrapper">
                    <input type="hidden" name="wtc_apply_to_shipping" value="no">
                    <input type="checkbox" id="wtc_apply_to_shipping" name="wtc_apply_to_shipping" value="yes" class="wtc-toggle-input">
                    <label for="wtc_apply_to_shipping" class="wtc-toggle-label"></label>
                    <span class="toggle-label">Puan indirimi kargo ücretini de kapsasın</span>
                    </div>
                    <p class="description">Aktif edilirse, puanlar kargo ücretini ödemek için de kullanılabilir.</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Kupon Uyumluluğu</th>
                <td>
                    <div class="wtc-toggle-wrapper">
                    <input type="hidden" name="wtc_points_coupon_compatibility" value="no">
                    <input type="checkbox" id="wtc_points_coupon_compatibility" name="wtc_points_coupon_compatibility" value="yes" checked="checked" class="wtc-toggle-input">
                    <label for="wtc_points_coupon_compatibility" class="wtc-toggle-label"></label>
                    <span class="toggle-label">Puan ve Kupon birlikte kullanılabilsin</span>
                    </div>
                    <p class="description">Pasif yapılırsa, sepette kupon kodu varsa puan kullanımı engellenir.</p>
                </td>
                </tr>
            </tbody>
            </table>
        </div>
        <div class="wtc-settings-section">
            <h3>Değer ve Dönüşüm</h3>
            <table class="form-table wtc-form-table">
            <tbody>
                <tr>
                <th scope="row">1 Puan Değeri (TL)</th>
                <td>
                    <input type="number" step="0.01" name="wtc_point_value" value="1" class="regular-text">
                    <p class="description">Müşteri 1 Puan harcadığında kaç TL indirim kazanır? (Örn: 1 Puan = 1 TL, 1 Puan = 0,5 TL)</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Kazanım Oranı (%)</th>
                <td>
                    <input type="number" step="1" name="wtc_point_earn_rate" value="1" class="regular-text">
                    <p class="description">Sipariş toplamının yüzde kaçı puan olarak geri döner? (Örn: %1, %2,5)</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Puan Geçerlilik Süresi (Gün)</th>
                <td>
                    <input type="number" step="1" name="wtc_point_expiry_days" value="365" class="regular-text">
                    <p class="description">Kazanılan puanlar kaç gün sonra silinsin? (0 = Süresiz)</p>
                </td>
                </tr>
            </tbody>
            </table>
        </div>
        <div class="wtc-settings-section">
            <h3>Bonus Puanlar</h3>
            <table class="form-table wtc-form-table">
            <tbody>
                <tr>
                <th scope="row">Hoşgeldin Bonusu</th>
                <td>
                    <input type="number" step="1" name="wtc_signup_bonus" value="0" class="regular-text">
                    <p class="description">Siteye yeni kayıt olan kullanıcılara verilecek hediye puan miktarı.</p>
                </td>
                </tr>
                <tr>
                <th scope="row">İlk Sipariş Bonusu</th>
                <td>
                    <input type="number" step="1" name="wtc_first_order_bonus" value="0" class="regular-text">
                    <p class="description">İlk alışverişini başarıyla tamamlayanlara verilecek ekstra puan.</p>
                </td>
                </tr>
            </tbody>
            </table>
        </div>
        <div class="wtc-settings-section">
            <h3>Harcama Kısıtlamaları</h3>
            <table class="form-table wtc-form-table">
            <tbody>
                <tr>
                <th scope="row">Minimum Sepet Tutarı</th>
                <td>
                    <input type="number" step="1" name="wtc_min_cart_total" value="0" class="regular-text">
                    <p class="description">Puan harcayabilmek için sepet tutarı en az bu kadar olmalıdır.</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Minimum Puan Limiti</th>
                <td>
                    <input type="number" step="1" name="wtc_min_point_limit" value="0" class="regular-text">
                    <p class="description">Kullanıcının hesabında en az bu kadar puan varsa harcama yapabilir.</p>
                </td>
                </tr>
                <tr>
                <th scope="row">Sipariş Başına Maksimum Puan</th>
                <td>
                    <input type="number" step="1" name="wtc_max_point_limit" value="0" class="regular-text">
                    <p class="description">Bir siparişte kullanılabilecek maksimum puan miktarı. (0 = Sınırsız)</p>
                </td>
                </tr>
            </tbody>
            </table>
        </div>
        <p class="submit">
            <button type="submit" class="button button-primary">Ayarları Kaydet</button>
        </p>
        </form>
        <?php
    }
    private function render_manual_transaction_page() { ?>
        <div class="wtc-settings-section">
        <h3 style="margin-top:0; margin-bottom:15px;">Manuel Puan Ekle / Çıkar</h3>
        <p style="margin-bottom:20px; color:#666;">Bu alandan bir kullanıcıya manuel olarak puan ekleyebilir veya bakiyesinden puan silebilirsiniz.</p>
        <div class="wtc-form-grid">
            <div class="wtc-form-row">
            <div class="wtc-form-group wtc-col-grow">
                <label for="user_identifier">Kullanıcı ID veya E-posta</label>
                <input type="text" id="user_identifier" name="user_identifier" class="regular-text full-width" placeholder="ID veya E-posta girin" value="" required="">
            </div>
            <div class="wtc-form-group wtc-col-fixed-sm">
                <label for="type">İşlem</label>
                <select id="type" name="type" class="full-width">
                <option value="add">Ekle (+)</option>
                <option value="remove">Çıkar (-)</option>
                </select>
            </div>
            </div>
            <div class="wtc-form-row">
            <div class="wtc-form-group wtc-col-fixed-sm">
                <label for="points">Puan Miktarı</label>
                <input type="number" id="points" name="points" step="0.01" min="0.01" class="regular-text full-width" required="">
            </div>
            <div class="wtc-form-group wtc-col-grow">
                <label for="description">Açıklama</label>
                <input type="text" id="description" name="description" class="regular-text full-width" placeholder="Örn: Telafi, Hediye..." required="">
            </div>
            </div>
            <div class="wtc-form-actions">
            <button type="submit" class="button button-primary button-large">İşlemi Uygula</button>
            </div>
    </div>
        </div>
        <?php
    }
    private function render_logs() { ?>
            <div class="wtc-logs-filter">
            <h3>Puan Kayıtlarını Filtrele</h3>
            <form method="get" action="">
                <input type="hidden" name="page" value="wtc-points" wfd-id="id0">
                <input type="hidden" name="tab" value="logs" wfd-id="id1">
                <table class="form-table">
                    <tbody><tr>
                        <th>Başlangıç Tarihi</th>
                        <td><input type="date" name="points_start_date" value="" wfd-id="id2"></td>
                        <th>Bitiş Tarihi</th>
                        <td><input type="date" name="points_end_date" value="" wfd-id="id3"></td>
                    </tr>
                    <tr>
                        <th>Sipariş ID</th>
                        <td><input type="number" name="points_order_id" value="" wfd-id="id4"></td>
                        <th>Kullanıcı ID/E-Posta</th>
                        <td><input type="text" name="points_user_id" wfd-id="id5"></td>
                    </tr>
                    <tr>
                        <th>İşlem Tipi</th>
                        <td>
                            <select name="points_type">
                                <option value="">Tüm Tipler</option>
                                <option value="manual">Manuel İşlem</option>
                                <option value="earn">Sipariş Kazancı</option>
                                <option value="spend">Harcama</option>
                                <option value="signup_bonus">Hoşgeldin Bonusu</option>
                                <option value="first_order_bonus">İlk Sipariş Bonusu</option>
                                <option value="refund_earn">İade (Geri Alım)</option>
                                <option value="refund_spend">İade (Puan İadesi)</option>
                            </select>
                        </td>
                    </tr>
                </tbody></table>
                <p class="submit">
                    <input type="submit" class="button button-primary" value="Filtrele" wfd-id="id6">
                    <a href="/wp-admin/admin.php?page=wtc-points&amp;tab=logs" class="button button-secondary">Filtreleri Temizle</a>
                </p>
            </form>
        </div>
        <h3>Puan Kayıtları</h3>
        <form method="post" id="wtc-points-logs-form">
                <div class="tablenav top">
                    <div class="alignleft actions bulkactions">
                        <select name="action" id="bulk-action-selector-top">
                            <option value="-1">Toplu İşlemler</option>
                            <option value="delete">Sil</option>
                        </select>
                        <input type="submit" id="doaction" class="button action" value="Uygula" wfd-id="id7">
                    </div>
                </div>
                <table class="widefat striped">
                    <thead>
                        <tr>
                            <td id="cb" class="manage-column column-cb check-column"><input id="cb-select-all-1" type="checkbox" wfd-id="id8"></td>
                            <th>Tarih</th>
                            <th>Kullanıcı</th>
                            <th>İşlem Tipi</th>
                            <th>Açıklama</th>
                            <th>Puan</th>
                            <th>TL Değeri</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr><td colspan="7">Kayıt bulunamadı.</td></tr>
                    </tbody>
                </table>
            </form>
        <?php
    }
    private function render_users_page() { ?>
        <div class="wtc-logs-filter">
                <h3>Kullanıcıları Filtrele</h3>
                <form method="get" action="">
                <input type="hidden" name="page" value="wtc-points">
                <input type="hidden" name="tab" value="users">
                <table class="form-table">
                    <tbody>
                    <tr>
                        <th>ID / E-posta / İsim</th>
                        <td>
                        <input type="text" name="s" value="" placeholder="Ara...">
                        </td>
                        <th>Min. Puan</th>
                        <td>
                        <input type="number" step="0.01" name="min_points" value="">
                        </td>
                    </tr>
                    </tbody>
                </table>
                <p class="submit">
                    <input type="submit" class="button button-primary" value="Filtrele">
                    <a href="/wp-admin/admin.php?page=wtc-points&amp;tab=users" class="button button-secondary">Filtreleri Temizle</a>
                </p>
                </form>
            </div>
            <h3>Kullanıcı Bakiyeleri</h3>
            <table class="widefat striped">
                <thead>
                <tr>
                    <th scope="col" class="manage-column column-id sortable sorted desc">
                    <a href="/wp-admin/admin.php?page=wtc-points&amp;tab=users&amp;orderby=id&amp;order=asc&amp;paged=1">
                        <span>ID</span>
                        <span class="sorting-indicator"></span>
                    </a>
                    </th>
                    <th scope="col" class="manage-column column-name sortable desc">
                    <a href="/wp-admin/admin.php?page=wtc-points&amp;tab=users&amp;orderby=name&amp;order=asc&amp;paged=1">
                        <span>Kullanıcı</span>
                        <span class="sorting-indicator"></span>
                    </a>
                    </th>
                    <th scope="col" class="manage-column column-email sortable desc">
                    <a href="/wp-admin/admin.php?page=wtc-points&amp;tab=users&amp;orderby=email&amp;order=asc&amp;paged=1">
                        <span>E-posta</span>
                        <span class="sorting-indicator"></span>
                    </a>
                    </th>
                    <th scope="col" class="manage-column column-points sortable desc">
                    <a href="/wp-admin/admin.php?page=wtc-points&amp;tab=users&amp;orderby=points&amp;order=asc&amp;paged=1">
                        <span>Puan</span>
                        <span class="sorting-indicator"></span>
                    </a>
                    </th>
                    <th>İşlemler</th>
                </tr>
                </thead>
                <tbody></tbody>
            </table>
            </div>
        <?php
    }
}