<?php


namespace App\Models\SettingManagement\Traits;


use Illuminate\Database\Eloquent\Model;

trait HasStatusAttributes{


    //ACTIONS
    /**
     * @param string $event
     */
    public function statuffy($event = "created"){
        $statusColumn = "status_code"; // "status_code"
        $datas = [$statusColumn => $this->{$statusColumn} ];
        $datas["created_at"] = now();
        $datas["created_by"] =  auth(activeGuard())->id() ?? null;

        //si c'est un update
        if ($event == "updated"){
            $changes = $this->changes ?? [] ;

            if ($changes){
                //si le statut a été modifié
                if (isset($changes[$statusColumn])){
                    $this->statusChanges()->create($datas);
                }
            }
        }else{
            $this->statusChanges()->create($datas);
        }
    }

    /**
     * @param null $code
     * @return mixed
     */
    public function setStatusTo($code){
        return $this->update(["status_code" => $code]);
    }


    //GETTERS
    public function getNameAttribute($value){
        return __($value);
    }

    public function getDotAttribute(){
        return html_entity_decode("<i class='badge-dot' style='background-color: $this->color !important;'></i>");
    }

    public function getBadgeDotAttribute(){
        return html_entity_decode("<i class='badge-dot' style='background-color: $this->color !important;'></i> $this->name");
    }


    public function getBadgeLabelAttribute(){
        $status = $this;
        $style = (is_array($status->style))
            ?  json_encode($status->style['label']) ?? ""
            : $status->style ?? "";

        $style = str_replace("{","",$style);
        $style = str_replace("}","",$style);
        $style = str_replace('"',"",$style);
        $style = str_replace(',',"",$style);

        return html_entity_decode("<label class='badge' style='$style'><strong>$status->name</strong></label>");
    }

}
