<?php
require_once 'config.php';

checkAccess(['admin', 'programmer']);

$data = json_decode(file_get_contents("php://input"), true);

if (isset($data['id'])) {
    $stmt = $pdo->prepare("UPDATE properties SET 
        code = ?, type = ?, tower = ?, floor = ?, number = ?, aliquot = ?, 
        has_uncommon = ?, credit_balance = ?, initial_debt = ?, status = ? 
        WHERE id = ?");
    $stmt->execute([
        $data['code'], $data['type'], $data['towerBlock'], $data['floor'], 
        $data['number'], $data['aliquot'], $data['hasUncommonExpense'] ? 1 : 0, 
        $data['creditBalance'], $data['initialDebt'], $data['status'], $data['id']
    ]);
} else {
    $stmt = $pdo->prepare("INSERT INTO properties 
        (code, type, tower, floor, number, aliquot, has_uncommon, credit_balance, initial_debt, status) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->execute([
        $data['code'], $data['type'], $data['towerBlock'], $data['floor'], 
        $data['number'], $data['aliquot'], $data['hasUncommonExpense'] ? 1 : 0, 
        $data['creditBalance'], $data['initialDebt'], $data['status']
    ]);
}

response(["success" => true]);
?>