<?php
namespace WTC_CHECKOUT\Modules\Refund\Admin;
defined('ABSPATH') || exit;
class RefundSettings {
    private $tabs = [];
    private $active_tab = '';
    private $nonce_name = 'wtc_refund_settings_nonce';
    public function __construct() {
        $this->tabs = [
            'settings' => __('Ayarlar', 'wtc-checkout'),
            'requests' => __('İade Talepleri', 'wtc-checkout'),
        ];
    }
    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_refund_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__('İade Yönetimi', 'wtc-checkout') . '</h1>';
        $this->render_tabs();
        echo '<div class="wtc-card">';
        switch ($this->active_tab) {
            case 'settings':
                $this->render_refund_settings();
                break;
            case 'requests':
                $this->render_refund_requests_tab();
                break;
        }
        echo '</div>'; 
        echo '</div>'; 
    }
    private function render_tabs() {
        echo '<h2 class="wtc-nav-tab-wrapper nav-tab-wrapper">';
        $nonce = wp_create_nonce('wtc_refund_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=wtc-refund&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://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>';
        echo '</h2>';
    }
    private function render_refund_settings() {
        ?>
        <form method="post" action="">
            <input type="hidden" id="wtc_nonce" name="wtc_nonce" value="baabacc963">
            <input type="hidden" name="_wp_http_referer" value="/wp-admin/admin.php?page=wtc-refund&amp;tab=settings">
            <input type="hidden" name="wtc_refund_settings_submitted" value="1">
            <div class="wtc-settings-section">
                <table class="form-table wtc-form-table">
                <tbody>
                    <tr>
                    <th scope="row">İade Sistemi</th>
                    <td>
                        <div class="wtc-toggle-wrapper">
                        <input type="checkbox" id="sms_enabled" name="wtc_refund_enabled" value="1" class="wtc-toggle-input">
                        <label for="sms_enabled" class="wtc-toggle-label"></label>
                        <span class="toggle-label">İade sistemini etkinleştir</span>
                        </div>
                        <p class="description">İade sistemi aktif olduğunda müşteriler iade talebi oluşturabilir.</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">İade Edilebilir Sipariş Durumları</th>
                    <td>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_order_statuses[]" value="wc-pending"> Ödeme bekliyor </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_order_statuses[]" value="wc-processing"> Hazırlanıyor </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_order_statuses[]" value="wc-on-hold"> Beklemede </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_order_statuses[]" value="wc-shipped"> Kargolandı </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_order_statuses[]" value="wc-completed" checked="checked"> Tamamlandı </label>
                        <p class="description">Müşterilerin hangi sipariş durumlarında iade talebi oluşturabileceğini seçin.</p>
                    </td>
                    </tr>
                    <tr>
                    <th scope="row">İade Talep Süresi (Gün)</th>
                    <td>
                        <input type="number" name="wtc_refund_request_period" min="1" max="365" value="14" class="regular-text">
                        <p class="description">Sipariş tamamlandıktan sonra kaç gün içinde iade talebi oluşturulabilir.</p>
                    </td>
                    </tr>
                    <tr>
                    <th scope="row">Kargo Ücretini İade Tutarına Dahil Et</th>
                    <td>
                        <div class="wtc-toggle-wrapper">
                        <input type="checkbox" id="include_shipping" name="wtc_refund_include_shipping" value="yes" class="wtc-toggle-input">
                        <label for="include_shipping" class="wtc-toggle-label"></label>
                        <span class="toggle-label">Kargo ücretini iade tutarına dahil et.</span>
                        </div>
                    </td>
                    </tr>
                </tbody>
                </table>
            </div>
            <div class="wtc-settings-section">
                <h3>Görünüm Ayarları</h3>
                <table class="form-table wtc-form-table">
                <tbody>
                    <tr>
                    <th scope="row">İade Butonu Görünüm Yerleri</th>
                    <td>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_button_locations[]" value="order_list" id="wtc_refund_order_list"> Siparişler Sayfası </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_button_locations[]" value="order_details" id="wtc_refund_order_details" checked="checked"> Sipariş Detay Sayfası </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_button_locations[]" value="thank_you" id="wtc_refund_thank_you"> Teşekkür Sayfası </label>
                        <p class="description">İade talep butonunun görüneceği sayfaları seçin.</p>
                    </td>
                    </tr>
                    <tr>
                    <th scope="row">İade Talebi İptal</th>
                    <td>
                        <div class="wtc-toggle-wrapper">
                        <input type="checkbox" id="allow_cancel_refund" name="wtc_refund_allow_cancel" value="yes" checked="checked" class="wtc-toggle-input">
                        <label for="allow_cancel_refund" class="wtc-toggle-label"></label>
                        <span class="toggle-label">Müşterilerin iade taleplerini iptal etmesine izin ver</span>
                        </div>
                        <p class="description">Bu ayar aktifse, müşteriler oluşturdukları iade taleplerini iptal edebilirler.</p>
                    </td>
                    </tr>
                </tbody>
                </table>
            </div>
            <div class="wtc-settings-section">
                <h3>Resim Yükleme Ayarları</h3>
                <table class="form-table wtc-form-table">
                <tbody>
                    <tr>
                    <th scope="row">Resim Yüklemeye İzin Ver</th>
                    <td>
                        <div class="wtc-toggle-wrapper">
                        <input type="checkbox" id="allow_images" name="wtc_refund_allow_images" value="yes" checked="checked" class="wtc-toggle-input">
                        <label for="allow_images" class="wtc-toggle-label"></label>
                        <span class="toggle-label">Müşterilerin iade talebiyle birlikte resim yüklemesine izin ver.</span>
                        </div>
                    </td>
                    </tr>
                    <tr>
                    <th scope="row">İzin Verilen Dosya Formatları</th>
                    <td>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_allowed_file_types[]" value="jpg" checked="checked"> JPG/JPEG </label>
                        <label class="wtc-checkbox-label">
                        <input type="checkbox" name="wtc_refund_allowed_file_types[]" value="png" checked="checked"> PNG </label>
                        <p class="description">İzin verilen dosya formatlarını seçin.</p>
                    </td>
                    </tr>
                    <tr>
                    <th scope="row">Maksimum Dosya Sayısı</th>
                    <td>
                        <input type="number" name="wtc_refund_max_files" min="1" max="10" value="3" class="regular-text">
                        <p class="description">Bir iade talebi için yüklenebilecek maksimum dosya sayısı.</p>
                    </td>
                    </tr>
                    <tr>
                    <th scope="row">Maksimum Dosya Boyutu (MB)</th>
                    <td>
                        <input type="number" name="wtc_refund_max_file_size" min="1" max="10" value="2" class="regular-text">
                        <p class="description">Yüklenebilecek bir dosyanın maksimum boyutu (MB cinsinden).</p>
                    </td>
                    </tr>
                </tbody>
                </table>
            </div>
            <div class="wtc-settings-section">
                <h3>İade Tanımlamaları</h3>
                <div class="wtc-refund-reasons">
                <table class="form-table wtc-form-table">
                    <tbody>
                    <tr>
                        <th scope="row">İade Sebepleri</th>
                        <td>
                        <div class="wtc-refund-reason-item">
                            <input type="text" name="wtc_refund_reasons[0][reason]" value="Ürün hasarlı geldi" class="regular-text">
                            <label class="wtc-toggle-switch">
                            <input type="checkbox" name="wtc_refund_reasons[0][active]" value="yes" checked="checked">
                            <span class="slider"></span>
                            </label>
                            <button type="button" class="button wtc-remove-reason">Sil</button>
                        </div>
                        <div class="wtc-refund-reason-item">
                            <input type="text" name="wtc_refund_reasons[1][reason]" value="Ürün açıklamadan farklı" class="regular-text">
                            <label class="wtc-toggle-switch">
                            <input type="checkbox" name="wtc_refund_reasons[1][active]" value="yes" checked="checked">
                            <span class="slider"></span>
                            </label>
                            <button type="button" class="button wtc-remove-reason">Sil</button>
                        </div>
                        <div class="wtc-refund-reason-item">
                            <input type="text" name="wtc_refund_reasons[2][reason]" value="Yanlış ürün gönderildi" class="regular-text">
                            <label class="wtc-toggle-switch">
                            <input type="checkbox" name="wtc_refund_reasons[2][active]" value="yes" checked="checked">
                            <span class="slider"></span>
                            </label>
                            <button type="button" class="button wtc-remove-reason">Sil</button>
                        </div>
                        <div class="wtc-refund-reason-item">
                            <input type="text" name="wtc_refund_reasons[3][reason]" value="Ürün artık istenmiyor" class="regular-text">
                            <label class="wtc-toggle-switch">
                            <input type="checkbox" name="wtc_refund_reasons[3][active]" value="yes" checked="checked">
                            <span class="slider"></span>
                            </label>
                            <button type="button" class="button wtc-remove-reason">Sil</button>
                        </div>
                        <div class="wtc-refund-reason-item">
                            <input type="text" name="wtc_refund_reasons[4][reason]" value="Diğer" class="regular-text">
                            <label class="wtc-toggle-switch">
                            <input type="checkbox" name="wtc_refund_reasons[4][active]" value="yes" checked="checked">
                            <span class="slider"></span>
                            </label>
                            <button type="button" class="button wtc-remove-reason">Sil</button>
                        </div>
                        <div class="wtc-refund-reason-template" style="display: none;">
                            <input type="text" name="wtc_refund_reasons[{index}][reason]" value="" class="regular-text">
                            <label class="wtc-toggle-switch">
                            <input type="checkbox" name="wtc_refund_reasons[{index}][active]" value="yes" checked="">
                            <span class="slider"></span>
                            </label>
                            <button type="button" class="button wtc-remove-reason">Sil</button>
                        </div>
                        <button type="button" class="button wtc-add-reason">Yeni Sebep Ekle</button>
                        <p class="description">Müşterilerin iade talebi oluştururken seçebileceği sebepleri yönetin.</p>
                        </td>
                    </tr>
                    </tbody>
                </table>
                </div>
            </div>
            <div class="wtc-settings-section">
                <table class="form-table wtc-form-table">
                <tbody>
                    <tr>
                    <th scope="row">İade Şartları</th>
                    <td>
                        <textarea name="wtc_refund_terms" rows="10" cols="50" class="large-text">Lütfen iade koşullarımızı dikkatle okuyun: 
            1. Ürünler satın alma tarihinden itibaren 14 gün içinde iade edilebilir.
            2. İade edilecek ürünler kullanılmamış, hasarsız ve orijinal ambalajında olmalıdır.
            3. İade işlemi onaylandıktan sonra, ödemeniz 5-10 iş günü içinde iade edilecektir.</textarea>
                        <p class="description">İade talebi oluşturulurken müşterilere gösterilecek şartlar.</p>
                    </td>
                    </tr>
                </tbody>
                </table>
            </div>
            <p class="submit">
                <input type="submit" name="submit" id="submit" class="button button-primary" value="Ayarları Kaydet">
            </p>
            </form>
        <?php
    }
    private function render_refund_requests_tab() {
        $this->render_refund_requests_filter_form();
    }
    private function render_refund_requests_filter_form() {
        ?>
        <div class="wtc-refund-logs-filter">
            <h3>İade Taleplerini Filtrele</h3>
            <form method="get" action="">
                <input type="hidden" name="page" value="wtc-refund">
                <input type="hidden" name="tab" value="requests">
                <table class="form-table">
                <tbody>
                    <tr>
                    <th>Başlangıç Tarihi</th>
                    <td>
                        <input type="date" name="start_date" value="">
                    </td>
                    <th>Bitiş Tarihi</th>
                    <td>
                        <input type="date" name="end_date" value="">
                    </td>
                    </tr>
                    <tr>
                    <th>Sipariş ID</th>
                    <td>
                        <input type="text" name="s" value="">
                    </td>
                    <th>Sipariş Durumu</th>
                    <td>
                        <select name="order_status">
                        <option value="" selected="selected"> Tüm Sipariş Durumları </option>
                        <option value="pending"> Ödeme bekliyor </option>
                        <option value="processing"> Hazırlanıyor </option>
                        <option value="on-hold"> Beklemede </option>
                        <option value="shipped"> Kargolandı </option>
                        <option value="completed"> Tamamlandı </option>
                        <option value="cancelled"> İptal edildi </option>
                        <option value="refunded"> İade edildi </option>
                        <option value="failed"> Başarısız </option>
                        <option value="checkout-draft"> Taslak </option>
                        <option value="refund-requested"> İade Talebi Oluşturuldu </option>
                        <option value="refund-rejected"> İade Reddedildi </option>
                        <option value="refund-cancelled"> İade Talebi İptal Edildi </option>
                        </select>
                    </td>
                    </tr>
                </tbody>
                </table>
                <p class="submit">
                <input type="submit" class="button button-primary" value="Filtrele">
                <a href="admin.php?page=wtc-refund&amp;tab=requests" class="button button-secondary">Filtreleri Temizle</a>
                </p>
            </form>
            </div>
            <div class="wtc-refund-logs">
            <h3>İade Talepleri</h3>
            <form method="post">
                <div class="tablenav top">
                <div class="tablenav-pages no-pages">
                    <span class="displaying-num">0 öge</span>
                    <span class="pagination-links">
                    <span class="tablenav-pages-navspan button disabled" aria-hidden="true">«</span>
                    <span class="tablenav-pages-navspan button disabled" aria-hidden="true">‹</span>
                    <span class="paging-input">
                        <label for="current-page-selector" class="screen-reader-text">Geçerli sayfa</label>
                        <input class="current-page" id="current-page-selector" type="text" name="paged" value="1" size="1" aria-describedby="table-paging">
                        <span class="tablenav-paging-text"> / <span class="total-pages">0</span>
                        </span>
                    </span>
                    <a class="next-page button" href="admin.php?page=wtc-refund&amp;tab=requests&amp;paged=0">
                        <span class="screen-reader-text">Sonraki sayfa</span>
                        <span aria-hidden="true">›</span>
                    </a>
                    <a class="last-page button" href="admin.php?page=wtc-refund&amp;tab=requests&amp;paged=0">
                        <span class="screen-reader-text">Son sayfa</span>
                        <span aria-hidden="true">»</span>
                    </a>
                    </span>
                </div>
                <br class="clear">
                </div>
                <table class="wp-list-table widefat fixed striped posts">
                <thead>
                    <input type="hidden" id="_wpnonce" name="_wpnonce" value="0bdd00c885">
                    <input type="hidden" name="_wp_http_referer" value="/wp-admin/admin.php?page=wtc-refund&amp;tab=requests">
                    <tr>
                    <td id="cb" class="manage-column column-cb check-column">
                        <input id="cb-select-all-1" type="checkbox">
                        <label for="cb-select-all-1">
                        <span class="screen-reader-text">Tümünü seç</span>
                        </label>
                    </td>
                    <th scope="col" id="order_id" class="manage-column column-order_id column-primary sortable desc">
                        <a href="admin.php?page=wtc-refund&amp;tab=requests&amp;orderby=order_id&amp;order=asc">
                        <span>Sipariş</span>
                        <span class="sorting-indicators">
                            <span class="sorting-indicator asc" aria-hidden="true"></span>
                            <span class="sorting-indicator desc" aria-hidden="true"></span>
                        </span>
                        <span class="screen-reader-text">Artan sıralama.</span>
                        </a>
                    </th>
                    <th scope="col" id="customer" class="manage-column column-customer">Müşteri</th>
                    <th scope="col" id="reason" class="manage-column column-reason">Sebep</th>
                    <th scope="col" id="amount" class="manage-column column-amount sortable desc">
                        <a href="admin.php?page=wtc-refund&amp;tab=requests&amp;orderby=amount&amp;order=asc">
                        <span>Tutar</span>
                        <span class="sorting-indicators">
                            <span class="sorting-indicator asc" aria-hidden="true"></span>
                            <span class="sorting-indicator desc" aria-hidden="true"></span>
                        </span>
                        <span class="screen-reader-text">Artan sıralama.</span>
                        </a>
                    </th>
                    <th scope="col" id="created_at" class="manage-column column-created_at sortable asc">
                        <a href="admin.php?page=wtc-refund&amp;tab=requests&amp;orderby=created_at&amp;order=desc">
                        <span>Oluşturulma Tarihi</span>
                        <span class="sorting-indicators">
                            <span class="sorting-indicator asc" aria-hidden="true"></span>
                            <span class="sorting-indicator desc" aria-hidden="true"></span>
                        </span>
                        <span class="screen-reader-text">Azalan sıralama.</span>
                        </a>
                    </th>
                    <th scope="col" id="status" class="manage-column column-status">Sipariş Durumu</th>
                    <th scope="col" id="refund_status" class="manage-column column-refund_status">İade Durumu</th>
                    <th scope="col" id="actions" class="manage-column column-actions">İşlemler</th>
                    <th scope="col" id="logs" class="manage-column column-logs">Log Verileri</th>
                    </tr>
                </thead>
                <tbody id="the-list">
                    <tr class="no-items">
                    <td class="colspanchange" colspan="10">İade talebi bulunamadı.</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                    <td class="manage-column column-cb check-column">
                        <input id="cb-select-all-2" type="checkbox">
                        <label for="cb-select-all-2">
                        <span class="screen-reader-text">Tümünü seç</span>
                        </label>
                    </td>
                    <th scope="col" class="manage-column column-order_id column-primary sortable desc">
                        <a href="admin.php?page=wtc-refund&amp;tab=requests&amp;orderby=order_id&amp;order=asc">
                        <span>Sipariş</span>
                        <span class="sorting-indicators">
                            <span class="sorting-indicator asc" aria-hidden="true"></span>
                            <span class="sorting-indicator desc" aria-hidden="true"></span>
                        </span>
                        <span class="screen-reader-text">Artan sıralama.</span>
                        </a>
                    </th>
                    <th scope="col" class="manage-column column-customer">Müşteri</th>
                    <th scope="col" class="manage-column column-reason">Sebep</th>
                    <th scope="col" class="manage-column column-amount sortable desc">
                        <a href="admin.php?page=wtc-refund&amp;tab=requests&amp;orderby=amount&amp;order=asc">
                        <span>Tutar</span>
                        <span class="sorting-indicators">
                            <span class="sorting-indicator asc" aria-hidden="true"></span>
                            <span class="sorting-indicator desc" aria-hidden="true"></span>
                        </span>
                        <span class="screen-reader-text">Artan sıralama.</span>
                        </a>
                    </th>
                    <th scope="col" class="manage-column column-created_at sortable asc">
                        <a href="admin.php?page=wtc-refund&amp;tab=requests&amp;orderby=created_at&amp;order=desc">
                        <span>Oluşturulma Tarihi</span>
                        <span class="sorting-indicators">
                            <span class="sorting-indicator asc" aria-hidden="true"></span>
                            <span class="sorting-indicator desc" aria-hidden="true"></span>
                        </span>
                        <span class="screen-reader-text">Azalan sıralama.</span>
                        </a>
                    </th>
                    <th scope="col" class="manage-column column-status">Sipariş Durumu</th>
                    <th scope="col" class="manage-column column-refund_status">İade Durumu</th>
                    <th scope="col" class="manage-column column-actions">İşlemler</th>
                    <th scope="col" class="manage-column column-logs">Log Verileri</th>
                    </tr>
                </tfoot>
                </table>
                <div class="tablenav bottom">
                <div class="tablenav-pages no-pages">
                    <span class="displaying-num">0 öge</span>
                    <span class="pagination-links">
                    <span class="tablenav-pages-navspan button disabled" aria-hidden="true">«</span>
                    <span class="tablenav-pages-navspan button disabled" aria-hidden="true">‹</span>
                    <span class="screen-reader-text">Geçerli sayfa</span>
                    <span id="table-paging" class="paging-input">
                        <span class="tablenav-paging-text">1 / <span class="total-pages">0</span>
                        </span>
                    </span>
                    <a class="next-page button" href="admin.php?page=wtc-refund&amp;tab=requests&amp;paged=0">
                        <span class="screen-reader-text">Sonraki sayfa</span>
                        <span aria-hidden="true">›</span>
                    </a>
                    <a class="last-page button" href="admin.php?page=wtc-refund&amp;tab=requests&amp;paged=0">
                        <span class="screen-reader-text">Son sayfa</span>
                        <span aria-hidden="true">»</span>
                    </a>
                    </span>
                </div>
                <br class="clear">
                </div>
            </form>
        </div>
        <?php
    }
} 