@csrf
@php
    $currentChannels = old('notification_channels', $rule->notification_channels ?? ['email','panel']);
    $notificationEmails = old('notification_emails', implode("\n", $rule->notification_emails ?? []));
@endphp
<div class="row">
    <div class="col-xl-8">
        <div class="card">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Müşteri</label>
                        <select name="client_id" class="form-control default-select">
                            <option value="">Tüm müşteriler</option>
                            @foreach($clients as $client)
                                <option value="{{ $client->id }}" @selected(old('client_id', $rule->client_id) == $client->id)>{{ $client->company_name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Platform *</label>
                        <select name="platform" class="form-control default-select" required>
                            @foreach(['google_ads'=>'Google Ads','meta_ads'=>'Meta Ads','search_console'=>'Search Console','ga4'=>'GA4','system'=>'Sistem'] as $value=>$label)
                                <option value="{{ $value }}" @selected(old('platform',$rule->platform)===$value)>{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-12 mb-3">
                        <label class="form-label">Kural Adı *</label>
                        <input type="text" name="name" class="form-control" value="{{ old('name', $rule->name) }}" placeholder="Örn: Günlük Google Ads harcaması 5.000 TL üstüne çıkarsa" required>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Metrik *</label>
                        <input type="text" name="metric" class="form-control" value="{{ old('metric', $rule->metric) }}" placeholder="cost, spend, ctr, clicks" required list="metric-list">
                        <datalist id="metric-list">
                            @foreach($metricCatalog as $platform => $items)
                                @foreach($items as $key => $label)
                                    <option value="{{ $key }}">{{ strtoupper(str_replace('_',' ', $platform)) }} - {{ $label }}</option>
                                @endforeach
                            @endforeach
                        </datalist>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Operatör *</label>
                        <select name="operator" class="form-control default-select" required>
                            @foreach(['>','>=','<','<=','=','!='] as $op)
                                <option value="{{ $op }}" @selected(old('operator',$rule->operator)===$op)>{{ $op }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Eşik *</label>
                        <input type="number" step="0.0001" name="threshold" class="form-control" value="{{ old('threshold', $rule->threshold) }}" required>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Periyot *</label>
                        <select name="period" class="form-control default-select" required>
                            <option value="daily" @selected(old('period',$rule->period)==='daily')>Günlük / Dün</option>
                            <option value="weekly" @selected(old('period',$rule->period)==='weekly')>Haftalık / Son 7 gün</option>
                            <option value="monthly" @selected(old('period',$rule->period)==='monthly')>Aylık / Son 30 gün</option>
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Önem *</label>
                        <select name="severity" class="form-control default-select" required>
                            <option value="info" @selected(old('severity',$rule->severity)==='info')>Bilgi</option>
                            <option value="warning" @selected(old('severity',$rule->severity)==='warning')>Uyarı</option>
                            <option value="critical" @selected(old('severity',$rule->severity)==='critical')>Kritik</option>
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Tekrar Bildirim Süresi</label>
                        <input type="number" name="cooldown_minutes" class="form-control" value="{{ old('cooldown_minutes', $rule->cooldown_minutes ?? 1440) }}" min="0" max="10080">
                        <small class="text-muted">Dakika. 1440 = aynı kural en erken 1 gün sonra tekrar bildirir.</small>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header"><h4 class="card-title">Hazır metrik örnekleri</h4></div>
            <div class="card-body">
                <div class="row">
                    @foreach($metricCatalog as $platform => $items)
                        <div class="col-md-6 mb-3">
                            <strong>{{ strtoupper(str_replace('_',' ', $platform)) }}</strong>
                            <div class="d-flex flex-wrap gap-1 mt-2">
                                @foreach($items as $key => $label)
                                    <span class="badge badge-light border">{{ $key }}</span>
                                @endforeach
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-4">
        <div class="card">
            <div class="card-body">
                <label class="form-label d-block">Bildirim Kanalları</label>
                @foreach(['email'=>'E-posta','panel'=>'Panel','webhook'=>'Webhook'] as $value=>$label)
                    <div class="form-check mb-2">
                        <input class="form-check-input" type="checkbox" name="notification_channels[]" value="{{ $value }}" id="channel_{{ $value }}" @checked(in_array($value, $currentChannels ?? []))>
                        <label class="form-check-label" for="channel_{{ $value }}">{{ $label }}</label>
                    </div>
                @endforeach

                <div class="mb-3 mt-3">
                    <label class="form-label">Ek Bildirim E-postaları</label>
                    <textarea name="notification_emails" rows="5" class="form-control" placeholder="Her satıra bir e-posta">{{ $notificationEmails }}</textarea>
                    <small class="text-muted">Boş bırakılırsa müşteri rapor alıcıları kullanılır.</small>
                </div>

                <div class="form-check mb-3">
                    <input class="form-check-input" type="checkbox" name="is_active" value="1" id="rule_active" @checked(old('is_active', $rule->is_active))>
                    <label class="form-check-label" for="rule_active">Aktif</label>
                </div>

                <button class="btn btn-primary w-100 mb-2">Kaydet</button>
                <a href="{{ route('admin.alert-rules.index') }}" class="btn btn-light w-100">Vazgeç</a>
            </div>
        </div>
    </div>
</div>
