<?php
$valid = "status != 'iptal'";
$today = db_one("SELECT COUNT(*) AS orders, COALESCE(SUM(total), 0) AS revenue FROM orders WHERE $valid AND DATE(created_at) = CURDATE()");
$month = db_one("SELECT COUNT(*) AS orders, COALESCE(SUM(total), 0) AS revenue FROM orders WHERE $valid AND created_at >= DATE_FORMAT(CURDATE(), '%Y-%m-01')");
$last_month = (float) db_value("SELECT COALESCE(SUM(total), 0) FROM orders WHERE $valid AND created_at >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, '%Y-%m-01') AND created_at < DATE_FORMAT(CURDATE(), '%Y-%m-01')");
$pending = (int) db_value("SELECT COUNT(*) FROM orders WHERE status = 'beklemede'");
$customers = (int) db_value('SELECT COUNT(*) FROM customers');
$new_customers = (int) db_value('SELECT COUNT(*) FROM customers WHERE created_at >= CURDATE() - INTERVAL 30 DAY');
$daily = array_column(db_all("SELECT DATE(created_at) AS day, SUM(total) AS total FROM orders WHERE $valid AND created_at >= CURDATE() - INTERVAL 13 DAY GROUP BY DATE(created_at)"), 'total', 'day');
$chart = [];
for ($i = 13; $i >= 0; $i--) {
$day = date('Y-m-d', strtotime("-$i days"));
$chart[$day] = (float) ($daily[$day] ?? 0);
}
$chart_max = max(1, max($chart));
$orders = db_all('SELECT * FROM orders ORDER BY id DESC LIMIT 6');
$low_stock = db_all('SELECT * FROM products WHERE stock <= 5 AND status = 1 ORDER BY stock ASC, name LIMIT 6');
$top = db_all('SELECT * FROM products WHERE sold > 0 ORDER BY sold DESC LIMIT 5');
$reviews = pending_review_count();
$change = $last_month > 0 ? round(($month['revenue'] - $last_month) / $last_month * 100) : null;
admin_header('Panel', '<a href="' . admin_url('urun') . '" class="btn-primary btn-sm">' . icon('plus', 'size-4', '2') . 'Yeni ürün</a>');
?>
<p class="text-sm text-zinc-500">Merhaba <?= e(current_admin()['name']) ?>, bugün mağazanda olanlar.</p>
<div class="mt-6 grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
<div class="card p-5">
<p class="flex items-center gap-2 text-[13px] text-zinc-500"><?= icon('banknote', 'size-4') ?>Bu ay ciro</p>
<p class="font-display mt-3 text-3xl font-semibold tracking-tight"><?= money($month['revenue']) ?></p>
<p class="mt-2 text-xs <?= $change === null ? 'text-zinc-400' : ($change >= 0 ? 'text-emerald-600' : 'text-rose-600') ?>">
<?= $change === null ? 'Geçen ay verisi yok' : ($change >= 0 ? '↑ %' . $change : '↓ %' . abs($change)) . ' geçen aya göre' ?>
</p>
</div>
<div class="card p-5">
<p class="flex items-center gap-2 text-[13px] text-zinc-500"><?= icon('cart', 'size-4') ?>Bugün</p>
<p class="font-display mt-3 text-3xl font-semibold tracking-tight"><?= (int) $today['orders'] ?> <span class="text-base font-medium text-zinc-400">sipariş</span></p>
<p class="mt-2 text-xs text-zinc-500"><?= money($today['revenue']) ?> ciro</p>
</div>
<a href="<?= admin_url('siparisler?durum=beklemede') ?>" class="card p-5 transition hover:border-zinc-300">
<p class="flex items-center gap-2 text-[13px] text-zinc-500"><?= icon('clock', 'size-4') ?>Bekleyen sipariş</p>
<p class="font-display mt-3 text-3xl font-semibold tracking-tight <?= $pending ? 'text-accent-600' : '' ?>"><?= $pending ?></p>
<p class="mt-2 text-xs text-zinc-500">Onay bekliyor</p>
</a>
<a href="<?= admin_url('musteriler') ?>" class="card p-5 transition hover:border-zinc-300">
<p class="flex items-center gap-2 text-[13px] text-zinc-500"><?= icon('users', 'size-4') ?>Müşteri</p>
<p class="font-display mt-3 text-3xl font-semibold tracking-tight"><?= $customers ?></p>
<p class="mt-2 text-xs text-zinc-500">Son 30 günde +<?= $new_customers ?></p>
</a>
</div>
<div class="mt-6 grid gap-6 xl:grid-cols-3">
<div class="card p-6 xl:col-span-2">
<div class="flex items-center justify-between">
<h2 class="font-semibold">Son 14 gün satış</h2>
<span class="text-sm text-zinc-500"><?= money(array_sum($chart)) ?></span>
</div>
<div class="mt-8 flex h-48 items-end gap-1.5 sm:gap-2.5">
<?php foreach ($chart as $day => $total): ?>
<div class="group relative flex h-full flex-1 flex-col justify-end">
<div class="pointer-events-none absolute -top-8 left-1/2 z-10 hidden -translate-x-1/2 rounded-md bg-ink px-2 py-1 text-[11px] whitespace-nowrap text-white group-hover:block"><?= money($total) ?></div>
<div class="w-full rounded-md transition <?= $total > 0 ? 'bg-ink group-hover:bg-accent-600' : 'bg-zinc-100' ?>" style="height: <?= max(3, round($total / $chart_max * 100)) ?>%"></div>
<span class="mt-2 text-center text-[10px] text-zinc-400"><?= date('d', strtotime($day)) ?></span>
</div>
<?php endforeach ?>
</div>
</div>
<div class="card">
<div class="flex items-center justify-between border-b border-zinc-100 px-5 py-4">
<h2 class="font-semibold">Çok satanlar</h2>
</div>
<ul class="divide-y divide-zinc-100">
<?php foreach ($top as $i => $product): ?>
<li class="flex items-center gap-3 px-5 py-3">
<span class="w-4 text-xs font-semibold text-zinc-400"><?= $i + 1 ?></span>
<span class="size-9 shrink-0 overflow-hidden rounded-lg bg-zinc-100"><?= product_image($product, 'h-full w-full object-cover', 'text-sm') ?></span>
<a href="<?= admin_url('urun?id=' . $product['id']) ?>" class="min-w-0 flex-1 truncate text-sm font-medium hover:underline"><?= e($product['name']) ?></a>
<span class="text-xs text-zinc-500"><?= (int) $product['sold'] ?> adet</span>
</li>
<?php endforeach ?>
<?php if (!$top): ?>
<li class="px-5 py-10 text-center text-sm text-zinc-500">Henüz satış yok.</li>
<?php endif ?>
</ul>
</div>
</div>
<div class="mt-6 grid gap-6 xl:grid-cols-3">
<div class="card overflow-hidden xl:col-span-2">
<div class="flex items-center justify-between border-b border-zinc-100 px-5 py-4">
<h2 class="font-semibold">Son siparişler</h2>
<a href="<?= admin_url('siparisler') ?>" class="text-sm text-zinc-500 hover:text-ink">Tümü →</a>
</div>
<div class="overflow-x-auto">
<table class="w-full min-w-[560px] text-sm">
<tbody class="divide-y divide-zinc-100">
<?php foreach ($orders as $order): ?>
<tr class="hover:bg-zinc-50">
<td class="px-5 py-3"><a href="<?= admin_url('siparis?id=' . $order['id']) ?>" class="font-semibold hover:underline">#<?= e($order['order_no']) ?></a></td>
<td class="px-5 py-3 text-zinc-600"><?= e($order['name']) ?></td>
<td class="px-5 py-3 text-zinc-500"><?= date_tr($order['created_at'], true) ?></td>
<td class="px-5 py-3"><?= order_status_badge($order['status']) ?></td>
<td class="px-5 py-3 text-right font-semibold"><?= money($order['total']) ?></td>
</tr>
<?php endforeach ?>
<?php if (!$orders): ?>
<tr><td class="px-5 py-12 text-center text-zinc-500">Henüz sipariş yok.</td></tr>
<?php endif ?>
</tbody>
</table>
</div>
</div>
<div class="space-y-6">
<?php if ($reviews): ?>
<a href="<?= admin_url('yorumlar') ?>" class="card flex items-center gap-4 p-5 transition hover:border-zinc-300">
<span class="flex size-10 items-center justify-center rounded-full bg-amber-50 text-amber-600"><?= icon('chat', 'size-5') ?></span>
<span class="flex-1 text-sm"><strong class="font-semibold"><?= $reviews ?> yorum</strong> onay bekliyor</span>
<?= icon('right', 'size-4 text-zinc-400', '2') ?>
</a>
<?php endif ?>
<div class="card">
<div class="border-b border-zinc-100 px-5 py-4">
<h2 class="font-semibold">Stoğu azalanlar</h2>
</div>
<ul class="divide-y divide-zinc-100">
<?php foreach ($low_stock as $product): ?>
<li>
<a href="<?= admin_url('urun?id=' . $product['id']) ?>" class="flex items-center justify-between gap-3 px-5 py-3 text-sm hover:bg-zinc-50">
<span class="truncate"><?= e($product['name']) ?></span>
<span class="shrink-0 rounded-full px-2 py-0.5 text-xs font-semibold <?= $product['stock'] < 1 ? 'bg-rose-50 text-rose-700' : 'bg-amber-50 text-amber-700' ?>"><?= $product['stock'] < 1 ? 'Tükendi' : (int) $product['stock'] . ' adet' ?></span>
</a>
</li>
<?php endforeach ?>
<?php if (!$low_stock): ?>
<li class="px-5 py-10 text-center text-sm text-zinc-500">Stok sorunu yok.</li>
<?php endif ?>
</ul>
</div>
</div>
</div>
<?php admin_footer() ?>
Lisans bilgisi projenin LICENSE dosyasında.