<?php
if ($_SERVER['HTTP_HOST'] === 'localhost') {
    // Localhost connection
    $host = "localhost";
    $user = "root";
    $pass = "";
    $db   = "sales_data";
} else {
    // Live server connection
    $host = "your_live_host";
    $user = "your_live_user";
    $pass = "your_live_password";
    $db   = "sales_data";
}
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
}

$message = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
    $file = $_FILES['csv_file']['tmp_name'];

    if (is_uploaded_file($file)) {
        $handle = fopen($file, "r");
        if ($handle !== FALSE) {

            // Skip header if exists (optional)
            fgetcsv($handle);

            $count = 0;
            $lineNo = 0;

            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $lineNo++;

                // Skip completely empty lines
                if (count($data) < 3 || empty(trim($data[0])) && empty(trim($data[1])) && empty(trim($data[2]))) {
                    continue;
                }

                // Assign values safely
                $Date = isset($data[0]) ? trim($conn->real_escape_string($data[0])) : null;
                $Phone = isset($data[1]) ? trim($conn->real_escape_string($data[1])) : null;
                $Disposition = isset($data[2]) ? trim($conn->real_escape_string($data[2])) : null;

                // Insert only if all values exist
                if ($Date && $Phone && $Disposition) {
                    $conn->query("INSERT INTO data (Date, Phone, Disposition) VALUES ('$Date', '$Phone', '$Disposition')");
                    $count++;
                }
            }

            fclose($handle);
            $message = "✅ Successfully imported <b>$count</b> valid rows.";
        } else {
            $message = "❌ Failed to open file.";
        }
    } else {
        $message = "❌ Please upload a valid CSV file.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Upload CSV - Sales Data</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

    <link rel="stylesheet" href="style.css">
</head>
<body>

<nav class="navbar">
    <div class="nav-container">
        <!-- <div class="logo">📊 Sales Panel</div> -->
        <div class="menu-toggle" id="mobile-menu">
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>
        </div>
        <ul class="nav-menu">
            <li><a href="index.php">Dashboard</a></li>
            <li><a href="uploader.php" class="active">Uploader</a></li>
        </ul>
    </div>
</nav>

<div class="container">
    <h2>📤 Upload Sales Data CSV</h2>
    <p>Upload your CSV file containing <b>Date, Phone, Disposition</b> columns.</p>
    <br>

    <?php if (!empty($message)): ?>
        <div style="padding:10px; background:#e8f5e9; border-left:5px solid #1abc9c; margin-bottom:20px;">
            <?= $message ?>
        </div>
    <?php endif; ?>

    <form method="POST" enctype="multipart/form-data" class="upload-form">
        <div class="form-group">
            <label>Select CSV File:</label><br><br>
            <input type="file" name="csv_file" accept=".csv" required>
        </div>
        <br>
        <button type="submit" class="btn-upload">Upload File</button>
    </form>
</div>

<script>
    const menu = document.querySelector('#mobile-menu');
    const navMenu = document.querySelector('.nav-menu');
    menu.addEventListener('click', () => {
        menu.classList.toggle('is-active');
        navMenu.classList.toggle('active');
    });
</script>

</body>
</html>
