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

include('include/headscript.php');

// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);

// Verifica della connessione
if ($conn->connect_error) {
    die("Connessione fallita: " . $conn->connect_error);
}

// Recupera l'id utente loggato dalla sessione
$iduserlogin = intval($_SESSION['iduserlogin']);

// Recupera i dati inviati tramite POST
$owner_id = isset($_POST['owner_id']) ? intval($_POST['owner_id']) : 0;
$owner_type = isset($_POST['owner_type']) ? $conn->real_escape_string($_POST['owner_type']) : 'individual'; // Default to 'individual'
$first_name = !empty($_POST['first_name']) ? $conn->real_escape_string($_POST['first_name']) : null;
$last_name = !empty($_POST['last_name']) ? $conn->real_escape_string($_POST['last_name']) : null;
$company_name = !empty($_POST['company_name']) ? $conn->real_escape_string($_POST['company_name']) : null;
$tax_code = isset($_POST['tax_code']) ? $conn->real_escape_string($_POST['tax_code']) : '';
$email = isset($_POST['email']) ? $conn->real_escape_string($_POST['email']) : '';
$phone = !empty($_POST['phone']) ? $conn->real_escape_string($_POST['phone']) : null;
$address = !empty($_POST['address']) ? $conn->real_escape_string($_POST['address']) : null;
$postal_code = !empty($_POST['postal_code']) ? $conn->real_escape_string($_POST['postal_code']) : null;
$city = !empty($_POST['city']) ? $conn->real_escape_string($_POST['city']) : null;
$province = !empty($_POST['province']) ? $conn->real_escape_string($_POST['province']) : null;
$country = isset($_POST['country']) ? intval($_POST['country']) : null;
$role = !empty($_POST['role']) ? $conn->real_escape_string($_POST['role']) : null;

// Verifica se stiamo aggiungendo un nuovo proprietario o aggiornando uno esistente
if ($owner_id > 0) {
    // Aggiorna il proprietario esistente
    $query = "UPDATE property_owners 
    SET owner_type = ?, first_name = ?, last_name = ?, company_name = ?, tax_code = ?, email = ?, phone = ?, address = ?, postal_code = ?, city = ?, province = ?, country = ?, role = ? 
    WHERE owner_id = ? AND user_id = ?";
    $stmt = $conn->prepare($query);
    if ($stmt === false) {
        die("Errore nella preparazione della query: " . $conn->error);
    }

    // Imposta i valori nulli per i campi facoltativi
    $company_name = !empty($company_name) ? $company_name : null;
    $role = !empty($role) ? $role : null;

    // Binding dei parametri
    $stmt->bind_param(
        'ssssssssssssiis',
        $owner_type,
        $first_name,
        $last_name,
        $company_name,
        $tax_code,
        $email,
        $phone,
        $address,
        $postal_code,
        $city,
        $province,
        $country,
        $role,
        $owner_id,
        $iduserlogin
    );

    // Esegui la query
    if ($stmt->execute()) {
        header("Location: person-list.php");
        exit();
    } else {
        die("Errore nell'aggiornamento: " . $stmt->error);
    }
} else {
    // Inserisci un nuovo proprietario
    $query = "INSERT INTO property_owners (user_id, owner_type, first_name, last_name, company_name, tax_code, email, phone, address, postal_code, city, province, country, role) 
              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($query);
    if ($stmt === false) {
        die("Errore nella preparazione della query: " . $conn->error);
    }

    // Binding dei parametri
    $stmt->bind_param(
        'isssssssssssis',
        $iduserlogin,
        $owner_type,
        $first_name,
        $last_name,
        $company_name,
        $tax_code,
        $email,
        $phone,
        $address,
        $postal_code,
        $city,
        $province,
        $country,
        $role
    );

    // Esegui la query
    if ($stmt->execute()) {
        header("Location: person-list.php");
        exit();
    } else {
        die("Errore nell'inserimento: " . $stmt->error);
    }
}

// Chiudi la connessione
$conn->close();
