<?php

namespace App\Components\Dsar\Tranche;

use App\Models\Dsar\DsarTranche;
use App\Services\DsarService;
use App\Services\HistoryService;
use Livewire\Component;

class View extends Component {

    public DsarTranche $tranche;

    public $toApprove = [];

    public $listeners = [
        'dsar-updated' => 'dsarUpdated',
    ];

    public function mount(): void {
        $this->authorize('dsars.tranche.view');
    }

    #[\Livewire\Attributes\On('dsar-updated')]
    public function dsarUpdated() {
    }

    public function sendBatch(DsarService $service, HistoryService $history) {
        $this->authorize('dsars.batch.send');

        $approved = $this->tranche->dsars()->where('status', 'approved')->get();

        $approved->each(function($dsar) use ($service, $history) {
            $service->send($dsar);
        });

        return redirect()->route('clients.show', $this->tranche->claim->client);
    }

    public function render() {
        return view('components.dsar.tranche.view');
    }
}
