<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Right extends Model
{
    public function getRightList()
    {
    	return Right::orderBy('id','asc')->get();
    }

    public function getRightByModule($module)
    {
    	return Right::where('module',$module)->orderBy('id','asc')->get();
    }

    public function updateRight($object)
    {

       return DB::transaction(function () use ($object) {

            $right = Right::find($object['right']);

            if(isset($right->id))
            {
                $right->module        = $object['module'];
                $right->feature       = $object['feature'];
                $right->icons         = $object['icon'];
                $right->type          = $object['navigation_type'];
                $right->priority      = $object['navigation_priority'];
                $right->status        = $object['status'];

                $right->update();

                return with($right);
            }
            
        });
    }

    public function getRightById($id)
    {
        return Right::where(['id' => $id, 'status' => 'Active'])->first();
    }
}
