<?php
/*=========================================================================================
    App Name  : eCase File PTUN Jakarta
    File Name : RoleAction
    --------------------------------------------------------------------------------------
    Author    : Fuad Auliya Rohman
    Email     : fuad.dev16@gmail.com
==========================================================================================*/

namespace Modules\Role\Livewire;

use Livewire\Component;
use Modules\Role\Services\RoleServiceInterface;

class RoleAction extends Component
{
    public $name;
    public $table;
    public $targetModal;
    public $data;
    public $updateMode;

    public $count;

    protected $listeners = [
        'createRole', 'updateRole'
    ];

    public function render()
    {
        return view('role::livewire.role-action');
    }

    public function mount()
    {
        if ($this->updateMode) {
            $this->name = $this->data->name;
        }
    }

    private function sanitize()
    {
        $this->name = sanitize($this->name);
    }

    private function resetProperty()
    {
        $this->reset(['name']);
        $this->resetErrorBag();
        $this->resetValidation();
    }

    public function createRole(RoleServiceInterface $roleService)
    {
        // SANITIZE
        $this->sanitize();

        // VALIDASI
        $this->validate([
            'name' => ['required', 'string', 'alpha_dash', 'unique:roles,name'],
        ]);

        try {
            $role = $roleService->create([
                'name' => $this->name,
                'guard_name' => config('auth.defaults.guard'),
            ]);

            // RESET
            $this->resetProperty();

            // CLOSE MODAL
            $this->emit('closeModalAndRefreshTable', ['table' => $this->table, 'targetModal' => $this->targetModal,]);

            $this->dispatchBrowserEvent('alert:success', [
                'message' => __('messages.save_success', ['action' => __('lang.groups'),]),
            ]);
        } catch (\Exception $e) {
            // CLOSE MODAL
            $this->emit('closeModal', ['table' => $this->table, 'targetModal' => $this->targetModal,]);

            $this->dispatchBrowserEvent('alert:failed', [
                'message' => __('messages.process_failed', ['action' => __('lang.add'),]),
            ]);

            // WRITE LOG
            writeAppLog($e->getMessage(), 'alert');

            return null;
        }
    }

    public function updateRole(RoleServiceInterface $roleService)
    {
        // SANITIZE
        $this->sanitize();

        // VALIDASI
        $this->validate([
            'name' => ['required', 'string', 'alpha_dash'],
        ]);

        try {
            $role = $roleService->findById($this->data->id);

            $role->update([
                'name' => $this->name,
            ]);

            // RESET
            $this->resetProperty();

            // CLOSE MODAL
            $this->emit('closeModalAndRefreshTable', ['table' => $this->table, 'targetModal' => $this->targetModal,]);

            $this->dispatchBrowserEvent('alert:success', [
                'message' => __('messages.update_success', ['action' => __('lang.groups'),]),
            ]);
        } catch (\Exception $e) {
            // CLOSE MODAL
            $this->emit('closeModal', ['table' => $this->table, 'targetModal' => $this->targetModal,]);

            $this->dispatchBrowserEvent('alert:failed', [
                'message' => __('messages.process_failed', ['action' => __('lang.edit'),]),
            ]);

            // WRITE LOG
            writeAppLog($e->getMessage(), 'alert');

            return null;
        }
    }
}
