<div>
    <x-card title="Automatic triggers">
        <button type="button" class="btn btn-primary btn-sm mb-3" wire:click="newTrigger">
            <i class="mdi mdi-plus me-1"></i>New trigger
        </button>

        <table class="table">
            <thead>
                <tr>
                    <th>Template</th>
                    <th>Trigger</th>
                    <th>Model</th>
                    <th>Dedupe</th>
                    <th>Active</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach($triggers as $t)
                    <tr>
                        <td>{{ $t->template?->name ?? '—' }}</td>
                        <td><span class="badge bg-secondary">{{ $t->trigger_type->label() }}</span></td>
                        <td><small>{{ class_basename($t->model_type ?? 'Any') }}</small></td>
                        <td><small>{{ $t->dedupe_window_minutes }}m</small></td>
                        <td>
                            @if($t->is_active)
                                <span class="badge bg-success">active</span>
                            @else
                                <span class="badge bg-secondary">off</span>
                            @endif
                        </td>
                        <td class="text-end">
                            <button class="btn btn-sm btn-outline-primary" wire:click="edit({{ $t->id }})">Edit</button>
                            <button class="btn btn-sm btn-outline-danger"
                                    wire:click="delete({{ $t->id }})"
                                    wire:confirm="Delete this trigger?">Delete</button>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>

        @if($editingId !== null)
            <hr/>
            <h6 class="mb-3">{{ $editingId ? 'Edit trigger' : 'New trigger' }}</h6>
            <form wire:submit.prevent="save" class="row g-3">
                <div class="col-md-6">
                    <label class="form-label">Template</label>
                    <select class="form-select" wire:model="form.contact_template_id">
                        <option value="">— choose —</option>
                        @foreach($templates as $t)
                            <option value="{{ $t->id }}">{{ $t->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Trigger type</label>
                    <select class="form-select" wire:model="form.trigger_type">
                        @foreach($triggerTypes as $tt)
                            <option value="{{ $tt->value }}">{{ $tt->label() }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Model class</label>
                    <input type="text" class="form-control" wire:model="form.model_type" placeholder="App\Models\Claim\Claim">
                </div>
                <div class="col-md-9">
                    <label class="form-label">Conditions (JSON)</label>
                    <textarea class="form-control" rows="4" wire:model="form.conditions" placeholder='{"to_status_id": 5}'></textarea>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Dedupe (minutes)</label>
                    <input type="number" class="form-control" wire:model="form.dedupe_window_minutes">
                </div>
                <div class="col-12">
                    <div class="form-check form-switch">
                        <input type="checkbox" class="form-check-input" wire:model="form.is_active" id="trg_active">
                        <label class="form-check-label" for="trg_active">Active</label>
                    </div>
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-success">Save</button>
                    <button type="button" class="btn btn-outline-secondary" wire:click="$set('editingId', null)">Cancel</button>
                </div>
            </form>
        @endif
    </x-card>
</div>
