<?php

namespace App\Components\Template;

use App\Models\Contact\ContactCategory;
use App\Models\Contact\Template;
use Livewire\Component;

class Index extends Component {
    public $showCategoryForm = false;
    public $newCategoryName;

    public $templates;

    public function mount() {
        $this->authorize('templates.view');
        $this->templates = Template::all();
    }

    public function saveCategory() {
        $this->authorize('templates.create');
        $this->validate(['newCategoryName' => 'required|unique:contact_categories,name']);
        ContactCategory::create(['name' => $this->newCategoryName]);
        $this->newCategoryName = '';
        $this->showCategoryForm = false;
    }

    public function deleteTemplate($id) {
        $this->authorize('templates.delete');
        Template::destroy($id);
    }

    public function render() {
        return view('components.template.index');
    }
}