<?php

namespace App\Http\Livewire;

use Livewire\Component;

class User extends Component
{
    public $records, $name, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;

    protected $rules = [
        'name' => 'required',
        'email' => 'required',
        'password' => 'required'
    ];

    protected $messages = [
        'name.required' => 'Il nome è obbligatorio',
        'email.required' => 'La mail è obbligatoria',
        'password.required' => 'La password è obbligatoria',
    ];

    public function resetFields(){
        $this->name = '';
        $this->email = '';
        $this->password = '';
        $this->oldPassword = '';
        $this->level = 0;
        $this->enabled = true;
        $this->emit('load-data-table');
    }

    public function render()
    {
        $this->records = \App\Models\User::select('id', 'name', 'email', 'password', 'level', 'enabled')->get();
        return view('livewire.user');
    }

    public function add()
    {
        $this->resetFields();
        $this->add = true;
        $this->update = false;
    }

    public function store()
    {
        $this->validate();
        try {
            \App\Models\User::create([
                'name' => $this->name,
                'email' => $this->email,
                'password' => bcrypt($this->password),
                'level' => $this->level,
                'enabled' => $this->enabled ? 1 : 0
            ]);
            session()->flash('success','Dato creato');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function edit($id){
        try {
            $user = \App\Models\User::findOrFail($id);
            if( !$user) {
                session()->flash('error','Dato non trovato');
            } else {
                $this->name = $user->name;
                $this->email = $user->email;
                $this->oldPassword = $user->password;
                $this->level = $user->level;
                $this->enabled = $user->enabled == 1;
                $this->dataId = $user->id;
                $this->update = true;
                $this->add = false;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function update()
    {
        $this->validate(
            [
                'name' => 'required',
                'email' => 'required',
            ]
        );
        try {
            \App\Models\User::whereId($this->dataId)->update([
                'name' => $this->name,
                'email' => $this->email,
                'password' => $this->password != '' ? bcrypt($this->password) : $this->oldPassword,
                'level' => $this->level,
                'enabled' => $this->enabled ? 1 : 0
            ]);
            session()->flash('success','Dato aggiornato');
            $this->resetFields();
            $this->update = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function cancel()
    {
        $this->add = false;
        $this->update = false;
        $this->resetFields();
    }

    public function delete($id)
    {
        try{
            \App\Models\User::find($id)->delete();
            session()->flash('success',"Dato eliminato");
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }
}
