<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>محرك دورات العمل<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<form method="POST" action="/workflows/process-timeouts" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-outline" onclick="return confirm('هل تريد معالجة الانتقالات التلقائية المنتهية؟')">⏱ معالجة المهلات المنتهية</button>
</form>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<!-- Workflow Definitions Overview -->
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(300px, 1fr));gap:20px;margin-bottom:30px;">
    <?php foreach ($definitions as $def): ?>
    <?php $s = $stats[$def['workflow_code']] ?? ['active' => 0, 'completed' => 0]; ?>
    <div class="card" style="padding:20px;">
        <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:15px;">
            <div>
                <h3 style="margin:0;color:#1A1A2E;"><?= e($def['name_ar']) ?></h3>
                <code style="font-size:11px;color:#9CA3AF;"><?= e($def['workflow_code']) ?></code>
            </div>
            <span style="background:#F3F4F6;padding:2px 8px;border-radius:4px;font-size:12px;">v<?= (int) $def['version'] ?></span>
        </div>
        <p style="color:#6B7280;font-size:13px;margin-bottom:15px;"><?= e($def['description_ar'] ?? '') ?></p>
        <div style="display:flex;gap:15px;margin-bottom:15px;">
            <div style="text-align:center;">
                <div style="font-size:24px;font-weight:700;color:#0D7377;"><?= $s['active'] ?></div>
                <div style="font-size:11px;color:#6B7280;">نشط</div>
            </div>
            <div style="text-align:center;">
                <div style="font-size:24px;font-weight:700;color:#059669;"><?= $s['completed'] ?></div>
                <div style="font-size:11px;color:#6B7280;">مكتمل</div>
            </div>
        </div>
        <div style="display:flex;gap:8px;">
            <a href="/workflows/diagram/<?= e($def['workflow_code']) ?>" class="btn btn-sm btn-outline">المخطط</a>
            <a href="/workflows/instances/<?= e($def['workflow_code']) ?>" class="btn btn-sm btn-outline">المثيلات</a>
            <a href="/workflows/instances/<?= e($def['workflow_code']) ?>?status=active" class="btn btn-sm btn-outline" style="color:#D97706;">النشطة</a>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<!-- Filtered Instances (if viewing specific workflow) -->
<?php if (!empty($filteredInstances)): ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;color:#0D7377;">
            مثيلات: <?= e($currentDef ? $currentDef->name_ar : ($filterCode ?? '')) ?>
            <?php if (!empty($filterStatus)): ?>
                <span style="font-size:14px;color:#6B7280;">(<?= $filterStatus === 'active' ? 'نشطة' : ($filterStatus === 'completed' ? 'مكتملة' : 'الكل') ?>)</span>
            <?php endif; ?>
        </h3>
        <a href="/workflows" class="btn btn-sm btn-outline">← العودة</a>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>نوع الكيان</th>
                    <th>رقم الكيان</th>
                    <th>الحالة الحالية</th>
                    <th>دخول الحالة</th>
                    <th>مكتمل</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($filteredInstances as $inst): ?>
                <?php
                    $stateLabel = $inst['current_state'];
                    if ($currentDef) {
                        $states = $currentDef->getStates();
                        $stateLabel = $states[$inst['current_state']]['label_ar'] ?? $inst['current_state'];
                    }
                ?>
                <tr>
                    <td><?= (int) $inst['id'] ?></td>
                    <td><code style="font-size:12px;"><?= e($inst['entity_type']) ?></code></td>
                    <td><?= (int) $inst['entity_id'] ?></td>
                    <td>
                        <span style="background:<?= $inst['is_completed'] ? '#F0FDF4' : '#FFF7ED' ?>;color:<?= $inst['is_completed'] ? '#059669' : '#D97706' ?>;padding:3px 10px;border-radius:4px;font-size:13px;font-weight:600;">
                            <?= e($stateLabel) ?>
                        </span>
                    </td>
                    <td style="font-size:12px;"><?= e($inst['state_entered_at']) ?></td>
                    <td><?= $inst['is_completed'] ? '<span style="color:#059669;">✓</span>' : '<span style="color:#D97706;">—</span>' ?></td>
                    <td>
                        <a href="/workflows/instances/<?= (int) $inst['id'] ?>" class="btn btn-sm btn-outline">عرض</a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($filteredInstances)): ?>
                <tr><td colspan="7" style="text-align:center;padding:40px;color:#6B7280;">لا توجد مثيلات</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php $__template->endSection(); ?>