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

if (isset($_GET['id'])) {
    $product_id = (int)$_GET['id'];
    $product = find_by_id('products', $product_id);
    if (!$product) {
        $session->msg("d", "Product not found!");
        redirect('product.php');
    }
} else {
    $session->msg("d", "Missing product ID.");
    redirect('product.php');
}

$media_id = $product['media_id'];
if ($media_id !== '0') {
    $media = find_by_id('media', $media_id);
} else {
    $session->msg("d", "No file associated with this product.");
    redirect('product.php');
}

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

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

<div class="row">
    <div class="panel panel-default">
        <div class="panel-heading">
            <strong>
                <span class="glyphicon glyphicon-file"></span>
                <span>View File</span>
            </strong>
        </div>
        <div class="panel-body">
            <div class="col-md-7">
                <h4>Product Name: <?php echo $product['name']; ?></h4>
                <h4>File Name: <?php echo $media['file_name']; ?></h4>
                <h4>File Type: <?php echo $media['file_type']; ?></h4>
                <h4>Uploaded Date: <?php echo $media['created']; ?></h4>
                <br>
                <?php
                $file_path = "uploads/products/{$media['file_name']";
                if (strpos($media['file_type'], 'image') !== false) {
                    echo '<img src="' . $file_path . '" class="img-thumbnail" />';
                } else if ($media['file_type'] == 'application/pdf') {
                    echo '<embed src="' . $file_path . '" type="application/pdf" width="100%" height="600px" />';
                }
                ?>
            </div>
        </div>
    </div>
</div>

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