<?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>

        <form id="removeDocumentForm" method="POST">

            <input type="hidden" name="pid" id="pid" value="<?php echo $rows['pid']; ?>">

            <button type="button" class="btn btn-danger unlinkFileButton" data-pid="<?php echo $rows['pid']; ?>">Remove

                Document</button>

        </form>

        <script>

        $(document).ready(function() {

            // Use a class selector and find the pid dynamically

            $('.unlinkFileButton').on('click', function() {

                // Show confirmation dialog

                if (confirm('Are you sure you want to remove this document?')) {

                    // Get the product ID from the button's data attribute

                    var pid = $(this).data('pid');



                    // Make an AJAX POST request to the remove-document.php script

                    $.ajax({

                        url: 'remove-document.php', // PHP script that handles document removal

                        type: 'POST',

                        data: {

                            pid: pid

                        },

                        success: function(response) {

                            // On success, display the message and update UI

                            alert(response

                                .message

                            ); // Display success or error message



                            if (response.success) {

                                // Optional: Disable the button and/or update the UI to reflect document removal

                                $('[data-pid="' + pid + '"]').prop(

                                    'disabled', true).text(

                                    'Document Removed');

                            }

                        },

                        error: function(xhr, status, error) {

                            // Handle error case

                            alert('An error occurred: ' + xhr

                                .responseText);

                        }

                    });

                }

            });

        });

        </script>

    </div>

</div>

<?php endif; ?>



<form action="update-final-doc.php" method="POST" enctype="multipart/form-data">

    <input type="hidden" name="pid" id="final_doc_pid<?php echo $rows['pid'];?>" value="<?php echo $rows['pid']; ?>">



    <div class="form-group">

        <label for="docFile">Upload Document</label>

        <input type="file" name="docFile" id="docFile" class="form-control"

            <?php echo empty($rows['pFinalDocument']) ? 'required' : ''; ?>>

    </div>



    <div class="form-group">

        <label for="remarks">Remarks</label>

        <textarea name="remarks" id="remarks" class="form-control"

            rows="3"><?php if (!empty($rows['pFinalRemark'])){ echo $rows['pFinalRemark'];} else { echo "Enter remark..";}?></textarea>

    </div>



    <div class="form-group mt-2">

        <button type="submit" class="btn btn-primary">Submit</button>

    </div>

</form>