@extends('layouts.default')

@section('content')
<div class="container-fluid">
    <div class="row page-titles mx-0">
        <div class="col-sm-6 p-md-0">
            <div class="welcome-text">
                <h4>Ajans Yönetim Raporu Mail Ayarları</h4>
                <span>Haftalık ve aylık ajans içi yönetim raporunun alıcılarını yönetin.</span>
            </div>
        </div>
        <div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
            <a href="{{ route('admin.management-reports.index') }}" class="btn btn-outline-secondary btn-sm">Yönetim Raporları</a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="row">
        <div class="col-xl-7">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Gönderim Ayarları</h4></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('admin.management-reports.mail-settings.save') }}">
                        @csrf
                        <div class="form-group">
                            <label>Yönetici Alıcıları</label>
                            <textarea name="recipients" rows="3" class="form-control" placeholder="ornek@datweb.com.tr, yonetici@datweb.com.tr">{{ old('recipients', $settings['recipients'] ?? '') }}</textarea>
                            <small class="text-muted">Virgül, noktalı virgül veya satır satır yazılabilir.</small>
                        </div>

                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label>CC</label>
                                <textarea name="cc" rows="2" class="form-control">{{ old('cc', $settings['cc'] ?? '') }}</textarea>
                            </div>
                            <div class="form-group col-md-6">
                                <label>BCC</label>
                                <textarea name="bcc" rows="2" class="form-control">{{ old('bcc', $settings['bcc'] ?? '') }}</textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label>Konu Ön Eki</label>
                            <input type="text" name="subject_prefix" class="form-control" value="{{ old('subject_prefix', $settings['subject_prefix'] ?? 'DW Ads Reporter') }}">
                        </div>

                        <div class="form-group">
                            <label>Mail Giriş Notu</label>
                            <textarea name="intro_note" rows="3" class="form-control">{{ old('intro_note', $settings['intro_note'] ?? '') }}</textarea>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="custom-control custom-switch mb-2">
                                    <input type="checkbox" class="custom-control-input" id="auto_weekly_enabled" name="auto_weekly_enabled" value="1" @checked($settings['auto_weekly_enabled'] ?? true)>
                                    <label class="custom-control-label" for="auto_weekly_enabled">Haftalık otomatik gönderim aktif</label>
                                </div>
                                <div class="custom-control custom-switch mb-2">
                                    <input type="checkbox" class="custom-control-input" id="auto_monthly_enabled" name="auto_monthly_enabled" value="1" @checked($settings['auto_monthly_enabled'] ?? true)>
                                    <label class="custom-control-label" for="auto_monthly_enabled">Aylık otomatik gönderim aktif</label>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="custom-control custom-switch mb-2">
                                    <input type="checkbox" class="custom-control-input" id="include_html" name="include_html" value="1" @checked($settings['include_html'] ?? true)>
                                    <label class="custom-control-label" for="include_html">HTML raporu ekle</label>
                                </div>
                                <div class="custom-control custom-switch mb-2">
                                    <input type="checkbox" class="custom-control-input" id="include_csv" name="include_csv" value="1" @checked($settings['include_csv'] ?? true)>
                                    <label class="custom-control-label" for="include_csv">CSV özetini ekle</label>
                                </div>
                            </div>
                        </div>

                        <button class="btn btn-primary mt-3">Ayarları Kaydet</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-xl-5">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Son Raporu Hızlı Gönder</h4></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('admin.management-reports.send-latest') }}">
                        @csrf
                        <div class="form-group">
                            <label>Dönem</label>
                            <select name="period_type" class="form-control">
                                <option value="weekly">Haftalık</option>
                                <option value="monthly">Aylık</option>
                                <option value="daily">Günlük</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Manuel Alıcılar</label>
                            <textarea name="recipients" rows="2" class="form-control" placeholder="Boş bırakılırsa varsayılan alıcılar kullanılır"></textarea>
                        </div>
                        <div class="custom-control custom-checkbox mb-3">
                            <input type="checkbox" class="custom-control-input" id="generate_if_missing" name="generate_if_missing" value="1">
                            <label class="custom-control-label" for="generate_if_missing">Gönderimden önce yeni rapor oluştur</label>
                        </div>
                        <button class="btn btn-success btn-block">Son Raporu Gönder</button>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h4 class="card-title">Son Gönderimler</h4></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Konu</th><th>Durum</th><th>Tarih</th></tr></thead>
                            <tbody>
                                @forelse($logs as $log)
                                    <tr>
                                        <td>{{ $log->subject }}</td>
                                        <td><span class="badge badge-{{ $log->status === 'sent' ? 'success' : ($log->status === 'failed' ? 'danger' : 'warning') }}">{{ $log->status }}</span></td>
                                        <td>{{ optional($log->created_at)->format('d.m.Y H:i') }}</td>
                                    </tr>
                                @empty
                                    <tr><td colspan="3" class="text-center text-muted p-4">Henüz gönderim yok.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
