<?php
// Set flag that this is a parent file
define('_FEXEC', 1);
require('includes/application_top.php');

if (isset($_POST['import_csv'])) {
    if (isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] == 0) {
        $csvFile = fopen($_FILES['csv_file']['tmp_name'], 'r');

        // Clear the existing seat plan data
        tep_db_query("TRUNCATE TABLE seat_plan");

        // Read first row and check if it's a header by checking the first cell.
        $firstRow = fgetcsv($csvFile);
        $hasHeader = false;
        if ($firstRow && strtolower(trim($firstRow[0])) == "products_id") {
            $hasHeader = true;
            error_log("CSV header detected, skipping first row.");
        } else {
            rewind($csvFile);
        }

        // Process each CSV row
        while (($row = fgetcsv($csvFile)) !== FALSE) {
            // Ensure there are at least 11 columns to process basic data
            if (count($row) >= 11) {
                // Basic 11 columns (always expected)v10.2
                $products_id       = tep_db_input(trim($row[0]));
                $products_quantity = tep_db_input(trim($row[1]));
                $products_name     = isset($row[2]) ? tep_db_input(trim($row[2])) : '';
                $products_number   = isset($row[3]) ? tep_db_input(trim($row[3])) : '';
                $color_code        = isset($row[4]) ? tep_db_input(trim($row[4])) : '';
                $products_status   = isset($row[5]) ? tep_db_input(trim($row[5])) : '1';
                $products_x        = isset($row[6]) ? tep_db_input(trim($row[6])) : '0';
                $products_y        = isset($row[7]) ? tep_db_input(trim($row[7])) : '0';
				$products_w        = isset($row[8]) ? tep_db_input(trim($row[8])) : '44';
				$products_h        = isset($row[9]) ? tep_db_input(trim($row[9])) : '36';
				$products_r        = isset($row[10]) ? tep_db_input(trim($row[10])) : '0';

                // If extra columns are provided (i.e. full export with 14 columns), use them
                if (count($row) >= 14) {
					//$products_w  = (isset($row[8])  && trim($row[8])  !== '') ? tep_db_input(trim($row[8]))  : '44';
                    //$products_h  = (isset($row[9])  && trim($row[9])  !== '') ? tep_db_input(trim($row[9]))  : '36';
					//$products_r  = (isset($row[10]) && trim($row[10]) !== '') ? tep_db_input(trim($row[10])) : '0';
                    $products_sx = (isset($row[11]) && trim($row[11]) !== '') ? tep_db_input(trim($row[11])) : '0';
                    $products_sy = (isset($row[12]) && trim($row[12]) !== '') ? tep_db_input(trim($row[12])) : '0';
                    $product_type = (isset($row[13]) && trim($row[13]) !== '') ? tep_db_input(trim($row[13])) : 'P';
                } else {
                    // If only the basic columns exist, assign default values for extras
                    //$products_w  = '44';
                    //$products_h  = '36';
                    //$products_r  = '0';
                    $products_sx = '0';
                    $products_sy = '0';
                    $product_type = 'P';
                }

                // Insert into the seat_plan table using all 14 columns
                $query = "INSERT INTO seat_plan (
                    products_id, products_quantity, products_name, products_number, color_code, products_status, 
                    products_x, products_y, products_w, products_h, products_r, products_sx, products_sy, product_type
                ) VALUES (
                    '$products_id', '$products_quantity', '$products_name', '$products_number', '$color_code', '$products_status',
                    '$products_x', '$products_y', '$products_w', '$products_h', '$products_r', '$products_sx', '$products_sy', '$product_type'
                )";
                tep_db_query($query);
            } else {
                error_log('Invalid CSV row: ' . print_r($row, true));
            }
        }
        fclose($csvFile);

        $_SESSION['import_success'] = "CSV file has been successfully imported!";
        header('Location: seatplan_generator.php'); // Adjust redirection as needed
        exit;
    } else {
        echo "Error uploading file. Please try again.";
    }
}
?>
