<?php

use Livewire\Component;
use App\Models\ReporterApplication;

return new class extends Component
{
    public $filter = 'all';
    public $viewingId = null;
    public $remarksInput = '';

    public function render()
    {
        return $this->view()->layout('layouts::auth');
    }

    public function with()
    {
        $query = ReporterApplication::latest();

        if ($this->filter !== 'all') {
            $query->where('status', $this->filter);
        }

        return [
            'applications' => $query->get(),
            'viewing'      => $this->viewingId ? ReporterApplication::find($this->viewingId) : null,
            'counts' => [
                'all'       => ReporterApplication::count(),
                'pending'   => ReporterApplication::where('status', 'pending')->count(),
                'interview' => ReporterApplication::where('status', 'interview')->count(),
                'selected'  => ReporterApplication::where('status', 'selected')->count(),
                'rejected'  => ReporterApplication::where('status', 'rejected')->count(),
            ],
        ];
    }

    public function openApplication($id)
    {
        $this->viewingId = $id;
        $app = ReporterApplication::find($id);
        $this->remarksInput = $app ? $app->admin_remarks ?? '' : '';
    }

    public function closeView()
    {
        $this->viewingId = null;
        $this->remarksInput = '';
    }

    public function updateStatus($id, $status)
    {
        ReporterApplication::find($id)?->update(['status' => $status]);
        if ($this->viewingId === $id) {
            // refresh remarks
        }
        session()->flash('message', 'Status updated.');
    }

    public function saveRemarks()
    {
        if (!$this->viewingId) return;
        ReporterApplication::find($this->viewingId)?->update(['admin_remarks' => $this->remarksInput]);
        session()->flash('message', 'Remarks saved.');
    }

    protected function view($data = [])
    {
        return app('view')->file('D:\02-my freelance\00-DR_Padhi\DR_PADHI\storage\framework/views/livewire/views/cf104398.blade.php', $data);
    }
};
