<?php
require_once '../config/config.php';
require_once '../includes/auth.php';

requireCompanyLogin();
ensureFeatureAndPaymentTables();

$companyId = (int)($_SESSION['company_id'] ?? 0);
$error = '';

$type = $_GET['type'] ?? ($_POST['type'] ?? 'other');
if (!in_array($type, ['gold', 'featured_service', 'other'], true)) {
    $type = 'other';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $csrf = $_POST['csrf_token'] ?? '';
    $amount = (float)($_POST['amount'] ?? 0);
    $sender_name = trim($_POST['sender_name'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $type = $_POST['type'] ?? 'other';
    if (!in_array($type, ['gold', 'featured_service', 'other'], true)) {
        $type = 'other';
    }

    if (!verifyCSRFToken($csrf)) {
        $error = 'Geçersiz istek.';
    } elseif ($amount <= 0) {
        $error = 'Tutar giriniz.';
    } elseif ($sender_name === '') {
        $error = 'Gönderen adı gereklidir.';
    } else {
        try {
            $stmt = $pdo->prepare("
                INSERT INTO payment_notifications (company_id, type, amount, sender_name, description, status)
                VALUES (?, ?, ?, ?, ?, 'pending')
            ");
            $stmt->execute([$companyId, $type, $amount, $sender_name, $description]);

            setFlashMessage('success', 'Ödeme bildiriminiz alındı. Admin kontrol ettikten sonra işleme alınacaktır.');
            redirect('payment-notice.php');
        } catch (PDOException $e) {
            $error = 'Ödeme bildirimi kaydedilirken hata oluştu.';
        }
    }
}

$page_title = 'Ödeme Bildirimi';
include 'includes/header.php';
?>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Ödeme Bildirimi</h5>
            </div>
            <div class="card-body">
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?= e($error) ?></div>
                <?php endif; ?>

                <form method="POST">
                    <input type="hidden" name="csrf_token" value="<?= e(generateCSRFToken()) ?>">

                    <div class="mb-3">
                        <label class="form-label">Bildirim Tipi</label>
                        <select class="form-select" name="type">
                            <option value="featured_service" <?= $type === 'featured_service' ? 'selected' : '' ?>>İlan Yayınlama</option>
                            <option value="gold" <?= $type === 'gold' ? 'selected' : '' ?>>Gold Paket</option>
                            <option value="other" <?= $type === 'other' ? 'selected' : '' ?>>Diğer</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Ödenen Tutar (TL)</label>
                        <input type="number" step="0.01" class="form-control" name="amount" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Gönderen Adı</label>
                        <input type="text" class="form-control" name="sender_name" required placeholder="Ödemeyi yapan kişi">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Açıklama</label>
                        <textarea class="form-control" name="description" rows="4" placeholder="Havale açıklaması / notunuz"></textarea>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-paper-plane me-2"></i>Gönder
                    </button>
                </form>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Kısa Yollar</h5>
            </div>
            <div class="card-body">
                <a href="<?= SITE_URL ?>/company/paid-services.php" class="btn btn-outline-primary w-100 mb-2">Ücretli Hizmetler</a>
                <a href="<?= SITE_URL ?>/company/services/index.php" class="btn btn-outline-secondary w-100">Hizmetlerim</a>
            </div>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>


