<?php

namespace App\Http\Livewire\People;

use Livewire\Component;
use App\Models\User;
use App\Http\Controllers\Helpers\HelperController;

class InventoryAccountant extends Component
{
    public $listMode = true;
    public $createMode = false;
    public $plants = [];
    public $users = [];
    public $name, $phone, $email, $password, $password_confirmation, $plant_code;

    protected $rules = [
        'password' => ['confirmed'],
        'email' => ['unique:users,email']
    ];

    public function create()
    {
        $this->listMode = false;
        $this->createMode = true;
    }

    public function list()
    {
    }

    public function store()
    {
        $this->validate();
        try {
            HelperController::save_global_level($this->name, $this->email, $this->phone, $this->password, 'Inventory Accountant', 'inventory_accountant', '');
            session()->flash('success', 'Inventory accountant saved successfully');
            $this->reset();
        } catch (\Throwable $th) {
            session()->flash('danger', $th->getMessage());
        }
    }

    public function index()
    {
        return User::where('role', 'inventory_accountant')->get();
    }
    public function render()
    {
        $this->users = $this->index();
        return view('livewire.people.inventory-accountant');
    }
}