<?php
// export_data.php - Export Data to CSV
// File location: /bidwheels.net/admin/export_data.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';

requireAdmin();

$export_type = isset($_GET['type']) ? $_GET['type'] : 'payments';
$start_date = isset($_GET['start_date']) ? sanitize($_GET['start_date']) : date('Y-m-d', strtotime('-30 days'));
$end_date = isset($_GET['end_date']) ? sanitize($_GET['end_date']) : date('Y-m-d');

// Set headers for CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=' . $export_type . '_' . date('Y-m-d') . '.csv');

// Create output stream
$output = fopen('php://output', 'w');

// Add UTF-8 BOM for Excel compatibility
fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));

if ($export_type == 'payments') {
    // Export payments data
    fputcsv($output, ['ID', 'User Name', 'User Email', 'Product', 'Payment Method', 'Payment Data', 'Phone', 'Email', 'Date']);
    
    $payments = fetchAll("
        SELECT p.*, u.name as user_name, u.email as user_email, pr.name as product_name
        FROM payments p
        LEFT JOIN users u ON p.user_id = u.id
        LEFT JOIN products pr ON p.product_id = pr.id
        WHERE DATE(p.created_at) BETWEEN '$start_date' AND '$end_date'
        ORDER BY p.created_at DESC
    ");
    
    foreach ($payments as $payment) {
        fputcsv($output, [
            $payment['id'],
            $payment['user_name'],
            $payment['user_email'],
            $payment['product_name'],
            $payment['payment_method'],
            $payment['payment_data'],
            $payment['phone'],
            $payment['email'],
            $payment['created_at']
        ]);
    }
    
} elseif ($export_type == 'bids') {
    // Export bids data
    fputcsv($output, ['ID', 'Product', 'Bidder', 'Amount', 'Type', 'Time']);
    
    $bids = fetchAll("
        SELECT b.*, p.name as product_name, u.name as user_name
        FROM bids b
        LEFT JOIN products p ON b.product_id = p.id
        LEFT JOIN users u ON b.user_id = u.id
        WHERE DATE(b.created_at) BETWEEN '$start_date' AND '$end_date'
        ORDER BY b.created_at DESC
    ");
    
    foreach ($bids as $bid) {
        fputcsv($output, [
            $bid['id'],
            $bid['product_name'],
            $bid['user_name'] ?? 'System',
            $bid['amount'],
            $bid['is_system_bid'] ? 'System' : 'User',
            $bid['created_at']
        ]);
    }
    
} elseif ($export_type == 'users') {
    // Export users data
    fputcsv($output, ['ID', 'Name', 'Email', 'Phone', 'Address', 'Type', 'Registered']);
    
    $users = fetchAll("
        SELECT * FROM users 
        WHERE is_dummy = 0
        ORDER BY created_at DESC
    ");
    
    foreach ($users as $user) {
        fputcsv($output, [
            $user['id'],
            $user['name'],
            $user['email'],
            $user['phone'],
            $user['address'],
            $user['role'],
            $user['created_at']
        ]);
    }
    
} elseif ($export_type == 'products') {
    // Export products data
    fputcsv($output, ['ID', 'Name', 'Category', 'Starting Price', 'Current Bid', 'Total Bids', 'Status', 'End Time', 'Created']);
    
    $products = fetchAll("
        SELECT p.*, c.name as category_name,
               (SELECT COUNT(*) FROM bids WHERE product_id = p.id) as bid_count
        FROM products p
        LEFT JOIN categories c ON p.category_id = c.id
        ORDER BY p.created_at DESC
    ");
    
    foreach ($products as $product) {
        fputcsv($output, [
            $product['id'],
            $product['name'],
            $product['category_name'],
            $product['starting_price'],
            $product['current_highest_bid'],
            $product['bid_count'],
            $product['is_reserved'] ? 'Reserved' : 'Active',
            $product['end_time'],
            $product['created_at']
        ]);
    }
}

fclose($output);
exit();
?>