<?php
// ডাটাবেস কানেকশন
$conn = mysqli_connect('localhost', 'mfudevlo_batuawallet', 'batua@wallet6602#', 'mfudevlo_batuawalletdb');

// ফাইল আপলোড লজিক
if (isset($_POST['import'])) {
    $filename = $_FILES['file']['tmp_name'];

    if ($_FILES['file']['size'] > 0) {
        $file = fopen($filename, "r");
        
        // হেডার লাইন বাদ দেওয়া
        fgetcsv($file);

        $count = 0;
        while (($column = fgetcsv($file, 1000, ",")) !== FALSE) {
            if (count($column) >= 5) {
                $operator = mysqli_real_escape_string($conn, trim($column[0]));
                $title    = mysqli_real_escape_string($conn, trim($column[1]));
                $price    = mysqli_real_escape_string($conn, trim($column[2]));
                $validity = mysqli_real_escape_string($conn, trim($column[3]));
                $status   = mysqli_real_escape_string($conn, trim($column[4]));

                $sql = "INSERT INTO offers (operator, title, price, validity, status) 
                        VALUES ('$operator', '$title', '$price', '$validity', '$status')";
                if($conn->query($sql)) $count++;
            }
        }
        fclose($file);
        echo "<div style='text-align:center; padding:20px; color:green; font-weight:bold;'>✅ সাফল্য: $count টি অফার আপডেট হয়েছে!</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Offer Import System</title>
    <style>
        body { font-family: Arial, sans-serif; background-color: #f4f7f6; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .card { background: white; padding: 30px; border-radius: 12px; box-shadow: 0 5px 15px rgba(0,0,0,0.1); text-align: center; width: 90%; max-width: 400px; }
        h2 { color: #333; margin-bottom: 20px; }
        input[type="file"] { display: block; width: 100%; margin: 20px 0; padding: 10px; border: 1px dashed #007bff; border-radius: 5px; cursor: pointer; }
        button { background: #007bff; color: white; border: none; padding: 12px 30px; border-radius: 5px; cursor: pointer; font-size: 16px; width: 100%; }
        button:hover { background: #0056b3; }
    </style>
</head>
<body>

<div class="card">
    <h2>Upload Offers CSV</h2>
    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="file" accept=".csv" required>
        <button type="submit" name="import">Import Now</button>
    </form>
    <p style="font-size: 12px; color: #777; margin-top: 15px;">Note: ফাইলটি অবশ্যই .csv ফরম্যাটে হতে হবে।</p>
</div>

</body>
</html>