<?php

namespace App\Components\Institution;

use App\Models\Dsar\DsarType;
use App\Models\Institution\Institution;
use App\Models\Institution\InstitutionType;
use Livewire\Component;

class Create extends Component {

    public $display_name;

    public $institution_types;
    public $institution_type_id;

    public $dsar_types;
    public $dsar_type_id;
    public $send_type;

    public $rules = [
        'institution_type_id' => 'required|integer|exists:institution_types,id',
        'dsar_type_id' => 'required|integer|exists:dsar_types,id',
        'display_name' => 'required|string|max:255|unique:institutions,display_name',
        'send_type' => 'nullable|string|in:post,email,both',
    ];

    public function mount() {
        $this->institution_types = InstitutionType::pluck('display_name', 'id');
        $this->dsar_types = DsarType::pluck('display_name', 'id');
    }

    public function saveInstitution() {
        $this->validate();

        $institution = Institution::create([
            'institution_type_id' => $this->institution_type_id,
            'dsar_type_id' => $this->dsar_type_id,
            'display_name' => $this->display_name,
            'send_type' => $this->send_type ?: null,
        ]);

        return redirect()->route('institutions.show', $institution);
    }


    public function render() {
        return view('components.institution.create');
    }
}
