<?php

namespace App\Models;

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

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

	const IS_PUBLISH = 1;
    const IS_UNPUBLISH = 2;
	
	// 1=>paket,2=>orang,3=>unit,4=>pcs
	const UNIT_TYPE_PAKET = 1;
	const UNIT_TYPE_ORANG = 2;
	const UNIT_TYPE_UNIT = 3;
	const UNIT_TYPE_PCS = 4;
	const UNIT_TYPE_LITER = 5;
	const UNIT_TYPE_CBM = 6;
	const UNIT_TYPE_KG = 7;
	const UNIT_TYPE_KOLI = 8;
	const UNIT_TYPE_DAY = 9;
	const UNIT_TYPE_HOUR = 10;
	const UNIT_TYPE_MINUTE = 11;
	const UNIT_TYPE_SECOND = 12;
	const UNIT_TYPE_METER = 13;
	const UNIT_TYPE_KM = 14;
	const UNIT_TYPE_GRAM = 15;
	const UNIT_TYPE_METER_PERSEGI = 16;
	const UNIT_TYPE_LOT = 17;
	const UNIT_TYPE_LS = 18;

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

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

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

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

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

    public static function getById($id)
    {
        return DB::select("SELECT * FROM t_unit_types as ut WHERE dlt is null AND id = ? ORDER BY ut.name ASC LIMIT 1;", [$id]);
    }

    public static function getByName($name)
    {
        return DB::select("SELECT * FROM t_unit_types as ut WHERE dlt is null AND name = ? ORDER BY ut.name ASC LIMIT 1;", [$name]);
    }

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

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

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