<?php

namespace App\Livewire\Forms\Studio\Master;

use App\Models\Warehouse;
use Livewire\Form;

class WarehouseForm extends Form
{
    public ?Warehouse $warehouse = null;

    public string $name = '';

    public ?string $description = null;

    public function setWarehouse(Warehouse $warehouse): void
    {
        $this->warehouse = $warehouse;
        $this->name = $warehouse->name;
        $this->description = $warehouse->description;
    }

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:100'],
            'description' => 'nullable',
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'name' => 'nama',
            'description' => 'keterangan',
        ];
    }

    public function store(): void
    {
        $this->validate();

        Warehouse::create($this->except('warehouse'));

        $this->reset();
    }

    public function update(): void
    {
        $this->validate();

        $this->warehouse->update($this->except('warehouse'));
    }

    public function delete(): void
    {
        $this->warehouse->delete();
    }
}
