<?php
$customer = current_customer();
$form = ['name' => post('name', $customer['name']), 'email' => post('email', $customer['email']), 'phone' => post('phone', $customer['phone'] ?? '')];

if (is_post()) {
    csrf_check();

    if (post('action') === 'password') {
        if (!password_verify(post('current', ''), $customer['password'])) {
            reply(false, 'Mevcut şifreniz hatalı.');
        }
        if (mb_strlen(post('password', '')) < 8) {
            reply(false, 'Yeni şifre en az 8 karakter olmalı.');
        }
        db_update('customers', ['password' => password_hash(post('password'), PASSWORD_DEFAULT)], $customer['id']);
        reply(true, 'Şifreniz güncellendi.', ['reset' => true]);
    }

    $error = match (true) {
        $form['name'] === ''                               => 'Ad soyad boş olamaz.',
        !filter_var($form['email'], FILTER_VALIDATE_EMAIL) => 'Geçerli bir e-posta girin.',
        email_taken($form['email'], $customer['id'])       => 'Bu e-posta başka bir hesapta kullanılıyor.',
        default                                            => null,
    };
    if ($error) {
        reply(false, $error);
    }

    db_update('customers', $form, $customer['id']);
    reply(true, 'Bilgileriniz güncellendi.');
}

$stats = db_one('SELECT COUNT(*) AS orders, COALESCE(SUM(total), 0) AS spent FROM orders WHERE customer_id = ? AND status != ?', [$customer['id'], 'iptal']);

account_header('Hesap bilgilerim');
?>

<div class="grid gap-4 sm:grid-cols-3">
    <div class="rounded-2xl border border-zinc-200 p-5">
        <p class="text-sm text-zinc-500">Siparişlerim</p>
        <p class="font-display mt-2 text-3xl font-semibold tracking-tight"><?= (int) $stats['orders'] ?></p>
    </div>
    <div class="rounded-2xl border border-zinc-200 p-5">
        <p class="text-sm text-zinc-500">Toplam alışveriş</p>
        <p class="font-display mt-2 text-3xl font-semibold tracking-tight"><?= money($stats['spent']) ?></p>
    </div>
    <div class="rounded-2xl border border-zinc-200 p-5">
        <p class="text-sm text-zinc-500">Favorilerim</p>
        <p class="font-display mt-2 text-3xl font-semibold tracking-tight"><?= favorite_count() ?></p>
    </div>
</div>

<div class="mt-10 grid gap-8 xl:grid-cols-2">
    <form method="post" action="<?= url('hesabim') ?>" data-ajax class="space-y-5 rounded-3xl border border-zinc-200 p-6 sm:p-8">
        <?= csrf_field() ?>
        <h2 class="text-lg font-semibold text-ink">Kişisel bilgiler</h2>
        <div>
            <label class="label">Ad soyad</label>
            <input name="name" value="<?= e($form['name']) ?>" class="input" required>
        </div>
        <div>
            <label class="label">E-posta</label>
            <input name="email" type="email" value="<?= e($form['email']) ?>" class="input" required>
        </div>
        <div>
            <label class="label">Telefon</label>
            <input name="phone" type="tel" value="<?= e($form['phone']) ?>" class="input">
        </div>
        <button class="btn-primary">Kaydet</button>
    </form>

    <form method="post" action="<?= url('hesabim') ?>" data-ajax class="space-y-5 rounded-3xl border border-zinc-200 p-6 sm:p-8">
        <?= csrf_field() ?>
        <input type="hidden" name="action" value="password">
        <h2 class="text-lg font-semibold text-ink">Şifre değiştir</h2>
        <div>
            <label class="label">Mevcut şifre</label>
            <input name="current" type="password" class="input" autocomplete="current-password" required>
        </div>
        <div>
            <label class="label">Yeni şifre</label>
            <input name="password" type="password" minlength="8" class="input" autocomplete="new-password" required>
        </div>
        <button class="btn-light">Şifreyi güncelle</button>
    </form>
</div>

<?php account_footer() ?>
