<?php

namespace App\Models;

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

class ConfTruckTypes extends Model
{

    const IS_ACTIVE = 1;
    const IS_INACTIVE = 2;

    const IS_PUBLISH = 1;
    const IS_UNPUBLISH = 2;

    const defaultSelectedTruckTypes = "
    tt.*
	,vt.name as type_name,vt.desc as type_desc
    ";

    public static function listTruckTypes($isActive = 0, $filter = [])
    {
        $params = [];
        $whereIsActive = '';
        if ($isActive != 0) {
            $whereIsActive = ' AND vt.is_active = ' . ConfTruckTypes::IS_ACTIVE;
        }

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

        return DB::select("SELECT " . ConfTruckTypes::defaultSelectedTruckTypes . " FROM t_conf_truck_types as tt LEFT JOIN t_vehicles_types as vt ON tt.type_id = vt.id WHERE tt.dlt is null " . $whereIsActive . " ;", $params);
    }

    public static function listTruckTypesRates($isActive = 0, $lane)
    {
        $params = [];
        $params[] = $lane;

        $whereIsActive = '';
        if ($isActive != 0) {
            $whereIsActive = ' AND vt.is_active = ' . ConfTruckTypes::IS_ACTIVE;
        }
        
        return DB::select("SELECT "
        . ConfTruckTypes::defaultSelectedTruckTypes . "
        FROM t_conf_truck_types as tt
        LEFT JOIN t_vehicles_types as vt ON tt.type_id = vt.id
        INNER JOIN t_conf_rates as rate ON tt.type_id = rate.vhc_type
        WHERE tt.dlt is null "
        . $whereIsActive . "
        AND rate.lane = ?
        GROUP BY vhc_type
        ;", $params);
    }

    public static function showTruckTypeById($ttid)
    {
        return DB::select("SELECT " . ConfTruckTypes::defaultSelectedTruckTypes . " FROM t_conf_truck_types as tt LEFT JOIN t_vehicles_types as vt ON tt.type_id = vt.id WHERE tt.dlt is null AND tt.id = ? LIMIT 1;", [$ttid]);
    }

	public static function getTruckTypeByName($type_name)
    {
        return DB::select("SELECT " . ConfTruckTypes::defaultSelectedTruckTypes . " FROM t_conf_truck_types as tt LEFT JOIN t_vehicles_types as vt ON tt.type_id = vt.id WHERE tt.dlt is null AND vt.name LIKE ? LIMIT 2;", ['%'.$type_name.'%']);
    }

    public static function addTruckType($data)
    {
        $ttid = DB::table("t_conf_truck_types")->insertGetId($data);
        return $ttid;
    }

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

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

	/**
	 * t_vehicles_types
	 */

    public static function getTypeById($id)
	{
		return DB::select("SELECT * FROM t_vehicles_types WHERE id = ? LIMIT 1;", [$id]);
	}

	public static function addTypes($data)
	{
		return DB::table("t_vehicles_types")->insertGetId($data);
	}

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