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

requireCompanyLogin();

$companyId = $_SESSION['company_id'];

// Mevcut resim sayısını kontrol et
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM company_images WHERE company_id = ?");
$stmt->execute([$companyId]);
$imageCount = $stmt->fetch()['count'];

if ($imageCount >= MAX_COMPANY_IMAGES) {
    setFlashMessage('error', 'Maksimum ' . MAX_COMPANY_IMAGES . ' resim yükleyebilirsiniz.');
    redirect('profile.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
    $upload = uploadImage($_FILES['image'], 'companies');
    
    if ($upload['success']) {
        // En yüksek sort_order'ı bul
        $stmt = $pdo->prepare("SELECT MAX(sort_order) as max_order FROM company_images WHERE company_id = ?");
        $stmt->execute([$companyId]);
        $maxOrder = $stmt->fetch()['max_order'] ?? 0;
        
        // Veritabanına kaydet
        $stmt = $pdo->prepare("
            INSERT INTO company_images (company_id, image_path, image_url, sort_order)
            VALUES (?, ?, ?, ?)
        ");
        $stmt->execute([$companyId, $upload['file_path'], $upload['url'], $maxOrder + 1]);
        
        setFlashMessage('success', 'Resim başarıyla yüklendi.');
    } else {
        setFlashMessage('error', $upload['message']);
    }
}

redirect('profile.php');





