<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

include('config.php');

if (!$bd) {
    die("Connection failed: " . mysqli_connect_error());
}

header('Content-Type: text/html; charset=utf-8');
$bd->set_charset("utf8mb4");

// Έλεγχος POST δεδομένων
echo '<pre>';
print_r($_POST);
echo '</pre>';

// Ανάκτηση τιμών από POST
$ar_symb = isset($_POST['ar_symb']) ? $_POST['ar_symb'] : NULL;
$dik_ofel = isset($_POST['dik_ofel']) ? $_POST['dik_ofel'] : NULL;
$name = isset($_POST['name']) ? trim($_POST['name']) : NULL;
$katoikos = isset($_POST['katoikos']) ? $_POST['katoikos'] : NULL;
$afm = isset($_POST['afm']) ? trim($_POST['afm']) : NULL;
$amka = isset($_POST['amka']) ? trim($_POST['amka']) : NULL;
$epitagi = isset($_POST['epitagi']) ? trim($_POST['epitagi']) : NULL;
$room = isset($_POST['room']) ? $_POST['room'] : NULL;
$typos_domatio = isset($_POST['typos_domatio']) ? $_POST['typos_domatio'] : NULL;
$extra = NULL; // INT που επιτρέπεται να είναι NULL
$plithos_dom = isset($_POST['plithos_dom']) ? $_POST['plithos_dom'] : NULL;
$date_in = isset($_POST['date_in']) ? $_POST['date_in'] : NULL;
$date_out = isset($_POST['date_out']) ? $_POST['date_out'] : NULL;
$imeres = isset($_POST['imeres']) ? $_POST['imeres'] : NULL;
$dianiktere = isset($_POST['dianiktere']) ? $_POST['dianiktere'] : NULL;
$epidotisi = isset($_POST['epidotisi']) ? $_POST['epidotisi'] : NULL;
$simetoxi = isset($_POST['simetoxi']) ? $_POST['simetoxi'] : NULL;
$simf_simetox = isset($_POST['simf_simetox']) ? $_POST['simf_simetox'] : NULL;
$hotel = 'thea'; // default value
$periodos = isset($_POST['periodos']) ? $_POST['periodos'] : NULL;
$ar_ofel = isset($_POST['ar_ofel']) ? $_POST['ar_ofel'] : NULL;
$sxolia = isset($_POST['sxolia']) ? $_POST['sxolia'] : NULL;
$onoma_ex = isset($_POST['onoma_ex']) ? $_POST['onoma_ex'] : NULL;
$im_ex = isset($_POST['im_ex']) ? $_POST['im_ex'] : NULL;
$idiotita_ex = isset($_POST['idiotita_ex']) ? $_POST['idiotita_ex'] : NULL;

// SQL query
$sql = "INSERT INTO oaed_thea 
    (ar_symb, dik_ofel, name, katoikos, afm, amka, epitagi, room, typos_domatio, extra, 
    plithos_dom, date_in, date_out, imeres, dianiktere, epidotisi, simetoxi, simf_simetox, hotel, 
    periodos, ar_ofel, sxolia, onoma_ex, im_ex, idiotita_ex) 
VALUES 
    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = $bd->prepare($sql);
if ($stmt === false) {
    die("SQL query preparation failed: " . $bd->error);
}

// Bind parameters με σωστούς τύπους:
// s = string, i = int, d = double
$stmt->bind_param(
    'sssssssissssssdddsissssss',  // 25 τύποι
    $ar_symb,        // s
    $dik_ofel,       // s
    $name,           // s
    $katoikos,       // s
    $afm,            // s
    $amka,           // s
    $epitagi,        // s
    $room,           // s
    $typos_domatio,  // i
    $extra,          // i (NULL if not given)
    $plithos_dom,    // s
    $date_in,        // s
    $date_out,       // s
    $imeres,         // s
    $dianiktere,     // s
    $epidotisi,      // d
    $simetoxi,       // d
    $simf_simetox,   // d
    $hotel,          // s
    $periodos,       // s
    $ar_ofel,        // s
    $sxolia,         // s
    $onoma_ex,       // s
    $im_ex,          // s
    $idiotita_ex     // s
);

// Εκτέλεση
if ($stmt->execute()) {
    // Επιτυχής εισαγωγή - ανακατεύθυνση
    echo "<meta http-equiv='refresh' content='0; url=http://hotelthea.gr/n_oaed24s/dik_oaed.php'>";
} else {
    echo "Error inserting record: " . $stmt->error;
}

// Κλείσιμο
$stmt->close();
$bd->close();
?>
