<?php
$data = is_array($movementData ?? null) ? $movementData : ['site'=>'','version'=>[],'rows'=>[],'total'=>0,'page'=>1,'per_page'=>50];
$version = is_array($data['version'] ?? null) ? $data['version'] : [];
$rows = is_array($data['rows'] ?? null) ? $data['rows'] : [];
$total = (int)($data['total'] ?? 0); $page=max(1,(int)($data['page']??1)); $perPage=max(20,(int)($data['per_page']??50)); $totalPages=max(1,(int)ceil($total/$perPage));
$isAr=current_lang()==='ar'; $tr=static function(string $en,string $ar)use($isAr):string{return $isAr?$ar:$en;};
$effectLabels=['0'=>$tr('Neutral','محايد'),'1'=>$tr('Inbound','وارد'),'2'=>$tr('Outbound','صادر')];
$statusLabels=['SOURCE_ONLY'=>$tr('Recorded','مسجل'),'CLASSIFIED'=>$tr('Classified','مصنف'),'RETIRED'=>$tr('Inactive','غير فعال')];
$isDeveloper=(new \App\Services\DiarAccessPolicy())->isDeveloper();
?>
<section class="diar-command-page diar-production-r273"><?php include __DIR__.'/_tabs.php'; ?><?php include __DIR__.'/_context.php'; ?>
<header class="diar-subpage-hero"><div><span class="eyebrow"><?= e($tr('MEDICATION MOVEMENT TYPES','أنواع حركات الأدوية')) ?></span><h1><?= e($tr('Medication movement types','أنواع حركات الأدوية')) ?></h1><p><?= e($tr('Review the approved names and inventory direction of recorded medication movements.','مراجعة الأسماء المعتمدة واتجاه المخزون لحركات الأدوية المسجلة.')) ?></p></div><a class="hero-action" href="<?= e(url_to('diar-command-center')) ?>"><?= e($tr('Back to overview','العودة إلى النظرة العامة')) ?></a></header>
<div class="diar-command-alert info"><strong><?= e($tr('About these results','حول هذه النتائج')) ?></strong><span><?= e($tr('The recorded movement code remains available for traceability while business names are used for day-to-day review.','يبقى كود الحركة المسجل متاحًا للتتبع، مع استخدام الاسم التشغيلي في المراجعة اليومية.')) ?></span></div>
<?php if(!empty($loadError)):?><div class="diar-command-alert error"><?= e($loadError) ?></div><?php endif;?>
<div class="dictionary-version-card"><div><span><?= e($tr('Hospital','المستشفى')) ?></span><strong><?= e((string)($data['site']??'')) ?></strong></div><div><span><?= e($tr('Active reference','المرجع الفعال')) ?></span><strong><?= e((string)($version['move_map_version_id']??'—')) ?></strong></div><div><span><?= e($tr('Movement types','أنواع الحركات')) ?></span><strong><?= number_format((int)($version['source_type_count']??0)) ?></strong></div><div><span><?= e($tr('Last updated','آخر تحديث')) ?></span><strong><?= e((string)($version['captured_at']??'—')) ?></strong></div><?php if($isDeveloper&&!empty($version['map_hash'])):?><details class="diar-technical-details wide"><summary><?= e($tr('Technical integrity details','تفاصيل السلامة الفنية')) ?></summary><code><?= e((string)$version['map_hash']) ?></code></details><?php endif;?></div>
<form class="diar-command-filters" method="get" action="index.php"><input type="hidden" name="page" value="diar-movement-dictionary"><input type="hidden" name="lang" value="<?= e(current_lang()) ?>"><div class="field search-wide"><label><?= e($tr('Search movement','البحث في الحركات')) ?></label><input name="q" value="<?= e((string)($filters['q']??'')) ?>" placeholder="<?= e($tr('Code or movement name','الكود أو اسم الحركة')) ?>"></div><div class="field"><label><?= e($tr('Hospital','المستشفى')) ?></label><select name="site"><?php foreach($allowedSites as $site):?><option value="<?= e($site) ?>" <?= (string)($filters['site']??'')===$site?'selected':'' ?>><?= e($site) ?></option><?php endforeach;?></select></div><div class="field"><label><?= e($tr('Inventory effect','أثر الحركة على المخزون')) ?></label><select name="effect"><option value=""><?= e($tr('All effects','كل التأثيرات')) ?></option><?php foreach($effectLabels as $code=>$label):?><option value="<?= e($code) ?>" <?= (string)($filters['effect']??'')===$code?'selected':'' ?>><?= e($label) ?></option><?php endforeach;?></select></div><div class="field"><label><?= e($tr('Status','الحالة')) ?></label><select name="mapping_status"><option value=""><?= e($tr('All statuses','كل الحالات')) ?></option><?php foreach($statusLabels as $code=>$label):?><option value="<?= e($code) ?>" <?= (string)($filters['mapping_status']??'')===$code?'selected':'' ?>><?= e($label) ?></option><?php endforeach;?></select></div><div class="filter-actions"><button class="primary-button" type="submit"><?= e($tr('Apply filters','تطبيق الفلاتر')) ?></button><a class="secondary-button" href="<?= e(url_to('diar-movement-dictionary',['site'=>(string)($data['site']??'')])) ?>"><?= e($tr('Reset','إعادة ضبط')) ?></a></div></form>
<section class="diar-command-section"><div class="section-heading"><div><h2><?= e($tr('Movement types','أنواع الحركات')) ?></h2><p><?= e($tr('Operational names, inventory direction and current status.','الأسماء التشغيلية واتجاه المخزون والحالة الحالية.')) ?></p></div><strong><?= number_format($total) ?></strong></div><?php if($rows===[]):?><div class="empty-state"><?= e($tr('No movement types match the selected filters.','لا توجد أنواع حركة تطابق الفلاتر المحددة.')) ?></div><?php else:?><div class="command-table-wrap"><table class="command-table movement-table"><thead><tr><th><?= e($tr('Code','الكود')) ?></th><th><?= e($tr('Movement name','اسم الحركة')) ?></th><th><?= e($tr('Inventory effect','أثر المخزون')) ?></th><th><?= e($tr('Direction','الاتجاه')) ?></th><th><?= e($tr('Status','الحالة')) ?></th><?php if($isDeveloper):?><th><?= e($tr('Technical details','تفاصيل فنية')) ?></th><?php endif;?></tr></thead><tbody><?php foreach($rows as $row):$effect=(string)($row['storetranseffect']??'');$mapping=(string)($row['mapping_status']??'SOURCE_ONLY');$name=$isAr?(string)($row['trans_type_ar']??''):(string)($row['trans_type_en']??'');?><tr><td><strong class="movement-code"><?= e((string)($row['storetranscode']??'')) ?></strong></td><td><strong><?= e($name!==''?$name:$tr('Unnamed movement','حركة بلا اسم')) ?></strong></td><td><span class="effect-badge effect-<?= e($effect) ?>"><?= e($effectLabels[$effect]??$effect) ?></span></td><td><strong><?= e((string)($row['direction_code']??'—')) ?></strong></td><td><span class="mapping-badge mapping-<?= e(strtolower($mapping)) ?>"><?= e($statusLabels[$mapping]??$mapping) ?></span></td><?php if($isDeveloper):?><td><details class="diar-technical-details"><summary><?= e($tr('Show','عرض')) ?></summary><small><?= e((string)($row['movement_group_code']??'')) ?> · Effect <?= e($effect) ?></small><?php if(!empty($row['source_row_hash'])):?><code><?= e((string)$row['source_row_hash']) ?></code><?php endif;?><small><?= e((string)($row['updated_at']??'')) ?></small></details></td><?php endif;?></tr><?php endforeach;?></tbody></table></div><?php endif;?><?php if($totalPages>1):?><nav class="pagination"><?php $base=$filters;$base['per_page']=$perPage;if($page>1):$q=$base;$q['p']=$page-1;?><a href="<?= e(url_to('diar-movement-dictionary',$q)) ?>"><?= e($tr('Previous','السابق')) ?></a><?php endif;?><span><?= e($tr('Page','صفحة')) ?> <?= number_format($page) ?> / <?= number_format($totalPages) ?></span><?php if($page<$totalPages):$q=$base;$q['p']=$page+1;?><a href="<?= e(url_to('diar-movement-dictionary',$q)) ?>"><?= e($tr('Next','التالي')) ?></a><?php endif;?></nav><?php endif;?></section></section>
