<?php
/*
|--------------------------------------------------------------------------
| RadyoHost.BiZ / Profesyonel Radyo Hosting ve Web Tasarım Hizmetleri
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| YENİ PAKET OLUŞTURMA
|--------------------------------------------------------------------------
| package-view.php
| Gold / Premium / VIP yeni paket oluşturma ekranı
| Developed By VoLKaN75
|--------------------------------------------------------------------------
*/

declare(strict_types=1);

$pageTitle = 'Yeni Paket Oluştur';
$pageDescription = 'Gold, Premium veya VIP paket oluşturun.';

require_once __DIR__ . '/includes/admin-layout.php';

$message = '';
$messageType = '';
$isCreated = false;

function package_view_e($value): string
{
    if (function_exists('admin_e')) {
        return admin_e($value);
    }

    return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $packageType  = trim((string)($_POST['package_type'] ?? ''));
    $title        = trim((string)($_POST['title'] ?? ''));
    $durationDays = (int)($_POST['duration_days'] ?? 30);
    $price        = (float)($_POST['price'] ?? 0);
    $description  = trim((string)($_POST['description'] ?? ''));
    $status       = trim((string)($_POST['status'] ?? 'active'));

    if (!in_array($packageType, ['gold', 'premium', 'vip'], true)) {
        $message = 'Geçerli bir paket türü seçiniz.';
        $messageType = 'error';
    } elseif ($title === '') {
        $message = 'Paket adı zorunludur.';
        $messageType = 'error';
    } else {
        try {
            $stmt = $pdo->prepare("
                INSERT INTO packages
                (package_type, title, duration_days, price, description, status, created_at)
                VALUES
                (:package_type, :title, :duration_days, :price, :description, :status, NOW())
            ");

            $stmt->execute([
                ':package_type'  => $packageType,
                ':title'         => $title,
                ':duration_days' => $durationDays > 0 ? $durationDays : 30,
                ':price'         => $price,
                ':description'   => $description,
                ':status'        => in_array($status, ['active', 'passive'], true) ? $status : 'active',
            ]);

            $message = 'Paket başarıyla oluşturuldu. 10 saniye sonra paket yönetimine yönlendiriliyorsunuz.';
            $messageType = 'success';
            $isCreated = true;
            $_POST = [];
        } catch (Throwable $e) {
            $message = 'Paket oluşturulurken sistem hatası oluştu.';
            $messageType = 'error';
        }
    }
}
?>

<?php if ($isCreated): ?>
    <meta http-equiv="refresh" content="10;url=packages.php">
<?php endif; ?>

<style>
.package-form-page{display:grid;grid-template-columns:1fr .38fr;gap:22px}
.package-card{padding:28px;border-radius:30px;background:rgba(255,255,255,.055);border:1px solid rgba(255,255,255,.09)}
.package-card h2{margin:0 0 8px;color:#f5b642;font-size:26px}
.package-card p{margin:0 0 22px;color:#9aa3b2;font-weight:700;line-height:1.6}
.form-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:18px}
.form-group.full{grid-column:1/-1}
.form-group label{display:block;margin-bottom:8px;color:#dce3ef;font-size:14px;font-weight:900}
.form-control{width:100%;height:52px;border:1px solid rgba(255,255,255,.12);border-radius:16px;background:rgba(0,0,0,.25);color:#fff;padding:0 15px;outline:none}
textarea.form-control{height:auto;min-height:150px;padding:15px;resize:vertical}
.form-control:focus{border-color:#f5b642;box-shadow:0 0 0 4px rgba(245,182,66,.12)}
.form-actions{display:flex;gap:12px;flex-wrap:wrap;margin-top:22px}
.form-alert{margin-bottom:18px;padding:15px 18px;border-radius:16px;font-weight:800}
.form-alert.error{background:rgba(185,0,0,.20);border:1px solid rgba(255,80,80,.30);color:#ffdede}
.form-alert.success{background:rgba(0,150,70,.20);border:1px solid rgba(0,255,120,.30);color:#d8ffe8}
.side-box{padding:22px;border-radius:26px;background:linear-gradient(135deg,rgba(245,182,66,.13),rgba(185,0,0,.10));border:1px solid rgba(255,255,255,.09);margin-bottom:16px}
.side-box strong{display:block;color:#f5b642;font-size:18px;margin-bottom:8px}
.side-box span{color:#cbd2df;font-size:13px;font-weight:700;line-height:1.7}
@media(max-width:1000px){.package-form-page{grid-template-columns:1fr}.form-grid{grid-template-columns:1fr}}
</style>

<section class="package-form-page">

    <div class="package-card">
        <h2>Yeni Paket Oluştur</h2>
        <p>Gold, Premium veya VIP paket oluşturun. Bu paketler daha sonra üyelere atanabilir.</p>

        <?php if ($message !== ''): ?>
            <div class="form-alert <?= package_view_e($messageType) ?>">
                <?= package_view_e($message) ?>
            </div>
        <?php endif; ?>

        <?php if (!$isCreated): ?>
            <form method="post">
                <div class="form-grid">

                    <div class="form-group">
                        <label>Paket Türü</label>
                        <select name="package_type" class="form-control" required>
                            <option value="">Seçiniz</option>
                            <option value="gold" <?= (($_POST['package_type'] ?? '') === 'gold') ? 'selected' : '' ?>>Gold</option>
                            <option value="premium" <?= (($_POST['package_type'] ?? '') === 'premium') ? 'selected' : '' ?>>Premium</option>
                            <option value="vip" <?= (($_POST['package_type'] ?? '') === 'vip') ? 'selected' : '' ?>>VIP</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label>Paket Adı</label>
                        <input type="text" name="title" class="form-control" value="<?= package_view_e($_POST['title'] ?? '') ?>" required>
                    </div>

                    <div class="form-group">
                        <label>Süre / Gün</label>
                        <input type="number" name="duration_days" class="form-control" value="<?= package_view_e($_POST['duration_days'] ?? '30') ?>" min="1">
                    </div>

                    <div class="form-group">
                        <label>Fiyat</label>
                        <input type="number" step="0.01" name="price" class="form-control" value="<?= package_view_e($_POST['price'] ?? '0.00') ?>">
                    </div>

                    <div class="form-group">
                        <label>Durum</label>
                        <select name="status" class="form-control">
                            <option value="active" <?= (($_POST['status'] ?? 'active') === 'active') ? 'selected' : '' ?>>Aktif</option>
                            <option value="passive" <?= (($_POST['status'] ?? '') === 'passive') ? 'selected' : '' ?>>Pasif</option>
                        </select>
                    </div>

                    <div class="form-group full">
                        <label>Paket Açıklaması</label>
                        <textarea name="description" class="form-control"><?= package_view_e($_POST['description'] ?? '') ?></textarea>
                    </div>

                </div>

                <div class="form-actions">
                    <button type="submit" class="admin-btn">Paketi Oluştur</button>
                    <a href="packages.php" class="admin-btn admin-btn-dark">Listeye Dön</a>
                </div>
            </form>
        <?php else: ?>
            <a href="packages.php" class="admin-btn">Paket Yönetimine Dön</a>
        <?php endif; ?>
    </div>

    <aside>
        <div class="side-box">
            <strong>Gold Paket</strong>
            <span>Başlangıç ve orta seviye üyelik avantajları için kullanılır.</span>
        </div>

        <div class="side-box">
            <strong>Premium Paket</strong>
            <span>Daha gelişmiş özellik ve ayrıcalık isteyen üyeler için kullanılır.</span>
        </div>

        <div class="side-box">
            <strong>VIP Paket</strong>
            <span>En üst seviye üyelik ve özel kullanıcı tanımlamaları için kullanılır.</span>
        </div>
    </aside>

</section>

<?php require_once __DIR__ . '/includes/admin-footer.php'; ?>