<?php 
header('Content-Type: text/html; charset=utf-8');
include("config.php"); 

if (isset($_POST['submit'])) {
    // Λήψη των δεδομένων από τη φόρμα
    $id = $_POST['id'];
    $dik_ofel = $_POST['dik_ofel'];
    $name = $_POST['name'];
    $katoikos = $_POST['katoikos'];
    $afm = $_POST['afm'];
    $amka = $_POST['amka'];
    $epitagi = $_POST['epitagi'];
    $room = $_POST['room'];
    $typos_domatio = $_POST['typos_domatio'];
    $plithos_dom = $_POST['plithos_dom'];
    $date_in = $_POST['date_in'];
    $date_out = $_POST['date_out'];
    $imeres = $_POST['imeres'];
    $dianiktere = $_POST['dianiktere'];
    $epidotisi = $_POST['epidotisi'];
    $simetoxi = $_POST['simetoxi'];
    $simf_simetox = $_POST['simf_simetox'];
    $sxolia = $_POST['sxolia'];
    $onoma_ex = $_POST['onoma_ex'];
    $im_ex = $_POST['im_ex'];
    $idiotita_ex = $_POST['idiotita_ex'];
    $periodos = $_POST['periodos'];
    // Σύνδεση PDO (αν δεν υπάρχει ήδη)
    try {
        $pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        die("Σφάλμα σύνδεσης: " . $e->getMessage());
    }

    // Εκτέλεση του update ερωτήματος στην βάση δεδομένων
    $stmt = $pdo->prepare("UPDATE `oaed_thea` SET 
        dik_ofel = :dik_ofel, name = :name, katoikos = :katoikos, afm = :afm, amka = :amka, 
        epitagi = :epitagi, room = :room, typos_domatio = :typos_domatio, plithos_dom = :plithos_dom, 
        date_in = :date_in, date_out = :date_out, imeres = :imeres, dianiktere = :dianiktere, 
        epidotisi = :epidotisi, simetoxi = :simetoxi, simf_simetox = :simf_simetox, 
        sxolia = :sxolia, onoma_ex = :onoma_ex, im_ex = :im_ex, idiotita_ex = :idiotita_ex,  periodos = :periodos
        WHERE id = :id");

    $stmt->execute([
        'dik_ofel' => $dik_ofel, 'name' => $name, 'katoikos' => $katoikos, 'afm' => $afm, 
        'amka' => $amka, 'epitagi' => $epitagi, 'room' => $room, 'typos_domatio' => $typos_domatio, 
        'plithos_dom' => $plithos_dom, 'date_in' => $date_in, 'date_out' => $date_out, 
        'imeres' => $imeres, 'dianiktere' => $dianiktere, 'epidotisi' => $epidotisi, 
        'simetoxi' => $simetoxi, 'simf_simetox' => $simf_simetox, 'sxolia' => $sxolia, 
        'onoma_ex' => $onoma_ex, 'im_ex' => $im_ex, 'idiotita_ex' => $idiotita_ex, 'periodos' => $periodos, 'id' => $id
    ]);

    // Μήνυμα και επιστροφή 2 σελίδες πίσω
    echo "<script>alert('Η εγγραφή ενημερώθηκε επιτυχώς!'); history.go(-2);</script>";
    exit;
}
?>
