<?php
$page_title = 'File Management';
require_once('includes/load.php');
page_require_level(2);

$files = find_all('files');

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file_upload'])) {
    $file_name = $_FILES['file_upload']['name'];

    $file_path = 'uploads/' . $file_name; // Lokasi penyimpanan file

    if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $file_path)) {
        $db->query("INSERT INTO files (name) VALUES ('$file_name')");
        $session->msg('s', 'File has been uploaded.');
        redirect('media.php');
    } else {
        $session->msg('d', 'Failed to upload file.');
        redirect('media.php');
    }
}
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-6">
        <?php echo display_msg($msg); ?>
    </div>

    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <span class="glyphicon glyphicon-file"></span>
                <span>Files</span>
                <div class="pull-right">
                    <form class="form-inline" action="media.php" method="POST" enctype="multipart/form-data">
                        <div class="form-group">
                            <div class="input-group">
                                <span class="input-group-btn">
                                    <input type="file" name="file_upload" accept="image/*,.pdf" class="btn btn-primary btn-file" />
                                </span>
                                <button type="submit" name="submit" class="btn btn-default">Upload</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="panel-body">
            <table class="table">
    <thead>
        <tr>
            <th class="text-center">#</th>
            <th class="text-center">File</th>
            <th class="text-center">File Name</th>
            <th class="text-center">Part Number</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($files as $file) : ?>
            <tr class="list-inline">
                <td class="text-center"><?php echo $file['id']; ?></td>
                <td class="text-center">
                    <?php
                    $file_name = $file['name'];
                    $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
                    if (in_array($file_extension, ['jpg', 'jpeg', 'png', 'gif'])) {
                        echo '<img src="uploads/' . $file_name . '" class="img-thumbnail" />';
                    } elseif ($file_extension === 'pdf') {
                        echo '<a href="uploads/' . $file_name . '" target="_blank">View PDF</a>';
                    }
                    ?>
                </td>
                <td class="text-center"><?php echo $file['name']; ?></td>
                <td class="text-center"><?php echo $file['part_number']; ?></td> <!-- Menampilkan Part Number -->
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
