<?php
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/stone_core_common.php';
require_login();
require_role(['admin']);

$page_title = 'Tedarikçiler';
$active_menu = 'suppliers';

$user = current_user();
$currencies = stone_currency_options();
if (is_post()) {
    verify_csrf();
    $action = $_POST['action'] ?? '';
    $currency = $_POST['default_currency'] ?? 'USD';
    if (!in_array($currency, $currencies, true)) $currency = 'USD';
    try {
        if ($action === 'create') {
            $stmt = db()->prepare("INSERT INTO suppliers (supplier_name, contact_name, phone, email, default_currency, notes, created_by) VALUES (?,?,?,?,?,?,?)");
            $stmt->execute([trim($_POST['supplier_name']??''), trim($_POST['contact_name']??''), trim($_POST['phone']??''), trim($_POST['email']??''), $currency, trim($_POST['notes']??''), $user['id']]);
            $_SESSION['flash'] = ['type'=>'success','msg'=>'Tedarikçi eklendi.'];
        } elseif ($action === 'update') {
            $stmt = db()->prepare("UPDATE suppliers SET supplier_name=?, contact_name=?, phone=?, email=?, default_currency=?, notes=?, is_active=?, updated_at=NOW() WHERE id=?");
            $stmt->execute([trim($_POST['supplier_name']??''), trim($_POST['contact_name']??''), trim($_POST['phone']??''), trim($_POST['email']??''), $currency, trim($_POST['notes']??''), isset($_POST['is_active'])?1:0, (int)$_POST['id']]);
            $_SESSION['flash'] = ['type'=>'success','msg'=>'Tedarikçi bilgileri güncellendi.'];
        }
    } catch (Throwable $e) {
        $_SESSION['flash'] = ['type'=>'error','msg'=>'İşlem yapılamadı: '.$e->getMessage()];
    }
    redirect('suppliers.php');
}
$rows = db()->query("SELECT * FROM suppliers ORDER BY is_active DESC, supplier_name")->fetchAll();
include __DIR__ . '/includes/layout.php';
?>
<div class="ph"><div><h1>Tedarikçiler</h1><p>Taş/aksesuar alımı için tedarikçi ve cari kartları.</p></div></div>
<div class="card"><div class="card-header"><span class="card-title">Yeni Tedarikçi</span></div><div class="card-body">
<form method="post" style="display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;align-items:end">
<?= csrf_field() ?><input type="hidden" name="action" value="create">
<div><label class="form-label">Adı</label><input class="form-control" name="supplier_name" required></div>
<div><label class="form-label">Yetkili</label><input class="form-control" name="contact_name"></div>
<div><label class="form-label">Para Birimi</label><select class="form-control" name="default_currency"><?php foreach($currencies as $c): ?><option value="<?= e($c) ?>"><?= e($c) ?></option><?php endforeach; ?></select></div>
<div><label class="form-label">Telefon</label><input class="form-control" name="phone"></div>
<div><label class="form-label">E-posta</label><input class="form-control" name="email"></div>
<div><button class="btn btn-primary" type="submit">Ekle</button></div>
<div style="grid-column:1/-1"><label class="form-label">Not</label><input class="form-control" name="notes"></div>
</form>
</div></div>
<div class="card"><div class="card-header"><span class="card-title">Tedarikçi Listesi</span><span class="c-mt">Satırdaki “Bilgileri Kaydet” butonu tedarikçi ad/yetkili/tel/para birimi düzeltmesi içindir. Cari için “Cari Kart”a gir.</span></div><div class="table-wrap"><table><thead><tr><th>ID</th><th>Ad</th><th>Yetkili</th><th>Tel</th><th>E-posta</th><th>Para</th><th>Aktif</th><th>İşlem</th></tr></thead><tbody>
<?php foreach($rows as $r): ?>
<tr><form method="post"><?= csrf_field() ?><input type="hidden" name="action" value="update"><input type="hidden" name="id" value="<?= (int)$r['id'] ?>">
<td><?= (int)$r['id'] ?></td><td><input class="form-control" name="supplier_name" value="<?= e($r['supplier_name']) ?>"></td><td><input class="form-control" name="contact_name" value="<?= e($r['contact_name']) ?>"></td><td><input class="form-control" name="phone" value="<?= e($r['phone']) ?>"></td><td><input class="form-control" name="email" value="<?= e($r['email']) ?>"></td><td><select class="form-control" name="default_currency" style="width:85px"><?php foreach($currencies as $c): ?><option value="<?= e($c) ?>" <?= $r['default_currency']===$c?'selected':'' ?>><?= e($c) ?></option><?php endforeach; ?></select></td><td><input type="checkbox" name="is_active" <?= (int)$r['is_active']?'checked':'' ?>></td><td><div style="display:flex;gap:6px"><button class="btn btn-secondary" type="submit">Bilgileri Kaydet</button><a class="btn btn-primary" href="supplier_detail.php?id=<?= (int)$r['id'] ?>">Cari Kart</a></div></td>
</form></tr>
<?php endforeach; ?>
</tbody></table></div></div>
<?php include __DIR__ . '/includes/layout_end.php'; ?>
