<?php
require 'auth_check.php';
require '../config/db.php';

$msg = "";
$msgClass = "";

if (isset($_POST['import'])) {
    if ($_FILES['csv_file']['size'] > 0) {
        $file = $_FILES['csv_file']['tmp_name'];
        $handle = fopen($file, "r");
        
        // Skip Header Row
        fgetcsv($handle);
        
        $count = 0;
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            // CSV Format: Name, Description, Category ID, Price, Variant Name (e.g. 1 Year)
            $name = $data[0];
            $desc = $data[1];
            $cat_id = $data[2];
            $price = $data[3];
            $var_name = $data[4];
            
            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $name)));

            // 1. Insert Product
            $stmt = $pdo->prepare("INSERT INTO products (name, slug, description, category_id, is_active) VALUES (?, ?, ?, ?, 1)");
            $stmt->execute([$name, $slug, $desc, $cat_id]);
            $pid = $pdo->lastInsertId();

            // 2. Insert Variant
            $stmtV = $pdo->prepare("INSERT INTO product_variants (product_id, name, price, stock_qty) VALUES (?, ?, ?, -1)");
            $stmtV->execute([$pid, $var_name, $price]);
            
            $count++;
        }
        fclose($handle);
        $msg = "Success! Imported $count products.";
        $msgClass = "success";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bulk Import</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .import-box { background: white; padding: 30px; border-radius: 8px; max-width: 600px; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .csv-guide { background: #f9f9f9; padding: 15px; border-left: 4px solid #3498db; margin-bottom: 20px; font-size: 14px; }
        .msg-box { padding: 10px; border-radius: 5px; margin-bottom: 20px; text-align: center; background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
    </style>
</head>
<body>
    <div class="sidebar"><?php include 'sidebar.php'; ?></div>
    <div class="content">
        <h1 style="text-align: center;">Bulk Product Import</h1>
        <?php if($msg): ?><div class="msg-box"><?php echo $msg; ?></div><?php endif; ?>

        <div class="import-box">
            <div class="csv-guide">
                <strong>CSV Format Required (Columns):</strong><br>
                Name, Description, Category ID, Price, Plan Name<br><br>
                <em>Example:</em><br>
                <code>Canva Pro, Best Design Tool, 1, 499, 1 Year</code>
            </div>

            <form method="POST" enctype="multipart/form-data">
                <input type="file" name="csv_file" required style="margin-bottom: 20px; border: 1px solid #ddd; padding: 10px; width: 100%;">
                <button type="submit" name="import" class="btn btn-primary" style="width: 100%; padding: 12px; font-size: 16px;">
                    <i class="fas fa-file-upload"></i> Upload & Import
                </button>
            </form>
        </div>
    </div>
</body>
</html>