<?php include __DIR__ . '/../header.php'; ?>
<?php
$workflowSummary = $workflowSummary ?? [];
$workflowRules = $workflowRules ?? [];
$workflowMembers = $workflowMembers ?? [];
$assignableMembers = $assignableMembers ?? [];
$range = $range ?? 'week';
$window = $window ?? ['start' => '', 'end' => ''];

$fmtHours = static function (int $seconds): string {
    $hours = floor($seconds / 3600);
    $minutes = floor(($seconds % 3600) / 60);
    return sprintf('%02d:%02d', $hours, $minutes);
};

$distributionOptions = [
    'balanced' => [
        'icon' => 'bi-bar-chart-steps',
        'title' => __('team.workflow_distribution_balanced', 'Balanced / lower load first'),
        'description' => __('team.workflow_distribution_balanced_help', 'Give the next order to the agent with the lightest current workload.'),
    ],
    'round_robin' => [
        'icon' => 'bi-arrow-repeat',
        'title' => __('team.workflow_distribution_round_robin', 'Round robin / one by one'),
        'description' => __('team.workflow_distribution_round_robin_help', 'Rotate orders one by one between the selected agents in a fixed order.'),
    ],
    'random' => [
        'icon' => 'bi-shuffle',
        'title' => __('team.workflow_distribution_random', 'Random / fair shuffle'),
        'description' => __('team.workflow_distribution_random_help', 'Distribute the next order randomly among the selected agents.'),
    ],
    'priority' => [
        'icon' => 'bi-lightning-charge',
        'title' => __('team.workflow_distribution_priority', 'Priority / most available first'),
        'description' => __('team.workflow_distribution_priority_help', 'Prefer the most recently active agent, then fall back to lighter workload.'),
    ],
];
?>
<style>
.workflow-hero {
    border-radius: 28px;
    padding: 1.5rem;
    background:
        radial-gradient(circle at top right, rgba(56, 189, 248, 0.22), transparent 28%),
        radial-gradient(circle at bottom left, rgba(37, 99, 235, 0.22), transparent 26%),
        linear-gradient(135deg, #0f172a 0%, #1d4ed8 58%, #38bdf8 100%);
    color: #fff;
    box-shadow: 0 24px 55px rgba(29, 78, 216, .22);
}
.workflow-hero p,
.workflow-hero .text-soft {
    color: rgba(255,255,255,.78);
}
.workflow-pill {
    display: inline-flex;
    align-items: center;
    gap: .5rem;
    padding: .55rem .9rem;
    border-radius: 999px;
    background: rgba(255,255,255,.12);
    border: 1px solid rgba(255,255,255,.16);
    color: #fff;
    font-size: .82rem;
}
.workflow-stat-card {
    border: 0;
    border-radius: 22px;
    overflow: hidden;
    box-shadow: 0 14px 32px rgba(15, 23, 42, .08);
}
.workflow-stat-card .icon-box,
.workflow-distribution-option .icon-box {
    width: 48px;
    height: 48px;
    border-radius: 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
}
.workflow-rule-card {
    border: 0;
    border-radius: 24px;
    box-shadow: 0 14px 34px rgba(15, 23, 42, .08);
}
.workflow-rule-card .rule-chip,
.workflow-agent-pill {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    padding: .35rem .7rem;
    border-radius: 999px;
    font-size: .78rem;
}
.workflow-agent-pill {
    background: #eff6ff;
    color: #1d4ed8;
    border: 1px solid rgba(29, 78, 216, .12);
}
.workflow-rule-card .rule-chip {
    background: #f8fafc;
    color: #475569;
    border: 1px solid rgba(148, 163, 184, .18);
}
.workflow-rule-actions form {
    margin: 0;
}
.workflow-distribution-option {
    position: relative;
}
.workflow-distribution-option input,
.workflow-member-check input {
    position: absolute;
    opacity: 0;
}
.workflow-distribution-option label {
    display: block;
    height: 100%;
    border-radius: 20px;
    padding: 1rem;
    border: 1px solid rgba(148, 163, 184, .22);
    background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
    cursor: pointer;
    transition: transform .16s ease, border-color .16s ease, box-shadow .16s ease;
}
.workflow-distribution-option input:checked + label {
    border-color: rgba(29, 78, 216, .5);
    box-shadow: 0 0 0 4px rgba(59, 130, 246, .12);
    transform: translateY(-2px);
}
.workflow-member-check label {
    display: block;
    padding: .9rem 1rem;
    border-radius: 18px;
    border: 1px solid rgba(148, 163, 184, .2);
    background: #fff;
    cursor: pointer;
    transition: border-color .16s ease, box-shadow .16s ease, transform .16s ease;
}
.workflow-member-check input:checked + label {
    border-color: rgba(29, 78, 216, .45);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, .1);
    transform: translateY(-1px);
}
.workflow-filter-bar {
    border-radius: 18px;
    padding: .85rem;
    background: #fff;
    box-shadow: 0 12px 30px rgba(15, 23, 42, .05);
}
.workflow-mini-kpi {
    border-radius: 18px;
    padding: 1rem;
    background: linear-gradient(180deg,#fff 0%,#f8fafc 100%);
    border: 1px solid rgba(148,163,184,.14);
    height: 100%;
}
</style>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<?php if (!empty($_SESSION['success'])): ?>
<div class="alert alert-success alert-dismissible fade show py-2">
    <?= htmlspecialchars($_SESSION['success']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php unset($_SESSION['success']); endif; ?>

<?php if (!empty($_SESSION['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show py-2">
    <?= htmlspecialchars($_SESSION['error']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php unset($_SESSION['error']); endif; ?>

<div class="workflow-hero mb-4">
    <div class="d-flex flex-wrap justify-content-between align-items-start gap-3">
        <div>
            <div class="workflow-pill mb-3"><i class="bi bi-stars"></i><?= __('team.workflow_title','Order Workflow Automation') ?></div>
            <h2 class="fw-bold mb-2"><?= __('team.workflow_title','Order Workflow Automation') ?></h2>
            <p class="mb-0 text-soft"><?= __('team.workflow_subtitle','Automatic distribution, reassignment, manager visibility, and live workload tracking') ?></p>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <button type="button" class="btn btn-light fw-semibold" data-bs-toggle="modal" data-bs-target="#workflowRuleModal">
                <i class="bi bi-plus-circle me-1"></i><?= __('team.workflow_create_rule_cta','Create a new rule') ?>
            </button>
            <a href="/admin/team" class="btn btn-outline-light"><i class="bi bi-arrow-right me-1"></i><?= __('actions.back','Back') ?></a>
        </div>
    </div>
</div>

<div class="workflow-filter-bar mb-4">
    <div class="d-flex flex-wrap justify-content-between align-items-center gap-3">
        <div class="d-flex flex-wrap gap-2">
            <?php foreach ([
                'hour' => __('team.workflow_filter_hour','Hour'),
                'day' => __('team.workflow_filter_day','Day'),
                'week' => __('team.workflow_filter_week','Week'),
                'month' => __('team.workflow_filter_month','Month')
            ] as $rangeKey => $rangeLabel): ?>
            <a href="?range=<?= urlencode($rangeKey) ?>" class="btn btn-sm <?= $range === $rangeKey ? 'btn-primary' : 'btn-outline-secondary' ?>"><?= $rangeLabel ?></a>
            <?php endforeach; ?>
        </div>
        <form method="GET" class="d-flex flex-wrap gap-2 align-items-center">
            <input type="hidden" name="range" value="custom">
            <input type="date" name="from" class="form-control form-control-sm" value="<?= htmlspecialchars(substr((string)($window['start'] ?? ''), 0, 10)) ?>">
            <input type="date" name="to" class="form-control form-control-sm" value="<?= htmlspecialchars(substr((string)($window['end'] ?? ''), 0, 10)) ?>">
            <button type="submit" class="btn btn-outline-secondary btn-sm"><?= __('team.workflow_filter_custom','Custom') ?></button>
        </form>
    </div>
</div>

<div class="row g-3 mb-4">
    <?php
    $cards = [
        ['label'=>__('team.workflow_active_rules','Active Rules'), 'value'=>(int)($workflowSummary['active_rules'] ?? 0), 'icon'=>'bi-sliders2', 'color'=>'primary', 'bg'=>'#eff6ff'],
        ['label'=>__('team.workflow_pending_assignments','Pending Assignments'), 'value'=>(int)($workflowSummary['pending_assignments'] ?? 0), 'icon'=>'bi-inboxes', 'color'=>'warning', 'bg'=>'#fff7ed'],
        ['label'=>__('team.workflow_confirmed','Confirmed'), 'value'=>(int)($workflowSummary['confirmed'] ?? 0), 'icon'=>'bi-check-circle', 'color'=>'success', 'bg'=>'#ecfdf5'],
        ['label'=>__('team.workflow_reassigned','Reassignments'), 'value'=>(int)($workflowSummary['reassigned'] ?? 0), 'icon'=>'bi-arrow-repeat', 'color'=>'info', 'bg'=>'#ecfeff'],
        ['label'=>__('team.workflow_first_response','Avg First Response'), 'value'=>(float)($workflowSummary['avg_first_response_minutes'] ?? 0) . 'm', 'icon'=>'bi-stopwatch', 'color'=>'primary', 'bg'=>'#eff6ff'],
        ['label'=>__('team.workflow_stale_orders','Stale Orders'), 'value'=>(int)($workflowSummary['stale_orders'] ?? 0), 'icon'=>'bi-exclamation-diamond', 'color'=>'danger', 'bg'=>'#fff1f2'],
    ];
    foreach ($cards as $card):
    ?>
    <div class="col-12 col-md-6 col-xl-2">
        <div class="card workflow-stat-card h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="icon-box" style="background:<?= $card['bg'] ?>;color:var(--bs-<?= $card['color'] ?>)">
                        <i class="bi <?= $card['icon'] ?>"></i>
                    </div>
                    <div class="small text-muted"><?= $card['label'] ?></div>
                </div>
                <div class="display-6 fw-bold mb-0"><?= $card['value'] ?></div>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<div class="row g-3 mb-4">
    <div class="col-lg-8">
        <div class="card workflow-rule-card h-100">
            <div class="card-header bg-white border-0 py-3 d-flex justify-content-between align-items-center">
                <div>
                    <h5 class="mb-1 fw-bold"><?= __('team.workflow_rules','Distribution Rules') ?></h5>
                    <div class="text-muted small"><?= __('team.workflow_rules_subtitle','Clean rules, clear ownership, and a simpler setup flow for managers.') ?></div>
                </div>
                <span class="badge rounded-pill text-bg-light border"><?= count($workflowRules) ?></span>
            </div>
            <div class="card-body">
                <?php if (empty($workflowRules)): ?>
                    <div class="text-center py-5 text-muted">
                        <i class="bi bi-diagram-3 fs-1 d-block mb-3 opacity-50"></i>
                        <div class="fw-semibold mb-2"><?= __('team.workflow_rules_empty','No rules yet. Add the first rule to activate automatic distribution.') ?></div>
                        <button type="button" class="btn btn-primary btn-sm mt-2" data-bs-toggle="modal" data-bs-target="#workflowRuleModal">
                            <i class="bi bi-plus-circle me-1"></i><?= __('team.workflow_create_rule_cta','Create a new rule') ?>
                        </button>
                    </div>
                <?php else: ?>
                    <div class="row g-3">
                        <?php foreach ($workflowRules as $rule): ?>
                        <div class="col-12">
                            <div class="border rounded-4 p-3 h-100">
                                <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
                                    <div>
                                        <div class="d-flex flex-wrap align-items-center gap-2 mb-2">
                                            <h6 class="mb-0 fw-bold"><?= htmlspecialchars($rule['rule_name']) ?></h6>
                                            <?php if (!empty($rule['is_active'])): ?>
                                            <span class="badge bg-success-subtle text-success border border-success-subtle"><?= __('team.active','Active') ?></span>
                                            <?php else: ?>
                                            <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle"><?= __('team.inactive','Inactive') ?></span>
                                            <?php endif; ?>
                                        </div>
                                        <div class="text-muted small"><?= __('team.workflow_created_by','Created by') ?>: <?= htmlspecialchars($rule['creator_name'] ?? 'System') ?></div>
                                    </div>
                                    <div class="workflow-rule-actions d-flex align-items-center gap-2">
                                        <form method="POST" action="/admin/team/workflow/rules/toggle/<?= (int)$rule['id'] ?>">
                                            <?= \Core\System\Csrf::field() ?>
                                            <button type="submit" class="btn btn-sm btn-outline-<?= !empty($rule['is_active']) ? 'warning' : 'success' ?>">
                                                <i class="bi bi-<?= !empty($rule['is_active']) ? 'pause' : 'play' ?>"></i>
                                            </button>
                                        </form>
                                        <form method="POST" action="/admin/team/workflow/rules/delete/<?= (int)$rule['id'] ?>" onsubmit="return confirm('<?= __('team.confirm_delete','Are you sure?') ?>')">
                                            <?= \Core\System\Csrf::field() ?>
                                            <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                                        </form>
                                    </div>
                                </div>

                                <div class="d-flex flex-wrap gap-2 mb-3">
                                    <span class="rule-chip"><i class="bi bi-shuffle"></i><?= htmlspecialchars((string)__('team.workflow_distribution_' . ($rule['distribution_mode'] ?? 'balanced'), (string)($rule['distribution_mode'] ?? 'balanced'))) ?></span>
                                    <span class="rule-chip"><i class="bi bi-map"></i><?= !empty($rule['wilaya_codes_list']) ? htmlspecialchars(implode(' - ', $rule['wilaya_codes_list'])) : __('team.workflow_all_states','All states') ?></span>
                                    <?php if (!empty($rule['work_start']) && !empty($rule['work_end'])): ?>
                                    <span class="rule-chip"><i class="bi bi-clock"></i><?= htmlspecialchars((string)$rule['work_start']) ?> - <?= htmlspecialchars((string)$rule['work_end']) ?></span>
                                    <?php endif; ?>
                                    <span class="rule-chip"><i class="bi bi-layers"></i><?= __('team.workflow_rule_max_load','Max Active') ?>: <?= (int)($rule['max_active_orders'] ?? 0) ?: __('team.workflow_unlimited','Unlimited') ?></span>
                                    <span class="rule-chip"><i class="bi bi-arrow-repeat"></i><?= __('team.workflow_max_reassignments','Max reassignments') ?>: <?= (int)($rule['max_reassignments'] ?? 3) ?></span>
                                </div>

                                <div class="d-flex flex-wrap gap-2">
                                    <?php foreach (($rule['admin_names_list'] ?? []) as $agentName): ?>
                                    <span class="workflow-agent-pill"><i class="bi bi-person-badge"></i><?= htmlspecialchars((string)$agentName) ?></span>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="workflow-mini-kpi mb-3">
            <div class="small text-muted mb-2"><?= __('team.workflow_design_tip','Recommended setup') ?></div>
            <div class="fw-bold mb-2"><?= __('team.workflow_design_tip_title','Split the team by wilaya, then choose one smart distribution mode.') ?></div>
            <div class="text-muted small"><?= __('team.workflow_design_tip_body','For most stores, one rule for east, one for center, and one for west is clearer than mixing too many conditions together.') ?></div>
        </div>
        <div class="workflow-mini-kpi mb-3">
            <div class="small text-muted mb-2"><?= __('team.workflow_distribution_round_robin','Round robin / one by one') ?></div>
            <div class="fw-semibold"><?= __('team.workflow_round_robin_tip','Best when you want strict turn-by-turn fairness between agents.') ?></div>
        </div>
        <div class="workflow-mini-kpi">
            <div class="small text-muted mb-2"><?= __('team.workflow_distribution_random','Random / fair shuffle') ?></div>
            <div class="fw-semibold"><?= __('team.workflow_random_tip','Best when you want a lighter and less predictable equal distribution.') ?></div>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
        <div>
            <h6 class="mb-1 fw-semibold"><?= __('team.workflow_team_board','Team Workload Board') ?></h6>
            <span class="small text-muted"><?= __('team.workflow_team_board_hint','Live snapshot from assignments, outcomes and activity logs') ?></span>
        </div>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3"><?= __('team.member','Member') ?></th>
                        <th class="text-center"><?= __('team.workflow_pending_assignments','Pending') ?></th>
                        <th class="text-center"><?= __('team.workflow_confirmed','Confirmed') ?></th>
                        <th class="text-center"><?= __('team.workflow_rejected','Rejected') ?></th>
                        <th class="text-center"><?= __('team.workflow_cancelled','Cancelled') ?></th>
                        <th class="text-center"><?= __('team.workflow_deferred','Deferred') ?></th>
                        <th class="text-center"><?= __('team.workflow_declined','Declined') ?></th>
                        <th class="text-center"><?= __('team.workflow_callbacks','Callbacks') ?></th>
                        <th class="text-center"><?= __('team.workflow_escalations','Escalations') ?></th>
                        <th class="text-center"><?= __('team.workflow_worked_hours','Worked') ?></th>
                        <th class="text-center"><?= __('team.workflow_score','Score') ?></th>
                        <th class="text-center"><?= __('team.workflow_rank','Rank') ?></th>
                        <th class="text-center"><?= __('team.confirm_rate','Confirm Rate') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($workflowMembers as $member): ?>
                    <?php $rate = (float)($member['confirm_rate'] ?? 0); ?>
                    <tr>
                        <td class="ps-3">
                            <div class="fw-semibold"><?= htmlspecialchars($member['username']) ?></div>
                            <div class="small text-muted"><?= \Core\App\Models\TeamMemberModel::roleLabel($member['role']) ?></div>
                        </td>
                        <td class="text-center fw-bold text-warning"><?= (int)($member['pending_assigned'] ?? 0) ?></td>
                        <td class="text-center fw-bold text-success"><?= (int)($member['confirmed'] ?? 0) ?></td>
                        <td class="text-center fw-bold text-danger"><?= (int)($member['rejected'] ?? 0) ?></td>
                        <td class="text-center"><?= (int)($member['cancelled_count'] ?? 0) ?></td>
                        <td class="text-center"><?= (int)($member['deferred_count'] ?? 0) ?></td>
                        <td class="text-center"><?= (int)($member['declined_count'] ?? 0) ?></td>
                        <td class="text-center"><?= (int)($member['callback_count'] ?? 0) ?></td>
                        <td class="text-center"><?= (int)($member['escalations_count'] ?? 0) ?></td>
                        <td class="text-center"><?= $fmtHours((int)($member['worked_seconds'] ?? 0)) ?></td>
                        <td class="text-center fw-bold"><?= (float)($member['score'] ?? 0) ?></td>
                        <td class="text-center"><span class="badge bg-light text-dark border">#<?= (int)($member['rank'] ?? 0) ?></span></td>
                        <td class="text-center">
                            <span class="badge bg-<?= $rate >= 60 ? 'success' : ($rate >= 35 ? 'warning' : 'danger') ?>">
                                <?= $rate ?>%
                            </span>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="workflowRuleModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content border-0 shadow-lg">
            <div class="modal-header border-0 pb-0">
                <div>
                    <div class="text-primary small fw-semibold mb-1"><?= __('team.workflow_rule_new','New Distribution Rule') ?></div>
                    <h4 class="modal-title fw-bold mb-1"><?= __('team.workflow_create_rule_cta','Create a new rule') ?></h4>
                    <div class="text-muted small"><?= __('team.workflow_modal_subtitle','Keep the rule short, clear, and linked to the agents who should receive these orders.') ?></div>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body pt-4">
                <form method="POST" action="/admin/team/workflow/rules/save" id="workflowRuleForm">
                    <?= \Core\System\Csrf::field() ?>
                    <input type="hidden" name="source_pattern" value="">
                    <input type="hidden" name="product_ids" value="">
                    <input type="hidden" name="hourly_limit" value="0">
                    <input type="hidden" name="daily_limit" value="0">

                    <div class="row g-4">
                        <div class="col-lg-5">
                            <label class="form-label fw-semibold"><?= __('team.workflow_rule_name','Rule Name') ?></label>
                            <input type="text" name="rule_name" class="form-control form-control-lg" required placeholder="<?= __('team.workflow_rule_name_ph','Example: East states to Ahmed') ?>">

                            <div class="mt-4">
                                <label class="form-label fw-semibold"><?= __('team.workflow_rule_states','Wilaya Codes') ?></label>
                                <input type="text" name="wilaya_codes" class="form-control" placeholder="16, 31, 19">
                                <div class="form-text"><?= __('team.workflow_rule_states_hint','Leave empty to match all wilayas.') ?></div>
                            </div>

                            <div class="row g-3 mt-1">
                                <div class="col-md-6">
                                    <label class="form-label fw-semibold"><?= __('team.workflow_rule_max_load','Max Active') ?></label>
                                    <input type="number" name="max_active_orders" class="form-control" value="0" min="0">
                                    <div class="form-text"><?= __('team.workflow_rule_max_load_hint','0 means no limit.') ?></div>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label fw-semibold"><?= __('team.workflow_max_reassignments','Max reassignments') ?></label>
                                    <input type="number" name="max_reassignments" class="form-control" value="3" min="1">
                                </div>
                            </div>

                            <div class="row g-3 mt-1">
                                <div class="col-md-6">
                                    <label class="form-label fw-semibold"><?= __('team.workflow_work_start','Work start') ?></label>
                                    <input type="time" name="work_start" class="form-control">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label fw-semibold"><?= __('team.workflow_work_end','Work end') ?></label>
                                    <input type="time" name="work_end" class="form-control">
                                </div>
                            </div>

                            <div class="mt-4 d-flex flex-column gap-2">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="wf_active" name="is_active" value="1" checked>
                                    <label class="form-check-label" for="wf_active"><?= __('team.workflow_rule_active','Active immediately') ?></label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="wf_reassign" name="auto_reassign" value="1" checked>
                                    <label class="form-check-label" for="wf_reassign"><?= __('team.workflow_rule_reassign','Allow auto-reassign on refusal') ?></label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="wf_backlog" name="allow_when_backlog" value="1" checked>
                                    <label class="form-check-label" for="wf_backlog"><?= __('team.workflow_allow_backlog','Allow assignment even if agent already has pending orders') ?></label>
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-7">
                            <label class="form-label fw-semibold mb-3"><?= __('team.workflow_distribution_mode','Distribution mode') ?></label>
                            <div class="row g-3 mb-4">
                                <?php foreach ($distributionOptions as $mode => $option): ?>
                                <div class="col-md-6">
                                    <div class="workflow-distribution-option h-100">
                                        <input type="radio" name="distribution_mode" id="wf_mode_<?= htmlspecialchars($mode) ?>" value="<?= htmlspecialchars($mode) ?>" <?= $mode === 'round_robin' ? 'checked' : '' ?>>
                                        <label for="wf_mode_<?= htmlspecialchars($mode) ?>">
                                            <div class="d-flex align-items-start gap-3">
                                                <div class="icon-box bg-light text-primary"><i class="bi <?= htmlspecialchars($option['icon']) ?>"></i></div>
                                                <div>
                                                    <div class="fw-bold mb-1"><?= htmlspecialchars($option['title']) ?></div>
                                                    <div class="small text-muted"><?= htmlspecialchars($option['description']) ?></div>
                                                </div>
                                            </div>
                                        </label>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>

                            <label class="form-label fw-semibold mb-3"><?= __('team.workflow_rule_agents','Agents') ?></label>
                            <div class="row g-3">
                                <?php foreach ($assignableMembers as $member): ?>
                                <div class="col-md-6">
                                    <div class="workflow-member-check position-relative">
                                        <input type="checkbox" id="workflow_member_<?= (int)$member['id'] ?>" name="admin_ids[]" value="<?= (int)$member['id'] ?>">
                                        <label for="workflow_member_<?= (int)$member['id'] ?>">
                                            <div class="d-flex justify-content-between align-items-start gap-2">
                                                <div>
                                                    <div class="fw-bold"><?= htmlspecialchars($member['username']) ?></div>
                                                    <div class="small text-muted"><?= __('team.workflow_current_load','Current load') ?>: <?= (int)($member['pending_count'] ?? 0) ?></div>
                                                </div>
                                                <span class="badge bg-light text-dark border"><?= \Core\App\Models\TeamMemberModel::roleLabel($member['role']) ?></span>
                                            </div>
                                        </label>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer border-0 pt-0">
                <button type="button" class="btn btn-light" data-bs-dismiss="modal"><?= __('actions.cancel','Cancel') ?></button>
                <button type="submit" form="workflowRuleForm" class="btn btn-primary px-4">
                    <i class="bi bi-save me-1"></i><?= __('actions.save','Save') ?>
                </button>
            </div>
        </div>
    </div>
</div>

</div></section></div>
<?php include __DIR__ . '/../footer.php'; ?>
