<?php

namespace App\Components;

use App\Models\Institution\InstitutionType;
use App\Services\HistoryService;
use Livewire\Component;

class InstitutionTypeManager extends Component {

    public $institutionTypes = [];
    public ?string $name = '';

    public bool $show = false;
    public bool $editing = false;
    public $institutionType;

    protected array $rules = [
        'name' => 'required|string|max:255',
    ];

    public function mount() {
        $this->authorize('institution-types.view');
        $this->loadData();
    }

    public function loadData() {
        $this->institutionTypes = InstitutionType::orderBy('display_name')->get()->toArray();
    }

    public function showAdd() {
        $this->resetErrorBag();
        $this->editing = false;
        $this->show = true;
    }

    public function hideAdd() {
        $this->reset('name', 'editing', 'institutionType');
        $this->show = false;
    }

    public function loadEdit(int $id) {
        $this->resetErrorBag();
        $type = InstitutionType::find($id);

        $this->institutionType = $type;
        $this->name = $type->display_name;
        $this->editing = true;

        $this->show = true;
    }

    public function save(HistoryService $historyService) {
        $this->authorize($this->editing ? 'institution-types.edit' : 'institution-types.create');
        $this->validate();

        if ($this->editing) {
            $this->institutionType->update([
                'display_name' => $this->name,
            ]);
            $historyService->add(auth()->user(), "Updated Institution Type '{$this->name}'", [
                'institution_type_id' => $this->institutionType->id,
                'name'                => $this->name,
            ]);
        } else {
            $type = InstitutionType::create([
                'display_name' => $this->name,
            ]);
            $historyService->add(auth()->user(), "Created Institution Type '{$this->name}'", [
                'institution_type_id' => $type->id,
                'name'                => $this->name,
            ]);
        }

        $this->loadData();
        $this->hideAdd();
    }

    public function deleteInstitutionType(HistoryService $service, int $id) {
        $this->authorize('institution-types.delete');
        $type = InstitutionType::findOrFail($id);
        $name = $type->display_name;
        $type->delete();

        $service->add(auth()->user(), "Deleted Institution Type '{$name}'", [
            'institution_type_id' => $id,
            'name'                => $name,
        ]);
        $this->loadData();
    }

    public function placeholder() {
        return <<<'HTML'
        <div class="row">
            <div class="col-12 col-md-6 offset-md-3">
                <div class="card">
                    <div class="card-body">
                        <div class="placeholder-glow">
                            <span class="placeholder col-12 mb-2"></span>
                            <span class="placeholder col-12 mb-2"></span>
                            <span class="placeholder col-8"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        HTML;
    }

    public function render() {
        return view('components.institution-type-manager');
    }
}
