@extends('layouts.app')
@section('content')
<main class="main-content"><div class="container-fluid"><h4>Assignment Rules</h4>
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
<div class="card mb-4"><div class="card-body"><form method="POST" action="{{ route('operations.assignment-rules.store') }}" class="row g-3">@csrf<div class="col-md-3"><input class="form-control" name="name" placeholder="Rule name (for example, EMEA lead routing)"></div><div class="col-md-2"><select name="module" class="form-select" required><option value="leads">Leads</option><option value="deals">Deals</option><option value="cases">Cases</option></select></div><div class="col-md-2"><select name="strategy" class="form-select" required><option value="round_robin">Round robin</option><option value="territory">Territory</option><option value="manual">Manual</option></select></div><div class="col-md-2"><select name="territory_country_ids[]" multiple class="form-select"><option disabled>Territory countries</option>@foreach($countries as $country)<option value="{{ $country->id }}">{{ $country->name }}</option>@endforeach</select></div><div class="col-md-2"><select name="user_ids[]" multiple class="form-select" required>@foreach($users as $user)<option value="{{ $user->id }}">{{ $user->name }} {{ $user->team_name ? '('.$user->team_name.')' : '' }}</option>@endforeach</select></div><div class="col-md-1"><button class="btn btn-primary">Save</button></div></form><small class="text-muted">Territory rules take priority when a Lead has a matching country; otherwise round-robin routing is used.</small></div></div>

<div class="card mb-4"><div class="card-header">Configured rules</div><div class="card-body table-responsive"><table class="table"><thead><tr><th>Rule</th><th>Module</th><th>Strategy</th><th>Territories</th><th>Members</th></tr></thead><tbody>@forelse($rules as $rule)<tr><td>{{ $rule->name }}</td><td>{{ ucfirst($rule->module) }}</td><td>{{ ucwords(str_replace('_',' ',$rule->strategy)) }}</td><td>{{ $rule->territory_names ?: '—' }}</td><td>{{ $rule->member_names ?: '—' }}</td></tr>@empty<tr><td colspan="5" class="text-muted">No assignment rules configured.</td></tr>@endforelse</tbody></table></div></div>

<div class="card mb-4 border-danger"><div class="card-header d-flex justify-content-between"><span>Failed-routing queue</span><span class="badge bg-danger">{{ $failures->count() }} open</span></div><div class="card-body table-responsive"><table class="table"><thead><tr><th>Module / record</th><th>Failure reason</th><th>Queued</th><th>Manual assignee</th></tr></thead><tbody>@forelse($failures as $failure)<tr><td>{{ ucfirst(rtrim($failure->module, 's')) }} #{{ $failure->assignable_id ?: 'Pending record' }}</td><td>{{ $failure->reason }}</td><td>{{ $failure->created_at }}</td><td>@if($failure->assignable_id)<form method="POST" action="{{ route('operations.assignment-failures.resolve',$failure->id) }}" class="d-flex gap-2">@csrf<select name="user_id" class="form-select form-select-sm" required><option value="">Select user</option>@foreach($users as $user)<option value="{{ $user->id }}">{{ $user->name }}</option>@endforeach</select><button class="btn btn-sm btn-success">Assign &amp; resolve</button></form>@else <span class="text-muted">Record creation was not completed.</span>@endif</td></tr>@empty<tr><td colspan="4" class="text-muted">No failed routing requires action.</td></tr>@endforelse</tbody></table></div></div>

<div class="card"><div class="card-header">Assignment audit</div><div class="card-body table-responsive"><table class="table"><thead><tr><th>Rule</th><th>Strategy</th><th>Eligible users</th><th>Selected user</th><th>Reason</th><th>Timestamp</th></tr></thead><tbody>@forelse($audits as $audit)<tr><td>{{ $audit->rule_name }}</td><td>{{ ucwords(str_replace('_',' ',$audit->strategy)) }}</td><td>{{ $audit->eligible_names ?: 'None' }}</td><td>{{ $audit->selected_name }}</td><td>{{ $audit->reason }}</td><td>{{ $audit->created_at }}</td></tr>@empty<tr><td colspan="6" class="text-muted">No automatic assignment decisions recorded.</td></tr>@endforelse</tbody></table></div></div>
</div></main>
@endsection
