<?php

namespace App\Models;

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

class DataTypes extends Model
{

    const IS_INACTIVE = 0;
    const IS_ACTIVE = 1;

    const defaultSelectedDataTypes = "
    dtypes.*
    ";

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

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

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

        if (isset($filter['data_types_id'])) {
            $where .= ' AND dtypes.id = ?';
            $params[] = $filter['data_types_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 "
        . DataTypes::defaultSelectedDataTypes . "
        $select
        FROM t_datatypes as dtypes
		$join
        WHERE dtypes.dlt is null
        $where
		$group_by
		$order_by
		$limit
        ;", $params);
    }

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

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

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

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

    public static function addDataType($data)
    {
        $data_types_id = DB::table("t_datatypes")->insertGetId($data);
        return $data_types_id;
    }

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

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