<?php

namespace App\Models;

use Exception;

class PermissionMenuModel extends BaseModel
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'permission';
    protected $appends = ["url","component"];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    protected $dateFormat = 'U';

    protected $guarded = [];


    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [ ];

    public function children ()
    {
        return $this->hasMany("\App\Models\PermissionMenuModel","parent_id","id")
            ->with("children");
    }


    protected function getHasLinkAttribute ()
    {
        return $this->url?true:false;
    }



    public function getMenuDetailArrayAttribute ()
    {
        try
        {
            return json_decode ( $this->menu_detail, true );
        } catch ( Exception $e )
        {
            return [];
        }
    }

    public function getUrlAttribute (  )
    {
        try
        {
            return $this->menuDetailArray[ "url" ];
        } catch ( Exception $e )
        {
            return null;
        }
    }

    public function getComponentAttribute (  )
    {
        try
        {
            return $this->menuDetailArray[ "component" ];
        } catch ( Exception $e )
        {
            return null;
        }
    }

}
