// admin.js - Admin Panel JavaScript
// File location: /bidwheels.net/assets/js/admin.js

$(document).ready(function() {
    
    // Confirm delete actions
    $('.delete-confirm').click(function(e) {
        if (!confirm('Are you sure you want to delete this item? This action cannot be undone.')) {
            e.preventDefault();
            return false;
        }
    });
    
    // Auto-hide alerts after 5 seconds
    setTimeout(function() {
        $('.alert').fadeOut('slow');
    }, 5000);
    
    // Image preview for featured image
    $('#featured_image').change(function() {
        const file = this.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function(e) {
                $('#featured_preview').attr('src', e.target.result).show();
            }
            reader.readAsDataURL(file);
        }
    });
    
    // Multiple image gallery preview
    $('#gallery_images').change(function() {
        const files = this.files;
        const previewContainer = $('#gallery_preview_container');
        previewContainer.empty();
        
        for (let i = 0; i < files.length; i++) {
            const reader = new FileReader();
            reader.onload = function(e) {
                previewContainer.append(`
                    <div class="gallery-preview-item">
                        <img src="${e.target.result}" style="width: 80px; height: 80px; object-fit: cover; margin: 5px; border-radius: 5px;">
                    </div>
                `);
            }
            reader.readAsDataURL(files[i]);
        }
    });
    
    // Toggle product status (reserve/unreserve)
    $('.toggle-reserve').click(function() {
        const productId = $(this).data('id');
        const currentStatus = $(this).data('status');
        const action = currentStatus == 1 ? 'unreserve' : 'reserve';
        
        if (confirm(`Are you sure you want to ${action} this product?`)) {
            window.location.href = `products.php?${action}=${productId}`;
        }
    });
    
    // Auto-bid script confirmation
    $('#run_auto_bid').click(function(e) {
        if (!confirm('WARNING: This will place automatic bids on ALL active listings. This action cannot be undone. Continue?')) {
            e.preventDefault();
            return false;
        }
    });
    
    // Bulk action selector
    $('#select_all').change(function() {
        $('.select-item').prop('checked', $(this).prop('checked'));
    });
    
    // Filter form auto-submit
    $('#filter_type, #filter_status').change(function() {
        $('#filter_form').submit();
    });
    
    // Date range picker initialization
    if ($('#start_date').length && $('#end_date').length) {
        $('#start_date').change(function() {
            $('#end_date').attr('min', $(this).val());
        });
    }
    
    // Export dropdown
    $('#export_type').change(function() {
        const type = $(this).val();
        const start = $('#start_date').val();
        const end = $('#end_date').val();
        if (type && start && end) {
            window.location.href = `export_data.php?type=${type}&start_date=${start}&end_date=${end}`;
        }
    });
    
    // Rich text editor for description (simple)
    if ($('#editor').length) {
        $('#editor').attr('placeholder', 'Write detailed description here...');
    }
    
    // Dashboard charts (using Chart.js if available)
    if (typeof Chart !== 'undefined') {
        // Bid statistics chart
        if ($('#bidsChart').length) {
            const ctx = document.getElementById('bidsChart').getContext('2d');
            new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: ['Real Bids', 'System Bids'],
                    datasets: [{
                        label: 'Bid Count',
                        data: [realBidsCount, systemBidsCount],
                        backgroundColor: ['#28a745', '#ffc107']
                    }]
                }
            });
        }
    }
    
    // Sidebar active link highlighting
    const currentPath = window.location.pathname;
    $('.admin-sidebar nav ul li a').each(function() {
        if ($(this).attr('href') === currentPath.split('/').pop()) {
            $(this).parent().addClass('active');
        }
    });
});