<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعديل: <?= e($definition->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/workflows/diagram/<?= e($definition->workflow_code) ?>" class="btn btn-outline">عرض المخطط</a>
<a href="/workflows" class="btn btn-outline">العودة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
    $states = $definition->getStates();
    $transitions = $definition->getTransitions();
?>

<!-- Basic Info -->
<form method="POST" action="/workflows/edit/<?= e($definition->workflow_code) ?>">
    <?= csrf_field() ?>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#1A1A2E;font-size:15px;">المعلومات الأساسية</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:16px;">
            <div class="form-group">
                <label class="form-label">كود دورة العمل</label>
                <input type="text" class="form-input" value="<?= e($definition->workflow_code) ?>" readonly style="background:#F3F4F6;font-family:monospace;">
            </div>
            <div class="form-group">
                <label class="form-label">الإصدار</label>
                <input type="text" class="form-input" value="v<?= (int) $definition->version ?>" readonly style="background:#F3F4F6;">
            </div>
            <div class="form-group">
                <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                <input type="text" name="name_ar" class="form-input" value="<?= e($definition->name_ar) ?>" required>
            </div>
            <div class="form-group">
                <label class="form-label">الاسم بالإنجليزي</label>
                <input type="text" name="name_en" class="form-input" value="<?= e($definition->name_en ?? '') ?>" style="direction:ltr;text-align:left;">
            </div>
            <div class="form-group" style="grid-column:1/-1;">
                <label class="form-label">الوصف</label>
                <textarea name="description_ar" class="form-textarea" rows="2"><?= e($definition->description_ar ?? '') ?></textarea>
            </div>
        </div>
    </div>

    <!-- States Editor -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
            <h3 style="margin:0;color:#1A1A2E;font-size:15px;">الحالات (<?= count($states) ?>)</h3>
            <span style="font-size:12px;color:#9CA3AF;">المفتاح غير قابل للتعديل — فقط التسميات</span>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;gap:10px;">
                <?php foreach ($states as $key => $state): ?>
                <?php
                    $type = $state['type'] ?? 'intermediate';
                    $leftColor = match($type) { 'initial' => '#0284C7', 'terminal' => '#DC2626', default => '#0D7377' };
                ?>
                <div style="display:grid;grid-template-columns:180px 1fr 1fr 120px;gap:12px;align-items:center;padding:12px 16px;background:#FAFAFA;border:1px solid #E5E7EB;border-right:4px solid <?= $leftColor ?>;border-radius:6px;">
                    <div>
                        <span style="font-family:monospace;font-size:12px;background:#F3F4F6;padding:3px 8px;border-radius:4px;"><?= e($key) ?></span>
                    </div>
                    <div>
                        <input type="text" name="states[<?= e($key) ?>][label_ar]" class="form-input" value="<?= e($state['label_ar'] ?? $key) ?>" placeholder="التسمية بالعربي" style="font-size:13px;padding:6px 10px;">
                    </div>
                    <div>
                        <input type="text" name="states[<?= e($key) ?>][label_en]" class="form-input" value="<?= e($state['label_en'] ?? '') ?>" placeholder="Label (English)" style="font-size:13px;padding:6px 10px;direction:ltr;text-align:left;">
                    </div>
                    <div style="text-align:center;">
                        <span style="font-size:11px;color:<?= $leftColor ?>;font-weight:600;">
                            <?= match($type) { 'initial' => 'بداية', 'terminal' => 'نهاية', default => 'وسيطة' } ?>
                        </span>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <!-- Transitions Editor -->
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#1A1A2E;font-size:15px;">الانتقالات (<?= count($transitions) ?>)</h3>
        </div>
        <div class="table-responsive">
            <table class="data-table" style="font-size:13px;">
                <thead>
                    <tr>
                        <th style="width:160px;">اسم الانتقال</th>
                        <th>من</th>
                        <th>إلى</th>
                        <th style="width:100px;">النوع</th>
                        <th>الشروط</th>
                        <th>الإجراءات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($transitions as $idx => $t): ?>
                    <?php
                        $fromState = $states[$t['from']] ?? [];
                        $toState = $states[$t['to']] ?? [];
                        $trigger = $t['trigger'] ?? 'manual';
                        $guards = $t['guards'] ?? [];
                        $actions = $t['actions'] ?? [];
                    ?>
                    <tr>
                        <td>
                            <input type="hidden" name="transitions[<?= $idx ?>][name]" value="<?= e($t['name']) ?>">
                            <input type="hidden" name="transitions[<?= $idx ?>][from]" value="<?= e($t['from']) ?>">
                            <input type="hidden" name="transitions[<?= $idx ?>][to]" value="<?= e($t['to']) ?>">
                            <input type="hidden" name="transitions[<?= $idx ?>][trigger]" value="<?= e($trigger) ?>">
                            <input type="hidden" name="transitions[<?= $idx ?>][guards_json]" value="<?= e(json_encode($guards, JSON_UNESCAPED_UNICODE)) ?>">
                            <input type="hidden" name="transitions[<?= $idx ?>][actions_json]" value="<?= e(json_encode($actions, JSON_UNESCAPED_UNICODE)) ?>">
                            <span style="font-weight:700;"><?= e($t['name']) ?></span>
                        </td>
                        <td>
                            <span style="background:#FEF2F2;color:#DC2626;padding:3px 8px;border-radius:4px;font-size:12px;"><?= e($fromState['label_ar'] ?? $t['from']) ?></span>
                        </td>
                        <td>
                            <span style="background:#F0FDF4;color:#059669;padding:3px 8px;border-radius:4px;font-size:12px;"><?= e($toState['label_ar'] ?? $t['to']) ?></span>
                        </td>
                        <td>
                            <select name="transitions[<?= $idx ?>][trigger_edit]" style="font-size:12px;padding:4px 8px;border:1px solid #E5E7EB;border-radius:4px;background:#fff;">
                                <option value="manual" <?= $trigger === 'manual' ? 'selected' : '' ?>>يدوي</option>
                                <option value="automatic" <?= $trigger === 'automatic' ? 'selected' : '' ?>>تلقائي</option>
                            </select>
                        </td>
                        <td style="font-size:11px;">
                            <?php if (empty($guards)): ?>
                                <span style="color:#D1D5DB;">بدون</span>
                            <?php else: ?>
                                <?php foreach ($guards as $g): ?>
                                    <div style="margin-bottom:2px;">
                                        <span style="background:#F3F4F6;padding:1px 5px;border-radius:3px;font-family:monospace;"><?= e($g['type'] ?? '') ?></span>
                                        <?php if (isset($g['days'])): ?><span style="color:#D97706;"><?= (int) $g['days'] ?>d</span><?php endif; ?>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </td>
                        <td style="font-size:11px;">
                            <?php if (empty($actions)): ?>
                                <span style="color:#D1D5DB;">&#8212;</span>
                            <?php else: ?>
                                <?php foreach ($actions as $a): ?>
                                    <div><span style="font-family:monospace;"><?= e(($a['type'] ?? '') . ':' . ($a['value'] ?? '')) ?></span></div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Save Actions -->
    <div style="display:flex;gap:10px;justify-content:space-between;align-items:center;">
        <div style="display:flex;gap:10px;">
            <button type="submit" class="btn btn-primary" style="padding:12px 30px;">حفظ التعديلات</button>
            <a href="/workflows/diagram/<?= e($definition->workflow_code) ?>" class="btn btn-outline" style="padding:12px 20px;">إلغاء</a>
        </div>
        <div style="font-size:12px;color:#9CA3AF;">
            سيتم زيادة رقم الإصدار تلقائياً عند الحفظ
        </div>
    </div>
</form>

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