<?php

namespace App\Actions;

use LaravelViews\Actions\Action;
use LaravelViews\Views\View;
use LaravelViews\Actions\Confirmable;
use App\Models\Especiality;

class ActiveEspeciality extends Action
{
    use Confirmable;
    /**
     * Any title you want to be displayed
     * @var String
     * */
    public $title;

    /**
     * This should be a valid Feather icon string
     * @var String
     */
    public $icon = "check";

     /**
     * 
     */
    public function __construct(){
        parent::__construct();
        $this->title = __("common.viewActive");
    }

    /**
     * Execute the action when the user clicked on the button
     *
     * @param $model Model object of the list where the user has clicked
     * @param $view Current view where the action was executed from
     */
    public function handle($model, View $view)
    {
        try {
            if (!$model->active) {
                $model->active = true;
                $model->save();
                $this->success(__("common.success_operation"));
            } else {
                $this->error(__("common.already_active"));
            }
        } catch (\Exception $e) {
            $this->error(__("common.error_operation"));
        }
    }

    public function getConfirmationMessage($item = null)
    {
        return __('¿Está seguro de continuar con este proceso?');
    }

     /**
     * 
     */
    public function renderIf($model, View $view){
        return !$model->active;
    }
} 