89 lines
3.7 KiB
PHP
89 lines
3.7 KiB
PHP
<?php
|
||
// Admin/fuel/save.php — إضافة محطة أو تعديلها
|
||
// POST: station_id (اختياري للتعديل), name_ar, chain_name, city, address,
|
||
// lat, lng, phone, discount_percent, is_active
|
||
|
||
require_once __DIR__ . '/../../connect.php';
|
||
|
||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||
jsonError('Unauthorized: Admin access required', 403);
|
||
}
|
||
|
||
$stationId = (int) (filterRequest('station_id', 'int') ?: 0);
|
||
$nameAr = filterRequest('name_ar');
|
||
|
||
if (empty($nameAr)) {
|
||
jsonError('اسم المحطة مطلوب');
|
||
}
|
||
|
||
$discount = (float) (filterRequest('discount_percent', 'numeric') ?: 0);
|
||
if ($discount < 0 || $discount > 100) {
|
||
jsonError('نسبة الخصم يجب أن تكون بين 0 و 100');
|
||
}
|
||
|
||
$lat = filterRequest('lat', 'numeric');
|
||
$lng = filterRequest('lng', 'numeric');
|
||
|
||
$fields = [
|
||
'name_ar' => $nameAr,
|
||
'chain_name' => filterRequest('chain_name'),
|
||
'city' => filterRequest('city'),
|
||
'address' => filterRequest('address'),
|
||
'lat' => $lat !== null ? (float) $lat : null,
|
||
'lng' => $lng !== null ? (float) $lng : null,
|
||
'phone' => filterRequest('phone'),
|
||
'discount_percent' => $discount,
|
||
'is_active' => filterRequest('is_active') === '0' ? 0 : 1,
|
||
];
|
||
|
||
try {
|
||
if ($stationId > 0) {
|
||
$sets = implode(', ', array_map(static fn($k) => "`$k` = ?", array_keys($fields)));
|
||
|
||
$st = $con->prepare("UPDATE fuel_stations SET $sets WHERE id = ?");
|
||
$st->execute([...array_values($fields), $stationId]);
|
||
|
||
if ($st->rowCount() === 0) {
|
||
// rowCount صفر قد يعني «غير موجودة» وقد يعني «حُفظت بلا تغيير».
|
||
// نفرّق بينهما بقراءة صريحة: إخبار المستخدم أن المحطة غير
|
||
// موجودة بينما هي موجودة يدفعه لإنشاء نسخة ثانية منها.
|
||
$chk = $con->prepare("SELECT 1 FROM fuel_stations WHERE id = ? LIMIT 1");
|
||
$chk->execute([$stationId]);
|
||
if (!$chk->fetchColumn()) {
|
||
jsonError('المحطة غير موجودة', 404);
|
||
}
|
||
}
|
||
|
||
error_log("[admin/fuel] عُدّلت المحطة #$stationId بواسطة $user_id");
|
||
|
||
jsonSuccess(['station_id' => $stationId, 'created' => false], 'تم حفظ التعديلات');
|
||
}
|
||
|
||
// ── إنشاء جديد ──
|
||
// المفتاح يُولَّد هنا ولا يُطلب من المستخدم: مفتاح يختاره إنسان يكون
|
||
// قصيراً ومتوقَّعاً، وهذا المفتاح يأذن بصرف مال.
|
||
$plainKey = bin2hex(random_bytes(24));
|
||
|
||
$cols = array_keys($fields);
|
||
$st = $con->prepare(
|
||
'INSERT INTO fuel_stations (`' . implode('`, `', $cols) . '`, `api_key_hash`) '
|
||
. 'VALUES (' . implode(', ', array_fill(0, count($cols), '?')) . ', ?)'
|
||
);
|
||
$st->execute([...array_values($fields), hash('sha256', $plainKey)]);
|
||
|
||
$stationId = (int) $con->lastInsertId();
|
||
|
||
error_log("[admin/fuel] أُنشئت المحطة #$stationId بواسطة $user_id");
|
||
|
||
jsonSuccess([
|
||
'station_id' => $stationId,
|
||
'created' => true,
|
||
// المرة **الوحيدة** التي يظهر فيها المفتاح. مخزَّن مُهشَّراً، فلا
|
||
// سبيل لاستعادته لاحقاً — من يفقده يدوّر مفتاحاً جديداً.
|
||
'api_key' => $plainKey,
|
||
], 'أُنشئت المحطة. انسخ المفتاح الآن — لن يظهر مرة أخرى.');
|
||
} catch (PDOException $e) {
|
||
error_log('[admin/fuel/save] ' . $e->getMessage());
|
||
jsonError('Server error', 500);
|
||
}
|