File: /home/muratemr/theotto.tr/admin/customer_detail.php
<?php
declare(strict_types=1);
$page_title = 'Müşteri Detay';
$active_menu = 'customers';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/csrf.php';
require_permission('customers');
$id = get_int('id');
if (!$id) { header('Location: customers.php'); exit; }
$customer = db_row("SELECT c.*, ct.name AS tag_name, ct.discount_rate
FROM customers c LEFT JOIN customer_tags ct ON ct.id=c.tag_id WHERE c.id=?", [$id]);
if (!$customer) { header('Location: customers.php'); exit; }
// İsim değişim loglarını ayrıştır
$note_raw = $customer['admin_note'] ?? '';
$note_lines = explode("\n", $note_raw);
$name_logs = array_filter($note_lines, fn($l) => preg_match('/^\d{2}\.\d{2}\.\d{4}: /', trim($l)));
$clean_note = trim(implode("\n", array_filter($note_lines, fn($l) => !preg_match('/^\d{2}\.\d{2}\.\d{4}: /', trim($l)))));
// Log satırlarını parse et
$parsed_logs = [];
foreach ($name_logs as $log) {
if (preg_match('/^(\d{2}\.\d{2}\.\d{4}): "([^"]+)" adıyla rezervasyon yaptı \(eski: "([^"]+)"\)/', trim($log), $m)) {
$parsed_logs[] = ['date' => $m[1], 'new_name' => $m[2], 'old_name' => $m[3]];
}
}
// İsim değiştirme POST
$msg = ''; $err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['set_name'])) {
if (!hash_equals(csrf_token(), $_POST['csrf_token'] ?? '')) { $err = 'Güvenlik hatası.'; }
else {
$new_name = trim($_POST['full_name'] ?? '');
if (!$new_name) { $err = 'İsim boş olamaz.'; }
else {
db_run("UPDATE customers SET full_name=? WHERE id=?", [$new_name, $id]);
$msg = '"' . $new_name . '" ismi kayıtlı isim olarak ayarlandı.';
$customer['full_name'] = $new_name;
}
}
}
// Not kaydetme POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_note'])) {
if (!hash_equals(csrf_token(), $_POST['csrf_token'] ?? '')) { $err = 'Güvenlik hatası.'; }
else {
$new_note_text = trim($_POST['admin_note'] ?? '');
// Log satırlarını koru
$log_part = implode("\n", $name_logs);
$full_note = $log_part ? $log_part . ($new_note_text ? "\n" . $new_note_text : '') : $new_note_text;
db_run("UPDATE customers SET admin_note=? WHERE id=?", [$full_note, $id]);
$msg = 'Not kaydedildi.';
$clean_note = $new_note_text;
}
}
// Rezervasyon geçmişi
$reservations = db_rows(
"SELECT full_name, DATE_FORMAT(date,'%d.%m.%Y') AS date,
TIME_FORMAT(time,'%H:%i') AS time, guest_count, status, special_request
FROM reservations WHERE customer_id=? ORDER BY date DESC LIMIT 50", [$id]);
$status_label = ['bekliyor'=>'Bekliyor','onaylandi'=>'Onaylı','reddedildi'=>'Reddedildi','iptal'=>'İptal','arsiv'=>'Arşiv'];
$status_badge = ['bekliyor'=>'warning','onaylandi'=>'success','reddedildi'=>'danger','iptal'=>'secondary','arsiv'=>'info'];
// Tüm benzersiz isimler (seçim için)
$all_names = [$customer['full_name']];
foreach ($parsed_logs as $l) {
if (!in_array($l['old_name'], $all_names)) $all_names[] = $l['old_name'];
if (!in_array($l['new_name'], $all_names)) $all_names[] = $l['new_name'];
}
require_once __DIR__ . '/../includes/admin_layout.php';
?>
<div class="d-flex align-items-center gap-2 mb-4">
<a href="customers.php" class="btn btn-outline-secondary btn-sm">
<i class="bi bi-arrow-left me-1"></i>Müşteri Listesi
</a>
<h5 class="mb-0 ms-2">
<i class="bi bi-person-circle text-warning me-2"></i>Müşteri Detay
</h5>
</div>
<?php if ($msg): ?>
<div class="alert alert-success alert-dismissible fade show">
<i class="bi bi-check-circle me-2"></i><?= e($msg) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($err): ?>
<div class="alert alert-danger alert-dismissible fade show">
<i class="bi bi-exclamation-triangle me-2"></i><?= e($err) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<div class="row g-3">
<!-- Sol: Müşteri Bilgisi -->
<div class="col-12 col-lg-4">
<!-- Temel Bilgiler -->
<div class="card mb-3">
<div class="card-header d-flex align-items-center justify-content-between">
<span><i class="bi bi-person me-2 text-warning"></i>Temel Bilgiler</span>
<?php if (is_admin()): ?>
<a href="customer_edit.php?id=<?= $id ?>" class="btn btn-outline-warning btn-sm">
<i class="bi bi-pencil"></i>
</a>
<?php endif; ?>
</div>
<div class="card-body">
<div class="mb-2">
<div style="font-size:11px;color:rgba(255,255,255,0.5);margin-bottom:2px">Kayıtlı İsim</div>
<div class="fw-bold"><?= e($customer['full_name']) ?></div>
</div>
<div class="mb-2">
<div style="font-size:11px;color:rgba(255,255,255,0.5);margin-bottom:2px">Telefon</div>
<div><?= e($customer['phone']) ?></div>
</div>
<div class="mb-2">
<div style="font-size:11px;color:rgba(255,255,255,0.5);margin-bottom:2px">Etiket</div>
<div><?= $customer['tag_name'] ? '<span class="badge bg-secondary">'.e($customer['tag_name']).'</span>' : '—' ?></div>
</div>
<div class="mb-2">
<div style="font-size:11px;color:rgba(255,255,255,0.5);margin-bottom:2px">İndirim</div>
<div><?= $customer['discount_rate'] > 0 ? '<span class="badge bg-success">%'.$customer['discount_rate'].'</span>' : '—' ?></div>
</div>
<div>
<div style="font-size:11px;color:rgba(255,255,255,0.5);margin-bottom:2px">Kayıt Tarihi</div>
<div><?= format_date($customer['created_at']) ?></div>
</div>
</div>
</div>
<!-- İsim Değişim Geçmişi -->
<?php if (!empty($parsed_logs)): ?>
<div class="card mb-3">
<div class="card-header">
<i class="bi bi-clock-history me-2 text-warning"></i>İsim Geçmişi
<span class="badge bg-warning text-dark ms-1"><?= count($parsed_logs) ?></span>
</div>
<div class="card-body p-0">
<!-- Zaman çizelgesi -->
<div class="p-3 border-bottom border-secondary" style="font-size:12px">
<?php foreach ($parsed_logs as $l): ?>
<div class="d-flex align-items-center gap-2 mb-2">
<span style="color:rgba(255,255,255,0.55);min-width:80px"><?= e($l['date']) ?></span>
<span style="color:rgba(255,255,255,0.6)"><?= e($l['old_name']) ?></span>
<i class="bi bi-arrow-right text-warning"></i>
<span class="fw-semibold"><?= e($l['new_name']) ?></span>
</div>
<?php endforeach; ?>
</div>
<!-- İsim seç -->
<div class="p-3">
<div style="font-size:11px;color:rgba(255,255,255,0.55);margin-bottom:8px">Kayıtlı ismi seçin:</div>
<form method="POST" action="customer_detail.php?id=<?= $id ?>">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token()) ?>">
<input type="hidden" name="set_name" value="1">
<div class="d-flex flex-wrap gap-2">
<?php foreach ($all_names as $nm): ?>
<button type="submit" name="full_name" value="<?= htmlspecialchars($nm) ?>"
class="btn btn-sm <?= $nm === $customer['full_name'] ? 'btn-warning' : 'btn-outline-secondary' ?>">
<?= e($nm) ?>
<?php if ($nm === $customer['full_name']): ?>
<i class="bi bi-check-lg ms-1"></i>
<?php endif; ?>
</button>
<?php endforeach; ?>
</div>
</form>
</div>
</div>
</div>
<?php endif; ?>
<!-- Admin Notu -->
<div class="card">
<div class="card-header">
<i class="bi bi-sticky me-2 text-warning"></i>Admin Notu
</div>
<div class="card-body">
<form method="POST" action="customer_detail.php?id=<?= $id ?>">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token()) ?>">
<input type="hidden" name="save_note" value="1">
<textarea name="admin_note" class="form-control mb-2" rows="3"
placeholder="Müşteri hakkında not..."><?= e($clean_note) ?></textarea>
<button type="submit" class="btn btn-warning btn-sm w-100">
<i class="bi bi-check-lg me-1"></i>Notu Kaydet
</button>
</form>
</div>
</div>
</div>
<!-- Sağ: Rezervasyon Geçmişi -->
<div class="col-12 col-lg-8">
<div class="card">
<div class="card-header d-flex align-items-center justify-content-between">
<span><i class="bi bi-calendar-check me-2 text-warning"></i>Rezervasyon Geçmişi</span>
<span class="badge bg-secondary"><?= count($reservations) ?> rezervasyon</span>
</div>
<div class="card-body p-0">
<?php if (empty($reservations)): ?>
<div class="text-center py-5" style="color:rgba(255,255,255,0.4)">
<i class="bi bi-calendar-x d-block mb-2" style="font-size:32px"></i>
Henüz rezervasyon yok.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-dark table-hover mb-0">
<thead>
<tr>
<th>Tarih</th>
<th>Saat</th>
<th>Kişi</th>
<th>Durum</th>
<th>İsim</th>
<th>Not</th>
</tr>
</thead>
<tbody>
<?php foreach ($reservations as $r): ?>
<tr>
<td><?= e($r['date']) ?></td>
<td><?= e($r['time']) ?></td>
<td><?= $r['guest_count'] ?></td>
<td>
<span class="badge bg-<?= $status_badge[$r['status']] ?? 'secondary' ?>">
<?= $status_label[$r['status']] ?? e($r['status']) ?>
</span>
</td>
<td style="font-size:12px">
<?= e($r['full_name']) ?>
<?php if ($r['full_name'] !== $customer['full_name']): ?>
<i class="bi bi-exclamation-circle text-warning ms-1" style="font-size:11px"
title="Kayıtlı isimden farklı"></i>
<?php endif; ?>
</td>
<td class="text-muted" style="font-size:11px">
<?= e(mb_strimwidth($r['special_request'] ?? '', 0, 30, '...')) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/../includes/admin_layout_end.php'; ?>