<?php

namespace App\View\Components\Form;

use App\View\Components\FormComponent;
use App\View\Concerns\HandlesValidationErrors;
use App\View\Concerns\HasModels;

class Checkbox extends FormComponent
{
    use HandlesValidationErrors;
    use HasModels;

    public string $type = 'checkbox';

    public function __construct(
        public null | string $name = null,
        public null | string $id = null,
        public mixed $value = null,
        public null | string $label = null,
        public null | string $description = '',
        public bool $checked = false,
        public $extraAttributes = '',
    ) {
        $this->id = $this->id ?? $this->name;

        if ($this->name) {
            $this->checked = (bool) old($this->name, $this->checked);
        }
    }

    public function render()
    {
        return view('components.form.checkbox-or-radio');
    }
}
