@extends('admin.layouts.app')
@section('title', 'Reklam Düzenle')
@section('page-title', 'Reklam Düzenle')

@section('content')
<div class="d-flex align-items-center gap-3 mb-4">
    <a href="{{ route('admin.ads.index') }}" class="btn btn-secondary btn-sm">
        <i class="bi bi-arrow-left"></i> Reklamlara Dön
    </a>
</div>

<div class="row justify-content-center">
<div class="col-xl-6 col-lg-8">

<form method="POST" action="{{ route('admin.ads.update', $ad) }}" enctype="multipart/form-data">
    @csrf @method('PUT')
    <div class="card">
        <div class="card-header">
            <i class="bi bi-badge-ad-fill" style="color:#ffd60a"></i>
            <h6>{{ $ad->name }}</h6>
        </div>
        <div class="card-body">
            @if($errors->any())
            <div class="alert alert-danger py-2" style="font-size:13px">
                @foreach($errors->all() as $err)<div>{{ $err }}</div>@endforeach
            </div>
            @endif

            {{-- Önizleme --}}
            @if($ad->media_url)
            <div class="mb-3 p-3" style="background:var(--bg);border-radius:8px;text-align:center">
                @if($ad->type === 'video')
                    <video src="{{ $ad->media_url }}" controls style="max-width:100%;max-height:220px;border-radius:6px"></video>
                @else
                    <img src="{{ $ad->media_url }}" alt="" style="max-width:100%;max-height:160px;border-radius:6px">
                @endif
            </div>
            @endif

            <div class="row g-2 mb-1" style="font-size:13px;color:var(--text2)">
                <div class="col-4">Gösterim: <b style="color:var(--text)">{{ number_format($ad->impressions) }}</b></div>
                <div class="col-4">Tıklama: <b style="color:var(--text)">{{ number_format($ad->clicks) }}</b></div>
                <div class="col-4">CTR: <b style="color:var(--text)">%{{ $ad->ctr }}</b></div>
            </div>
            <hr style="border-color:var(--border)">

            <div class="mb-3">
                <label class="form-label">Reklam Adı</label>
                <input type="text" name="name" class="form-control" value="{{ old('name', $ad->name) }}" required>
            </div>
            <div class="row g-2 mb-3">
                <div class="col-6">
                    <label class="form-label">Tür</label>
                    <select name="type" class="form-select">
                        <option value="video"  {{ $ad->type === 'video'  ? 'selected' : '' }}>Video (pre-roll)</option>
                        <option value="banner" {{ $ad->type === 'banner' ? 'selected' : '' }}>Banner (görsel)</option>
                    </select>
                </div>
                <div class="col-6">
                    <label class="form-label">Konum</label>
                    <select name="placement" class="form-select">
                        <option value="preroll"     {{ $ad->placement === 'preroll'     ? 'selected' : '' }}>Video başı</option>
                        <option value="home_mid"    {{ $ad->placement === 'home_mid'    ? 'selected' : '' }}>Ana sayfa — orta</option>
                        <option value="home_bottom" {{ $ad->placement === 'home_bottom' ? 'selected' : '' }}>Ana sayfa — alt</option>
                    </select>
                </div>
            </div>
            <div class="mb-3">
                <label class="form-label">Yeni Dosya Yükle <span style="color:var(--text2);font-weight:400">(mevcut dosyanın yerine geçer)</span></label>
                <input type="file" name="media_file" class="form-control" accept="{{ $ad->type === 'video' ? 'video/mp4' : 'image/*' }}">
            </div>
            <div class="mb-3">
                <label class="form-label">Dış URL</label>
                <input type="url" name="external_url" class="form-control font-monospace" value="{{ old('external_url', $ad->external_url) }}">
            </div>
            <div class="mb-3">
                <label class="form-label">Tıklama URL'si</label>
                <input type="url" name="click_url" class="form-control" value="{{ old('click_url', $ad->click_url) }}">
            </div>
            <div class="row g-2 mb-3">
                <div class="col-6">
                    <label class="form-label">Geçme süresi (sn)</label>
                    <input type="number" name="skip_after" class="form-control" min="0" max="60" value="{{ old('skip_after', $ad->skip_after) }}">
                    <div class="form-text">0 = geçilemez</div>
                </div>
                <div class="col-6">
                    <label class="form-label">Ağırlık</label>
                    <input type="number" name="weight" class="form-control" min="1" max="100" value="{{ old('weight', $ad->weight) }}">
                </div>
            </div>
            <div class="row g-2 mb-3">
                <div class="col-6">
                    <label class="form-label">Başlangıç</label>
                    <input type="datetime-local" name="starts_at" class="form-control"
                           value="{{ old('starts_at', $ad->starts_at?->format('Y-m-d\TH:i')) }}">
                </div>
                <div class="col-6">
                    <label class="form-label">Bitiş</label>
                    <input type="datetime-local" name="ends_at" class="form-control"
                           value="{{ old('ends_at', $ad->ends_at?->format('Y-m-d\TH:i')) }}">
                </div>
            </div>
            <div class="form-check form-switch mb-3">
                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="edit_ad_active" role="switch"
                    {{ $ad->is_active ? 'checked' : '' }}>
                <label class="form-check-label" for="edit_ad_active">Aktif</label>
            </div>
            <button type="submit" class="btn btn-primary w-100">
                <i class="bi bi-floppy"></i> Değişiklikleri Kaydet
            </button>
        </div>
    </div>
</form>

</div>
</div>
@endsection
