<?php

// upload images
function uploadImage($resizedImageBase64,$originalFileName)
{
   // Get the current date
    $currentDate = now();

    // Specify the storage path with the folder structure
    $storagePath = 'images/' . $currentDate->year . '/' . $currentDate->month . '/' . $currentDate->day . '/';

    // Create storage directory if not exists
    File::makeDirectory(public_path($storagePath), $mode = 0777, true, true);
 
    
    // Convert the base64 data URL to a file
    list($type, $data) = explode(';', $resizedImageBase64);
    list(, $data) = explode(',', $data);
    $decodedData = base64_decode($data);

    // Save the resized image to storage with the folder structure
    // Get the original file name
    
    $fileExtension = explode('/', $type)[1];
    
    $resizedImagePath = public_path($storagePath . $originalFileName);
    file_put_contents($resizedImagePath, $decodedData);

    // Optionally, update the database with information about the uploaded images
    // You may have a model representing images, and you can create new records in the database

    return response()->json(['message' => 'Image uploaded and resized successfully']);
}


/**
 * Display sort icon for a table column
 * Checks if the column is currently being sorted and displays the appropriate icon
 *
 * @param string $columnName The column name to check (e.g., 'email', 'name')
 * @param string|null $defaultColumn The default column to sort by when no sort_by is set (optional)
 * @return string HTML for the sort icon or empty string
 */
function renderSortIcon($columnName, $defaultColumn = null)
{
    $currentSortBy = request('sort_by');
    $currentSortOrder = request('sort_order');
    
    // Check if this column is being sorted
    $isActive = ($currentSortBy == $columnName) || 
                (!$currentSortBy && $defaultColumn && $columnName == $defaultColumn);
    
    if (!$isActive) {
        return '';
    }
    
    // Determine icon based on sort order
    // If no sort_order or desc, show down arrow; otherwise show up arrow
    if ($currentSortOrder == 'desc' || (!$currentSortOrder && $defaultColumn && $columnName == $defaultColumn)) {
        return '<i class="ri-arrow-down-line"></i>';
    } else {
        return '<i class="ri-arrow-up-line"></i>';
    }
}



