<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: ../login.php");
    exit;
}

function e($v){
    return htmlspecialchars($v ?? '', ENT_QUOTES);
}

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) die("Invalid product ID");

$stmt = $conn->prepare("
    SELECT p.*, c.country_name
    FROM products p
    LEFT JOIN countries c ON c.id = p.country_id
    WHERE p.id = ?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$product = $stmt->get_result()->fetch_assoc();

if (!$product) die("Product not found");
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>View Product</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
:root{
    --sidebar:260px;
    --header:70px;
}

/* ===== BODY ===== */
body{
    margin:0;
    font-family:Arial;
    background:#f4f6f9;
}

/* ===== HEADER FIX ===== */
header{
    position:fixed;
    top:0;
    left:0;
    right:0;
    height:var(--header);
    background:#fff;
    z-index:1000;
    box-shadow:0 2px 10px rgba(0,0,0,0.08);
}

/* ===== SIDEBAR FIX ===== */
.sidebar{
    position:fixed;
    top:var(--header);
    left:0;
    width:var(--sidebar);
    height:calc(100vh - var(--header));
    background:#fff;
    z-index:999;
}

/* ===== CONTENT ===== */
.container{
    margin-left:var(--sidebar);
    margin-top:var(--header);
    padding:20px;
}

/* ===== PAGE HEADER ===== */
.page-title{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:20px;
}

.page-title h1{
    font-size:22px;
    margin:0;
}

/* ===== CARD ===== */
.card{
    background:#fff;
    border-radius:14px;
    padding:20px;
    box-shadow:0 8px 25px rgba(0,0,0,0.06);
}

/* ===== SECTION ===== */
.section{
    margin-bottom:25px;
}

.section-title{
    font-weight:600;
    margin-bottom:10px;
    border-bottom:1px solid #eee;
    padding-bottom:5px;
}

/* ===== GRID ===== */
.grid{
    display:grid;
    grid-template-columns:repeat(3,1fr);
    gap:15px;
}

/* ===== FIELD ===== */
.field{
    background:#f9fafb;
    padding:12px;
    border-radius:10px;
    border:1px solid #e5e7eb;
}

.label{
    font-size:11px;
    color:#777;
}

.value{
    font-size:14px;
    font-weight:600;
}

/* FULL */
.full{
    grid-column:1/-1;
}

/* BUTTON */
.btn{
    background:#000;
    color:#fff;
    padding:7px 14px;
    border-radius:8px;
    text-decoration:none;
    font-size:13px;
}

/* MOBILE */
@media(max-width:768px){
    .container{
        margin-left:0;
    }
    .grid{
        grid-template-columns:1fr;
    }
}
</style>
</head>

<body>

<?php include "../header.php"; ?>
<?php include "adminsidebar.php"; ?>

<div class="container">

<!-- ✅ PAGE HEADER (IMPORTANT) -->
<div class="page-title">
    <h1><i class="fa fa-eye"></i> View Product</h1>
    <a href="admin_product_approval.php" class="btn">← Back</a>
</div>

<div class="card">

<!-- BASIC -->
<div class="section">
<div class="section-title">Basic Info</div>
<div class="grid">

<div class="field"><div class="label">Product</div><div class="value"><?=e($product['product_name'])?></div></div>
<div class="field"><div class="label">Generic</div><div class="value"><?=e($product['generic_name'])?></div></div>
<div class="field"><div class="label">Brand</div><div class="value"><?=e($product['brand_name'])?></div></div>

<div class="field"><div class="label">Strength</div><div class="value"><?=e($product['strength'])?></div></div>
<div class="field"><div class="label">Volume</div><div class="value"><?=e($product['volume'])?></div></div>
<div class="field"><div class="label">Dosage Form</div><div class="value"><?=e($product['dosage_form'])?></div></div>

<div class="field"><div class="label">Shelf Life</div><div class="value"><?=e($product['shelf_life'])?></div></div>
<div class="field"><div class="label">Country</div><div class="value"><?=e($product['country_name'])?></div></div>

</div>
</div>

<!-- PACKING -->
<div class="section">
<div class="section-title">Packing Details</div>
<div class="grid">
<div class="field"><div class="label">Primary Packing</div><div class="value"><?=e($product['primary_packing'])?></div></div>
<div class="field"><div class="label">Secondary Packing</div><div class="value"><?=e($product['secondary_packing'])?></div></div>
</div>
</div>

<!-- COMPANY -->
<div class="section">
<div class="section-title">Company Info</div>
<div class="grid">
<div class="field"><div class="label">Manufactured</div><div class="value"><?=e($product['manufactured_by'])?></div></div>
<div class="field"><div class="label">Marketed</div><div class="value"><?=e($product['marketed_by'])?></div></div>
<div class="field"><div class="label">Distributed</div><div class="value"><?=e($product['distributed_by'])?></div></div>
</div>
</div>

<!-- PRICING -->
<div class="section">
<div class="section-title">Pricing</div>
<div class="grid">
<div class="field"><div class="label">Qty</div><div class="value"><?=e($product['qty'])?></div></div>
<div class="field"><div class="label">Units</div><div class="value"><?=e($product['units_per_packet'])?></div></div>
<div class="field"><div class="label">Price</div><div class="value">₹ <?=e($product['price_per_packet'])?></div></div>
</div>
</div>

<!-- INDICATION -->
<div class="section">
<div class="section-title">Indication</div>
<div class="field full">
    <div class="value"><?=e($product['indication'])?></div>
</div>
</div>

</div>
</div>

</body>
</html>