<?php

use Livewire\Component;
use App\Models\BreakingNews;

return new class extends Component
{
    public $breakingNewsId;
    public $content = '';
    public $is_active = true;

    public function mount(BreakingNews $breakingNews)
    {
        $this->breakingNewsId = $breakingNews->id;
        $this->content = $breakingNews->content;
        $this->is_active = $breakingNews->is_active;
    }

    public function render()
    {
        if (!auth()->user()->can('edit breaking news')) {
            abort(403);
        }
        return $this->view()->layout('layouts::auth');
    }

    protected $rules = [
        'content' => 'required|string',
        'is_active' => 'boolean',
    ];

    public function update()
    {
        if (!auth()->user()->can('edit breaking news')) {
            session()->flash('error', 'Unauthorized.');
            return;
        }

        $this->validate();

        $breakingNews = BreakingNews::find($this->breakingNewsId);
        if ($breakingNews) {
            $breakingNews->update([
                'content' => $this->content,
                'is_active' => $this->is_active,
            ]);
        }

        session()->flash('message', 'Breaking news updated successfully.');
        return redirect()->route('admin.breaking');
    }

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