<?php

namespace App\Models;

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

class LogbookTypes extends Model
{

    const IS_INACTIVE = 0;
    const IS_ACTIVE = 1;

    const defaultSelectedLgbTypes = "
    lgb_type.*
    ";

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

        if (isset($filter['is_active'])) {
            $where .= ' AND lgb_type.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 "
        . LogbookTypes::defaultSelectedLgbTypes . "
        $select
        FROM t_lgb_types as lgb_type
		$join
        WHERE lgb_type.dlt is null
        $where
		$group_by
		$order_by
		$limit
        ;", $params);
    }

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

        if (isset($filter['lgb_type_id'])) {
            $where .= ' AND lgb_type.id = ?';
            $params[] = $filter['lgb_type_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 "
        . LogbookTypes::defaultSelectedLgbTypes . "
        $select
        FROM t_lgb_types as lgb_type
		$join
        WHERE lgb_type.dlt is null
        $where
		$group_by
		$order_by
		$limit
        ;", $params);
    }

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

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

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

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

    public static function addLgbType($data)
    {
        $lgb_type_id = DB::table("t_lgb_types")->insertGetId($data);
        return $lgb_type_id;
    }

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

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