<?php
declare(strict_types=1);

if (!function_exists('owner_label')) {
function owner_label(string $type, int $id): string {
  if ($type === 'oasis') {
    $r = db_one("SELECT code,name FROM oases WHERE id=?", [$id]);
    return $r ? ("واحة: " . $r['name'] . " (" . $r['code'] . ")") : "واحة: #{$id}";
  }
  if ($type === 'falaj') {
    $r = db_one("SELECT code,name FROM aflaj WHERE id=?", [$id]);
    return $r ? ("فلج: " . $r['name'] . " (" . $r['code'] . ")") : "فلج: #{$id}";
  }
  if ($type === 'farm') {
    $r = db_one("SELECT code,owner_name FROM farms WHERE id=?", [$id]);
    $nm = $r['owner_name'] ?? '';
    return $r ? ("مزرعة: " . ($nm ?: 'بدون مالك') . " (" . $r['code'] . ")") : "مزرعة: #{$id}";
  }
  return "{$type}: #{$id}";
}
}

if (!function_exists('status_tag')) {
function status_tag(string $status): string {
  $map = [
    'active' => ['ok', 'نشطة'],
    'inactive' => ['danger', 'غير نشطة'],
    'under_maintenance' => ['warn', 'تحت الصيانة'],

    'operational' => ['ok', 'عاملة'],
    'partial' => ['warn', 'جزئية'],
    'stopped' => ['danger', 'متوقفة'],
    'under_maintenance2' => ['warn', 'تحت الصيانة'],

    'ok' => ['ok', 'سليم'],
    'needs_service' => ['warn', 'بحاجة صيانة'],
    'out_of_order' => ['danger', 'متعطل'],

    'open' => ['danger', 'مفتوح'],
    'assigned' => ['warn', 'مُسند'],
    'in_progress' => ['warn', 'قيد التنفيذ'],
    'done' => ['ok', 'مُنجز'],
    'cancelled' => ['danger', 'ملغي'],
  ];
  $x = $map[$status] ?? ['tag', $status];
  return '<span class="tag '.$x[0].'">'.e($x[1]).'</span>';
}
}
