@extends('layouts.app')
@section('title', 'Daily Branch Reports')

@section('content')
<x-ui.page-toolbar
    title="Daily Branch Reports"
    description="Manage report recipients and review the permanent archive of manager-confirmed daily branch reports."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Administration'],['label'=>'Daily Branch Reports']]"
>
    <x-slot:actions>
        <a class="mf-btn" href="{{ route('admin.system-settings.edit') }}">Delivery settings</a>
    </x-slot:actions>
</x-ui.page-toolbar>

<div class="space-y-6">
    <section class="mf-card">
        <div class="mf-card-body border-b">
            <h2 class="text-sm font-semibold">Report recipients</h2>
            <p class="mt-1 text-xs text-muted-foreground">Choose a specific branch or All branches. A manager can receive the same report through both channels by adding two recipient rows.</p>
        </div>

        <form method="POST" action="{{ route('admin.daily-reports.recipients.store') }}" class="mf-card-body border-b">
            @csrf
            <div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-12">
                <div class="xl:col-span-2">
                    <label class="mf-field-label" for="report_recipient_name">Name</label>
                    <input id="report_recipient_name" class="mf-input w-full" style="height:40px" name="name" value="{{ old('name') }}" placeholder="Manager name">
                </div>
                <div class="xl:col-span-2">
                    <label class="mf-field-label" for="report_recipient_branch">Branch</label>
                    <select id="report_recipient_branch" class="mf-input w-full" style="height:40px" name="branch_id">
                        <option value="">All branches</option>
                        @foreach($branches as $branch)
                            <option value="{{ $branch->id }}" @selected((string)old('branch_id')===(string)$branch->id)>{{ $branch->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="xl:col-span-2">
                    <label class="mf-field-label" for="report_recipient_channel">Channel</label>
                    <select id="report_recipient_channel" class="mf-input w-full" style="height:40px" name="channel" required>
                        <option value="email" @selected(old('channel','email')==='email')>Email</option>
                        <option value="whatsapp" @selected(old('channel')==='whatsapp')>WhatsApp</option>
                    </select>
                </div>
                <div class="xl:col-span-4">
                    <label class="mf-field-label" for="report_recipient_destination">Email or phone</label>
                    <input id="report_recipient_destination" class="mf-input w-full" style="height:40px" name="destination" value="{{ old('destination') }}" placeholder="manager@example.com or 0756123456" required>
                </div>
                <div class="xl:col-span-1">
                    <span class="mf-field-label" aria-hidden="true">&nbsp;</span>
                    <label class="mf-option w-full justify-center" style="height:40px;min-height:40px;padding-top:0;padding-bottom:0">
                        <input type="checkbox" name="is_active" value="1" checked>
                        <span class="text-xs font-medium">Active</span>
                    </label>
                </div>
                <div class="xl:col-span-1">
                    <span class="mf-field-label" aria-hidden="true">&nbsp;</span>
                    <button class="mf-btn mf-btn-primary w-full" style="height:40px" type="submit">Add</button>
                </div>
            </div>
        </form>

        <div class="mf-table-frame border-0">
            <table class="mf-table" data-standard-table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Branch</th>
                        <th>Channel</th>
                        <th>Destination</th>
                        <th>Status</th>
                        <th class="text-right">Actions</th>
                    </tr>
                </thead>
                <tbody>
                @forelse($recipients as $recipient)
                    <tr>
                        <td class="font-medium">{{ $recipient->name ?: '—' }}</td>
                        <td>{{ $recipient->branch?->name ?? 'All branches' }}</td>
                        <td><span class="mf-badge mf-badge-muted">{{ $recipient->channel === 'whatsapp' ? 'WhatsApp' : 'Email' }}</span></td>
                        <td>{{ $recipient->destination }}</td>
                        <td><span class="mf-badge {{ $recipient->is_active ? 'mf-badge-success' : 'mf-badge-muted' }}">{{ $recipient->is_active ? 'Active' : 'Paused' }}</span></td>
                        <td class="text-right whitespace-nowrap">
                            <div class="inline-flex gap-2">
                                <form method="POST" action="{{ route('admin.daily-reports.recipients.update',$recipient) }}">
                                    @csrf
                                    @method('PUT')
                                    <input type="hidden" name="name" value="{{ $recipient->name }}">
                                    <input type="hidden" name="branch_id" value="{{ $recipient->branch_id }}">
                                    <input type="hidden" name="channel" value="{{ $recipient->channel }}">
                                    <input type="hidden" name="destination" value="{{ $recipient->destination }}">
                                    @if(!$recipient->is_active)<input type="hidden" name="is_active" value="1">@endif
                                    <button class="mf-btn mf-btn-sm" type="submit">{{ $recipient->is_active ? 'Pause' : 'Activate' }}</button>
                                </form>
                                <form method="POST" action="{{ route('admin.daily-reports.recipients.destroy',$recipient) }}" onsubmit="return confirm('Remove this report recipient? Existing delivery history will remain.');">
                                    @csrf
                                    @method('DELETE')
                                    <button class="mf-btn mf-btn-sm" type="submit">Remove</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="6" class="text-center text-muted-foreground">No report recipients configured yet.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </section>

    <section class="mf-card">
        <div class="mf-card-body border-b">
            <h2 class="text-sm font-semibold">Report archive</h2>
            <p class="mt-1 text-xs text-muted-foreground">Immutable copies of manager-confirmed branch reports and their delivery results.</p>
        </div>

        <div class="mf-table-frame border-0">
            <table class="mf-table" data-standard-table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Branch</th>
                        <th>Closing</th>
                        <th>Confirmed by</th>
                        <th>Email</th>
                        <th>WhatsApp</th>
                        <th>Generated</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                @forelse($reports as $report)
                    @php
                        $email = $report->deliveries->where('channel','email');
                        $whatsapp = $report->deliveries->where('channel','whatsapp');
                        $deliveryLabel = function ($items) {
                            if ($items->isEmpty()) return 'Not sent';
                            $sent = $items->where('status','sent')->count();
                            $failed = $items->where('status','failed')->count();
                            $pending = $items->where('status','pending')->count();
                            return $sent.' sent'.($failed ? ' · '.$failed.' failed' : '').($pending ? ' · '.$pending.' pending' : '');
                        };
                    @endphp
                    <tr>
                        <td>{{ $report->report_date->format('d M Y') }}</td>
                        <td class="font-medium">{{ $report->branch->name }}</td>
                        <td>{{ $report->closing_number }}</td>
                        <td>{{ $report->dailyClosing?->managerConfirmer?->name ?? '—' }}</td>
                        <td>{{ $deliveryLabel($email) }}</td>
                        <td>{{ $deliveryLabel($whatsapp) }}</td>
                        <td>{{ $report->generated_at->format('d M Y H:i') }}</td>
                        <td class="text-right"><a class="mf-btn mf-btn-sm" href="{{ route('admin.daily-reports.show',$report) }}">Open</a></td>
                    </tr>
                @empty
                    <tr><td colspan="8" class="text-center text-muted-foreground">No confirmed daily branch reports have been archived yet.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </section>
</div>

<div class="mt-4">{{ $reports->links() }}</div>
@endsection
