<?php if (!empty($rows['pFinalDocument'])): ?>
<div class="form-group row">
    <div class="col-6">
        <?php 
            $fdocument = $rows['pFinalDocument'];
            $file_ext = strtolower(pathinfo($fdocument, PATHINFO_EXTENSION)); 
                            
            $fdocument_url = "../upload/finalDoc/" . $fdocument; 
                            
            // Display the file based on its type
            if (in_array($file_ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
                // Display image
                echo '<img src="' . $fdocument_url . '" alt="Document Image" style="max-width: 100%; height: auto;">';
            } elseif ($file_ext == 'pdf') {
                // Display PDF in an iframe or object tag
                echo '<iframe src="' . $fdocument_url . '" width="100%" height="600px"></iframe>';
            } elseif (in_array($file_ext, ['doc', 'docx'])) {
                // Provide a link to download Word document
                echo '<a href="' . $fdocument_url . '" target="_blank">Download Word Document</a>';
            } elseif (in_array($file_ext, ['xls', 'xlsx'])) {
                // Provide a link to download Excel document
                echo '<a href="' . $fdocument_url . '" target="_blank">Download Excel Document</a>';
            } else {
                // Provide a download link for other file types
                echo '<a href="' . $fdocument_url . '" target="_blank">Download File</a>';
            }
        ?>
    </div>
    <div class="col-6">
        <label>Current Document:</label>
        <p><?php echo $rows['pFinalDocument']; ?></p>
        <button type="button" class="btn btn-success"
            onclick="downloadDocument('<?php echo $fdocument_url; ?>')">Download Document</button>
    </div>
</div>
<script>
function downloadDocument(url) {
    const link = document.createElement('a');
    link.href = url;
    link.download = url.split('/').pop(); // Get file name from URL
    link.target = '_blank';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
</script>
<?php endif; ?>