<?php

namespace App\Models;

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

class UsersMenuPermissions extends Model
{
	const IS_ACTIVE = 1;
    const IS_INACTIVE = 2;

    const IS_NOPERMISSION = 0;
	const IS_PERMISSION = 1;

    public static function listPermissionsMenus($filter = [])
    {
        $params = [];
        $select = '';
        $where = '';

        if (isset($filter['is_active'])) {
            $where .= ' AND pm.is_active = ?';
            $params[] = $filter['is_active'];
        }

        return DB::select("SELECT
        pm.*
        $select
        FROM
        t_permissions_menus as pm
        WHERE pm.dlt is null
        $where
        ORDER BY pm.id ASC
        ;",
        $params);
    }

    public static function showPermissionsMenusById($id)
    {
        return DB::select("SELECT * FROM t_permissions_menus as pm WHERE dlt is null AND id = ? LIMIT 1;", [$id]);
    }

    public static function get()
    {
        return DB::select("SELECT * FROM t_permissions_menus as pm WHERE dlt is null ORDER BY pm.name ASC;");
    }

    public static function getById($id)
    {
        return DB::select("SELECT * FROM t_permissions_menus as pm WHERE dlt is null AND id = ? LIMIT 1;", [$id]);
    }

    public static function getByName($name, $filter = [])
    {
        $params = [$name];
        $where = '';
        return DB::select("SELECT * FROM t_permissions_menus as pm WHERE dlt is null AND name = ? $where LIMIT 1;", $params);
    }

    public static function add($data)
    {
        $id = DB::table("t_permissions_menus")->insertGetId($data);
        return $id;
    }

    public static function updt($id, $data)
    {
        return DB::table("t_permissions_menus")->where("id", $id)->update($data);
    }

    public static function dlt($id)
    {
        return DB::table("t_permissions_menus")->where("id", $id)->delete();
    }
}
