<?php
declare(strict_types=1);

if (session_status() === PHP_SESSION_NONE) { session_start(); }
global $pdo;

header('Content-Type: application/json; charset=utf-8');

function as_str($v, $d=''){ $v = trim((string)($v ?? '')); return $v!=='' ? $v : $d; }

$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) { echo json_encode(['ok'=>false,'error'=>'id yok']); exit; }

$telefon1 = as_str($_POST['telefon1'] ?? '', '');
$telefon2 = as_str($_POST['telefon2'] ?? '', '');
$email    = as_str($_POST['email'] ?? '', '');

if ($telefon1 === '') { echo json_encode(['ok'=>false,'error'=>'Telefon 1 boş olamaz']); exit; }

try {
  $st = $pdo->prepare("UPDATE hastalar SET telefon1=?, telefon2=?, email=?, updated_at=CURRENT_TIMESTAMP WHERE id=?");
  $st->execute([$telefon1, $telefon2 ?: null, $email ?: null, $id]);
  echo json_encode(['ok'=>true], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
  echo json_encode(['ok'=>false,'error'=>$e->getMessage()]);
}
