<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel - Vector Shop</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    <header>
        <div class="container">
            <h1>Vector Shop - Admin Panel</h1>
            <nav>
                <a href="{{ url_for('index') }}">Ana Sayfa</a>
                <a href="{{ url_for('view_logs') }}">Loglar</a>
                <a href="{{ url_for('admin_logout') }}">Çıkış</a>
            </nav>
        </div>
    </header>

    <main class="container">
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                <div class="flash-messages">
                    {% for message in messages %}
                        <div class="alert">{{ message }}</div>
                    {% endfor %}
                </div>
            {% endif %}
        {% endwith %}

        <div class="admin-stats">
            <div class="stat-card">
                <h3>Toplam Gelir</h3>
                <p class="stat-value">${{ "%.2f"|format(total_revenue) }}</p>
            </div>
            <div class="stat-card">
                <h3>Toplam Sipariş</h3>
                <p class="stat-value">{{ total_orders }}</p>
            </div>
            <div class="stat-card">
                <h3>Aktif Ürünler</h3>
                <p class="stat-value">{{ products|length }}</p>
            </div>
            <div class="stat-card">
                <h3>Kuponlar</h3>
                <p class="stat-value">{{ coupons|length }}</p>
            </div>
        </div>

        <section class="admin-section">
            <h2>Ürün Ekle</h2>
            <form method="POST" action="{{ url_for('add_product') }}" class="admin-form">
                <div class="form-row">
                    <div class="form-group">
                        <label>Ürün Adı:</label>
                        <input type="text" name="name" required>
                    </div>
                    <div class="form-group">
                        <label>Fiyat ($):</label>
                        <input type="number" step="0.01" name="price" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>Açıklama:</label>
                    <textarea name="description" rows="4" required></textarea>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Paddle Product ID:</label>
                        <input type="text" name="paddle_product_id" required>
                    </div>
                    <div class="form-group">
                        <label>GitHub Release Tag:</label>
                        <input type="text" name="github_release_tag">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Dosya Parça Sayısı:</label>
                        <input type="number" name="file_parts_count" value="1">
                    </div>
                    <div class="form-group">
                        <label>Görsel URL:</label>
                        <input type="text" name="image_url">
                    </div>
                </div>
                
                <div class="form-group">
                    <label>
                        <input type="checkbox" name="active" checked>
                        Aktif
                    </label>
                </div>
                
                <button type="submit" class="btn btn-primary">Ürün Ekle</button>
            </form>
        </section>

        <section class="admin-section">
            <h2>CSV ile Toplu Ürün Ekle (veri.csv formatı)</h2>
            <form method="POST" action="{{ url_for('import_csv') }}" enctype="multipart/form-data" class="admin-form">
                <div class="form-group">
                    <label>CSV Dosyası:</label>
                    <input type="file" name="file" accept=".csv" required>
                    <small>Format (pipe | ile ayrılmış): id|title|description|price|image|category|tags|keywords|popular|permanentlink|customers</small>
                </div>
                <button type="submit" class="btn btn-primary">İçe Aktar</button>
            </form>
        </section>

        <section class="admin-section">
            <h2>Kupon Ekle</h2>
            <form method="POST" action="{{ url_for('add_coupon') }}" class="admin-form">
                <div class="form-row">
                    <div class="form-group">
                        <label>Kupon Kodu:</label>
                        <input type="text" name="code" required>
                    </div>
                    <div class="form-group">
                        <label>İndirim (%):</label>
                        <input type="number" step="0.01" name="discount_percent" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>Maksimum Kullanım (Boş bırakılabilir):</label>
                    <input type="number" name="max_uses">
                </div>
                
                <button type="submit" class="btn btn-primary">Kupon Oluştur</button>
            </form>
        </section>

        <section class="admin-section">
            <h2>Ürünler</h2>
            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Ürün Adı</th>
                            <th>Fiyat</th>
                            <th>Paddle ID</th>
                            <th>Release Tag</th>
                            <th>Durum</th>
                            <th>İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for product in products %}
                        <tr>
                            <td>{{ product.id }}</td>
                            <td>{{ product.name }}</td>
                            <td>${{ "%.2f"|format(product.price) }}</td>
                            <td>{{ product.paddle_product_id }}</td>
                            <td>{{ product.github_release_tag or '-' }}</td>
                            <td>
                                {% if product.active %}
                                <span class="badge badge-success">Aktif</span>
                                {% else %}
                                <span class="badge badge-danger">Pasif</span>
                                {% endif %}
                            </td>
                            <td>
                                <form method="POST" action="{{ url_for('delete_product', product_id=product.id) }}" style="display:inline;">
                                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Emin misiniz?')">Sil</button>
                                </form>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </section>

        <section class="admin-section">
            <h2>Son Siparişler</h2>
            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>Sipariş ID</th>
                            <th>Ürün</th>
                            <th>Müşteri</th>
                            <th>Tutar</th>
                            <th>Durum</th>
                            <th>Teslimat</th>
                            <th>Tarih</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in orders %}
                        <tr>
                            <td>{{ order.order_id }}</td>
                            <td>{{ order.product.name if order.product else 'N/A' }}</td>
                            <td>{{ order.customer_email }}</td>
                            <td>${{ "%.2f"|format(order.amount) }}</td>
                            <td>
                                {% if order.status == 'completed' %}
                                <span class="badge badge-success">Tamamlandı</span>
                                {% else %}
                                <span class="badge badge-warning">{{ order.status }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if order.delivery_sent %}
                                <span class="badge badge-success">Gönderildi</span>
                                {% else %}
                                <span class="badge badge-danger">Bekliyor</span>
                                {% endif %}
                            </td>
                            <td>{{ order.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </section>

        <section class="admin-section">
            <h2>Kuponlar</h2>
            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>Kod</th>
                            <th>İndirim %</th>
                            <th>Kullanım</th>
                            <th>Max Kullanım</th>
                            <th>Durum</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for coupon in coupons %}
                        <tr>
                            <td><strong>{{ coupon.code }}</strong></td>
                            <td>{{ coupon.discount_percent }}%</td>
                            <td>{{ coupon.current_uses }}</td>
                            <td>{{ coupon.max_uses or '∞' }}</td>
                            <td>
                                {% if coupon.active and coupon.is_valid() %}
                                <span class="badge badge-success">Aktif</span>
                                {% else %}
                                <span class="badge badge-danger">Pasif</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </section>
    </main>

    <footer>
        <div class="container">
            <p>&copy; 2026 Vector Shop Admin Panel. Tüm hakları saklıdır.</p>
        </div>
    </footer>
</body>
</html>
