@extends('layouts.admin')

@section('title', 'Billing reminder automation')

@section('page-title', 'Billing reminder automation')

@section('breadcrumbs')
    <a href="{{ route('admin.settings.general') }}" class="hover:text-primary">Settings</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">Reminders</span>
@endsection

@section('content')
<div class="max-w-5xl mx-auto space-y-6">
    @if(session('success'))
        <div class="rounded-xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-800">{{ session('success') }}</div>
    @endif

    <form method="POST" action="{{ route('admin.reminders.settings.update') }}" class="space-y-6">
        @csrf

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50/80">
                <h2 class="text-lg font-bold text-gray-900">Invoice &amp; payment emails</h2>
                <p class="text-sm text-gray-500 mt-1">Automatic messages when invoices and payments change.</p>
            </div>
            <div class="px-6 py-2 divide-y divide-gray-100">
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_invoice_created', 'label' => 'Invoice created', 'hint' => 'When a new invoice is saved in admin.', 'checked' => $settings->enable_invoice_created])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_invoice_due_reminder', 'label' => 'Invoice due soon', 'hint' => 'Scheduler sends before due date (see timing below).', 'checked' => $settings->enable_invoice_due_reminder])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_invoice_overdue_reminder', 'label' => 'Invoice overdue', 'hint' => 'Scheduler sends after due date for unpaid balances.', 'checked' => $settings->enable_invoice_overdue_reminder])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_invoice_paid', 'label' => 'Invoice paid', 'checked' => $settings->enable_invoice_paid])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_partial_payment', 'label' => 'Partial payment received', 'checked' => $settings->enable_partial_payment])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_payment_receipt', 'label' => 'Payment receipt', 'checked' => $settings->enable_payment_receipt])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_recurring_invoice_generated', 'label' => 'Recurring invoice generated', 'checked' => $settings->enable_recurring_invoice_generated])
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50/80">
                <h2 class="text-lg font-bold text-gray-900">Quotations &amp; estimates</h2>
            </div>
            <div class="px-6 py-2">
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_quotation_created', 'label' => 'Quotation created', 'checked' => $settings->enable_quotation_created])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_quotation_expiry', 'label' => 'Quotation expiry reminder', 'hint' => 'Scheduler sends before validity ends.', 'checked' => $settings->enable_quotation_expiry])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_quotation_accepted', 'label' => 'Quotation accepted', 'checked' => $settings->enable_quotation_accepted])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_quotation_rejected', 'label' => 'Quotation rejected', 'checked' => $settings->enable_quotation_rejected])
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50/80">
                <h2 class="text-lg font-bold text-gray-900">Purchase &amp; general</h2>
            </div>
            <div class="px-6 py-2">
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_purchase_order_created', 'label' => 'Purchase order created (supplier email)', 'checked' => $settings->enable_purchase_order_created])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_customer_welcome', 'label' => 'Customer welcome email', 'checked' => $settings->enable_customer_welcome])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_manual_reminders', 'label' => 'Manual reminders from invoice / quotation', 'checked' => $settings->enable_manual_reminders])
                @include('admin.reminders.partials.bool-switch', ['name' => 'enable_follow_up_reminder', 'label' => 'Follow-up reminder type', 'hint' => 'When staff marks “log as follow-up”.', 'checked' => $settings->enable_follow_up_reminder])
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50/80">
                <h2 class="text-lg font-bold text-gray-900">Timing &amp; delivery</h2>
            </div>
            <div class="px-6 py-4 grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label for="due_reminder_days_before" class="block text-sm font-medium text-gray-700 mb-1">Days before due date (due soon)</label>
                    <input type="number" name="due_reminder_days_before" id="due_reminder_days_before" min="0" max="90" value="{{ old('due_reminder_days_before', $settings->due_reminder_days_before) }}" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-1 focus:ring-primary" required>
                    @error('due_reminder_days_before')<p class="text-xs text-red-600 mt-1">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label for="overdue_reminder_days_after" class="block text-sm font-medium text-gray-700 mb-1">First overdue reminder after (days past due)</label>
                    <input type="number" name="overdue_reminder_days_after" id="overdue_reminder_days_after" min="0" max="90" value="{{ old('overdue_reminder_days_after', $settings->overdue_reminder_days_after) }}" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-1 focus:ring-primary" required>
                    @error('overdue_reminder_days_after')<p class="text-xs text-red-600 mt-1">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label for="quotation_expiry_days_before" class="block text-sm font-medium text-gray-700 mb-1">Quotation expiry reminder (days before)</label>
                    <input type="number" name="quotation_expiry_days_before" id="quotation_expiry_days_before" min="0" max="90" value="{{ old('quotation_expiry_days_before', $settings->quotation_expiry_days_before) }}" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-1 focus:ring-primary" required>
                    @error('quotation_expiry_days_before')<p class="text-xs text-red-600 mt-1">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label for="email_sending_mode" class="block text-sm font-medium text-gray-700 mb-1">Email sending mode</label>
                    <select name="email_sending_mode" id="email_sending_mode" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-primary focus:ring-1 focus:ring-primary" required>
                        <option value="queue" @selected(old('email_sending_mode', $settings->email_sending_mode) === 'queue')>Queue (recommended — needs cron + worker)</option>
                        <option value="instant" @selected(old('email_sending_mode', $settings->email_sending_mode) === 'instant')>Send immediately</option>
                    </select>
                    <p class="text-xs text-gray-500 mt-1">Use database queue on shared hosting; run <code class="bg-gray-100 px-1 rounded">php artisan queue:work</code> or cron.</p>
                </div>
            </div>
            <div class="px-6 pb-4 space-y-3">
                @include('admin.reminders.partials.bool-switch', ['name' => 'send_copy_to_admin', 'label' => 'Send copy to company email', 'checked' => $settings->send_copy_to_admin])
                @include('admin.reminders.partials.bool-switch', ['name' => 'attach_pdf_to_emails', 'label' => 'Attach PDF when template supports it', 'hint' => 'Invoice / quotation PDFs where implemented.', 'checked' => $settings->attach_pdf_to_emails])
            </div>
        </div>

        <div class="flex justify-end">
            <button type="submit" class="px-6 py-2.5 rounded-lg bg-primary text-white text-sm font-semibold hover:opacity-95 shadow-sm">Save settings</button>
        </div>
    </form>
</div>
@endsection
