116 lines
4.1 KiB
PHP
116 lines
4.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
ensure_tables();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$slugInput = trim($_POST['slug'] ?? '');
|
|
$email = trim($_POST['contact_email'] ?? '');
|
|
$currency = strtoupper(trim($_POST['currency'] ?? 'USD'));
|
|
|
|
if ($name === '') {
|
|
flash_set('error', 'Store name is required.');
|
|
} elseif (!preg_match('/^[A-Z]{3}$/', $currency)) {
|
|
flash_set('error', 'Currency must be a 3-letter ISO code.');
|
|
} else {
|
|
$slug = $slugInput !== '' ? slugify($slugInput) : slugify($name);
|
|
$stmt = db()->prepare(
|
|
"INSERT INTO stores (name, slug, contact_email, currency, created_at)
|
|
VALUES (:name, :slug, :email, :currency, :created_at)"
|
|
);
|
|
$stmt->bindValue(':name', $name);
|
|
$stmt->bindValue(':slug', $slug);
|
|
$stmt->bindValue(':email', $email !== '' ? $email : null);
|
|
$stmt->bindValue(':currency', $currency);
|
|
$stmt->bindValue(':created_at', date('Y-m-d H:i:s'));
|
|
$stmt->execute();
|
|
flash_set('success', 'Store created successfully.');
|
|
}
|
|
|
|
header('Location: /stores.php');
|
|
exit;
|
|
}
|
|
|
|
$stores = get_stores();
|
|
|
|
render_header('Store Directory', 'Create and manage tenant stores with isolated catalogs.');
|
|
?>
|
|
|
|
<section class="row g-4">
|
|
<div class="col-12 col-lg-4">
|
|
<div class="content-card h-100">
|
|
<h4 class="mb-3">Create a store</h4>
|
|
<form method="post" class="vstack gap-3">
|
|
<div>
|
|
<label class="form-label">Store name</label>
|
|
<input type="text" name="name" class="form-control" placeholder="Atlas Home Goods" required>
|
|
</div>
|
|
<div>
|
|
<label class="form-label">Slug</label>
|
|
<input type="text" name="slug" class="form-control" placeholder="atlas-home-goods">
|
|
</div>
|
|
<div>
|
|
<label class="form-label">Contact email</label>
|
|
<input type="email" name="contact_email" class="form-control" placeholder="ops@store.com">
|
|
</div>
|
|
<div>
|
|
<label class="form-label">Currency</label>
|
|
<input type="text" name="currency" class="form-control" value="USD" maxlength="3">
|
|
</div>
|
|
<button class="btn btn-dark w-100" type="submit">Create store</button>
|
|
</form>
|
|
<p class="text-muted small mt-3 mb-0">Each store has its own catalog, inventory, and future order pipeline.</p>
|
|
</div>
|
|
</div>
|
|
<div class="col-12 col-lg-8">
|
|
<div class="content-card">
|
|
<div class="d-flex align-items-center justify-content-between mb-3">
|
|
<div>
|
|
<h4 class="mb-1">Stores</h4>
|
|
<p class="text-muted small mb-0">Multi-tenant roster with catalog counts.</p>
|
|
</div>
|
|
<span class="badge text-bg-light border"><?= h((string)count($stores)) ?> total</span>
|
|
</div>
|
|
|
|
<?php if (empty($stores)): ?>
|
|
<div class="empty-state">
|
|
<p class="mb-2">No stores added yet.</p>
|
|
<p class="text-muted small mb-0">Create the first store to unlock product management.</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table align-middle">
|
|
<thead>
|
|
<tr>
|
|
<th>Store</th>
|
|
<th>Slug</th>
|
|
<th>Contact</th>
|
|
<th class="text-end">Products</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($stores as $store): ?>
|
|
<tr>
|
|
<td>
|
|
<a class="link-dark" href="/store.php?id=<?= h((string)$store['id']) ?>">
|
|
<?= h($store['name']) ?>
|
|
</a>
|
|
</td>
|
|
<td class="text-muted"><?= h($store['slug']) ?></td>
|
|
<td><?= h($store['contact_email'] ?? '—') ?></td>
|
|
<td class="text-end"><?= h((string)$store['product_count']) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<?php render_footer(); ?>
|