<?php

namespace App\Http\Livewire\People;

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

class GeneralManager extends Component
{
    public $listMode = true;
    public $createMode = false;
    public $plants = [];
    public $users = [];
    public $companies = [];
    public $company_code;
    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, 'General Manager', 'general_manager', $this->company_code);
            $this->reset();
            session()->flash('success', 'General Manager saved successfully');
        } catch (\Throwable $th) {
            session()->flash('danger', $th->getMessage());
        }
    }

    public function get_companies()
    {
        return Company::get();
    }
    public function index()
    {
        return User::where('role', 'general_manager')->with('companies')->get();
    }
    public function render()
    {
        $this->users = $this->index();
        $this->companies = $this->get_companies();
        return view('livewire.people.general-manager');
    }
}