<?php

namespace App\Models;

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

class LogbookKeys extends Model
{
    const IS_INACTIVE = 0;
    const IS_ACTIVE = 1;

    const defaultSelectedLgbKeys = "
    lgb_key.*
    ";

    public static function listLgbKeys($filter = [])
    {
        $select = ''; $join = ''; $where = ''; $group_by = ''; $order_by = '';  $limit = '';
        $params = [];

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

		if (isset($filter['group_by'])) $where .= ' GROUP BY ' . $filter['group_by'];
		if (isset($filter['order_by'])) $where .= ' ORDER BY ' . $filter['order_by'];
		if (isset($filter['limit'])) $where .= ' LIMIT ' . $filter['limit'];

        return DB::select("SELECT "
        . LogbookKeys::defaultSelectedLgbKeys . "
        $select
        FROM t_lgb_keys as lgb_key
		$join
        WHERE lgb_key.dlt is null
        $where
		$group_by
		$order_by
		$limit
        ;", $params);
    }

	public static function showLgbKey($filter = [])
    {
        $select = ''; $join = ''; $where = ''; $group_by = ''; $order_by = '';  $limit = '';
        $params = [];

        if (isset($filter['lgb_key_id'])) {
            $where .= ' AND lgb_key.id = ?';
            $params[] = $filter['lgb_key_id'];
        }

		if (isset($filter['group_by'])) $where .= ' GROUP BY ' . $filter['group_by'];
		if (isset($filter['order_by'])) $where .= ' ORDER BY ' . $filter['order_by'];
		if (isset($filter['limit'])) $where .= ' LIMIT ' . $filter['limit'];

        return DB::select("SELECT "
        . LogbookKeys::defaultSelectedLgbKeys . "
        $select
        FROM t_lgb_keys as lgb_key
		$join
        WHERE lgb_key.dlt is null
        $where
		$group_by
		$order_by
		$limit
        ;", $params);
    }

    public static function getLgbKeys()
    {
        return DB::select("SELECT * FROM t_lgb_keys WHERE dlt is null;");
    }

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

    public static function getLgbKeyByCode($code)
    {
        return DB::select("SELECT * FROM t_lgb_keys WHERE dlt is null AND code = ? LIMIT 1;", [$code]);
    }

	public static function getLgbKeyByPckDrop($pck_id, $drop_id)
    {
        return DB::select("SELECT * FROM t_lgb_keys WHERE dlt is null AND pck_id = ? AND drop_id = ? LIMIT 1;", [$pck_id, $drop_id]);
    }

    public static function addLgbKey($data)
    {
        $lgb_key_id = DB::table("t_lgb_keys")->insertGetId($data);
        return $lgb_key_id;
    }

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

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