<?php

namespace App\Http\Livewire\People;

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

class SystemAdmin 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,'System Admin', 'system_admin');
            $this->reset();
            session()->flash('success', 'System Admin saved successfully');
        } catch (\Throwable $th) {
            session()->flash('danger',$th->getMessage());
        }
    }

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