<?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);
}

/* PRODUCT ID OPTIONAL */
$product_id = (int)($_GET['product_id'] ?? 0);

$product = null;

/* SINGLE PRODUCT VARIATIONS */
if ($product_id > 0) {

    $stmt = $conn->prepare("
SELECT 
    product_name,
    registration_number,
    (
        SELECT COUNT(*)
        FROM admin_product_approval apa
        WHERE apa.product_id = products.id
          AND apa.type = 'variation'
          AND apa.status = 'approved'
    ) AS variation_count
FROM products
WHERE id=?
    ");
    $stmt->bind_param("i", $product_id);
    $stmt->execute();
    $product = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    if (!$product) {
        die("Product not found");
    }

    $stmt = $conn->prepare("
        SELECT
    apa.product_name,
    p.registration_number,
    apa.id AS variation_no,
    apa.type AS variation_type,
    'Approved from admin approval' AS variation_description,
    apa.created_at AS submission_date,
    CASE 
        WHEN apa.status='approved' THEN apa.created_at
        ELSE NULL
    END AS approval_date,
    '' AS comments,
    '' AS document_file
FROM admin_product_approval apa
LEFT JOIN products p ON p.id = apa.product_id
WHERE apa.product_id=?
  AND apa.type IN ('edit','variation')
  AND apa.status='approved'
ORDER BY apa.id DESC
    ");
    $stmt->bind_param("i", $product_id);

/* ALL VARIATIONS */
} else {

    $stmt = $conn->prepare("
        SELECT
    apa.product_name,
    p.registration_number,
    apa.id AS variation_no,
    apa.type AS variation_type,
    'Approved from admin approval' AS variation_description,
    apa.created_at AS submission_date,
    CASE 
        WHEN apa.status='approved' THEN apa.created_at
        ELSE NULL
    END AS approval_date,
    '' AS comments,
    '' AS document_file
FROM admin_product_approval apa
LEFT JOIN products p ON p.id = apa.product_id
WHERE apa.type IN ('edit','variation')
  AND apa.status='approved'
ORDER BY apa.product_id DESC, apa.id DESC
    ");
}

$stmt->execute();
$variations = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Variation Module</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
body{
    margin:0;
    font-family:Arial,sans-serif;
    background:#f4f6f9;
    color:#111827;
}

/* MAIN */
.main-content{
    margin-left:260px;
    padding:20px 20px 20px;
}

/* CARD */
.card{
    background:#fff;
    border-radius:14px;
    padding:24px;
    box-shadow:0 4px 18px rgba(0,0,0,.08);
}

/* HEADER */
.top-bar{
    display:flex;
    justify-content:space-between;
    align-items:center;
    flex-wrap:wrap;
    gap:10px;
    margin-bottom:20px;
}

h2{
    margin:0;
    font-size:24px;
    color:#1f2937;
}

/* BUTTON */
.btn{
    display:inline-flex;
    align-items:center;
    gap:8px;
    background:#374151;
    color:#fff;
    text-decoration:none;
    padding:10px 16px;
    border-radius:8px;
    font-size:14px;
    border:none;
    cursor:pointer;
}

.btn:hover{
    background:#111827;
}

/* TABLE */
.table-wrap{
    width:100%;
    overflow-x:auto;
}

table{
    width:100%;
    min-width:1200px;
    border-collapse:collapse;
}

th{
    background:#374151;
    color:#fff;
    padding:12px;
    text-align:left;
    font-size:13px;
    white-space:nowrap;
}

td{
    padding:12px;
    border-bottom:1px solid #e5e7eb;
    font-size:13px;
    vertical-align:top;
}

tr:hover{
    background:#f9fafb;
}

.badge{
    display:inline-block;
    padding:4px 10px;
    border-radius:999px;
    font-size:12px;
    background:#e0f2fe;
    color:#0369a1;
}

/* PDF */
.pdf-link{
    color:#2563eb;
    text-decoration:none;
    font-weight:600;
}

.pdf-link:hover{
    text-decoration:underline;
}

/* EMPTY */
.empty{
    text-align:center;
    padding:30px;
    color:#6b7280;
}

/* MOBILE */
@media(max-width:768px){

    .main-content{
        margin-left:0 !important;
        padding:75px 12px 20px;
    }

    .top-bar{
        flex-direction:column;
        align-items:stretch;
    }

    .btn{
        width:100%;
        justify-content:center;
    }

    table{
        min-width:900px;
    }
}
</style>
</head>
<body>

<?php include 'rasidebar.php'; ?>
<?php include '../header.php'; ?>

<div class="main-content">

    <div class="card">

        <div class="top-bar">

            <h2>
                <i class="fa fa-code-branch"></i>

                <?php if ($product_id > 0 && $product): ?>
                    Variation History -
                    <?php echo e($product['product_name']); ?>
                    (<?php echo e($product['registration_number']); ?>)
                   
                <?php else: ?>
                    All Product Variations
                <?php endif; ?>
            </h2>

        </div>

        <div class="table-wrap">

            <table>

                <tr>
                    <th>Product Name</th>
                    <th>Registration No</th>
                    <th>Variation No</th>
                    <th>Type</th>
                    <th>Description</th>
                    <th>Submission Date</th>
                    <th>Approval Date</th>
                    <th>Comments</th>
                    <th>Document</th>
                </tr>

                <?php if($variations->num_rows > 0): ?>

                    <?php while($v = $variations->fetch_assoc()): ?>

                        <tr>

                            <td>
                                <?php echo e($v['product_name']); ?>
                            </td>

                            <td>
                                <?php echo e($v['registration_number']); ?>
                            </td>

                            <td>
                                <span class="badge">
                                    #<?php echo e($v['variation_no']); ?>
                                </span>
                            </td>

                            <td>
                                <?php echo e($v['variation_type']); ?>
                            </td>

                            <td>
                                <?php echo e($v['variation_description']); ?>
                            </td>

                            <td>
                                <?php echo !empty($v['submission_date']) ? date('d-m-Y', strtotime($v['submission_date'])) : '—'; ?>
                            </td>

                            <td>
                                <?php echo !empty($v['approval_date']) ? date('d-m-Y', strtotime($v['approval_date'])) : 'Pending'; ?>
                            </td>

                            <td>
                                <?php echo !empty($v['comments']) ? e($v['comments']) : '—'; ?>
                            </td>

                            <td>
                                <?php if(!empty($v['document_file'])): ?>
                                    <a class="pdf-link" href="../<?php echo e($v['document_file']); ?>" target="_blank">
                                        <i class="fa fa-file-pdf"></i> View PDF
                                    </a>
                                <?php else: ?>
                                    —
                                <?php endif; ?>
                            </td>

                        </tr>

                    <?php endwhile; ?>

                <?php else: ?>

                    <tr>
                        <td colspan="9" class="empty">
                            No variations found.
                        </td>
                    </tr>

                <?php endif; ?>

            </table>

        </div>

    </div>

</div>

</body>
</html>