<?php

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

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

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

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

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

        $this->validate();

        BreakingNews::create([
            'content' => $this->content,
            'is_active' => $this->is_active,
        ]);

        session()->flash('message', 'Breaking news created 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/9a4567bc.blade.php', $data);
    }
};
