<?php

use Livewire\Component;
use App\Models\Faq;

return new class extends Component
{
    public Faq $faq;
    public $question = '';
    public $answer = '';
    public $sort_order = 0;
    public $status = '';

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

    public function mount(Faq $faq)
    {
        $this->faq = $faq;
        $this->question = $faq->question;
        $this->answer = $faq->answer;
        $this->sort_order = $faq->sort_order;
        $this->status = $faq->status;
    }

    public function save()
    {
        $this->validate([
            'question' => 'required|min:5',
            'answer' => 'required|min:10',
            'sort_order' => 'required|integer',
            'status' => 'required|in:draft,published',
        ]);

        $this->faq->update([
            'question' => $this->question,
            'answer' => $this->answer,
            'sort_order' => $this->sort_order,
            'status' => $this->status,
        ]);

        session()->flash('message', 'FAQ updated successfully.');
        return $this->redirect(route('admin.faqs'), navigate: true);
    }

    public function delete()
    {
        $this->faq->delete();
        session()->flash('message', 'FAQ deleted successfully.');
        return $this->redirect(route('admin.faqs'), navigate: true);
    }

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