<?php

namespace App\Models;

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

class ConfRates extends Model
{

    const IS_ACTIVE = 1;
    const IS_INACTIVE = 2;

    const UNIT_DAY = 1;

    const LANE_EARTH = 1;
    const LANE_SEA = 2;

    const CRT_TYPE_SYSTEM = 0;
    const CRT_TYPE_ADMIN = 1;

    const defaultSelectedRates = "
    rates.*
    ,(SELECT name FROM t_conf_lanes WHERE id = rates.lane LIMIT 1) as lane_name
    ,(SELECT nmProvinsiKel FROM t_region WHERE kodeProv = rates.origin_prov LIMIT 1) as origin_prov_name
    ,(SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = rates.origin_city LIMIT 1) as origin_city_name
    ,(SELECT nmProvinsiKel FROM t_region WHERE kodeProv = rates.dest_prov LIMIT 1) as dest_prov_name
    ,(SELECT nmKotamadyaKel FROM t_region WHERE kodeKab = rates.dest_city LIMIT 1) as dest_city_name
    ,(SELECT nmKecamatanKel FROM t_region WHERE kodeKec = rates.dest_district LIMIT 1) as dest_district_name
    ,u.first_name as vdr_name,u.email as vdr_mail,vt.name as vhc_type_name
    ";

    public static function listRates()
    {
        return DB::select("SELECT "
        . ConfRates::defaultSelectedRates . "
        FROM t_conf_rates as rates
        LEFT JOIN t_users as u ON rates.vdr_id = u.id
        LEFT JOIN t_vehicles_types as vt ON rates.vhc_type = vt.id
        WHERE rates.dlt is null;");
    }

    public static function showRateById($rid)
    {
        return DB::select("SELECT "
        . ConfRates::defaultSelectedRates . "
        FROM t_conf_rates as rates
        LEFT JOIN t_users as u ON rates.vdr_id = u.id
        LEFT JOIN t_vehicles_types as vt ON rates.vhc_type = vt.id
        WHERE rates.dlt is null AND rates.id = ?
        LIMIT 1;", [$rid]);
    }

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

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

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

    // // start packing list data
    // public static function getRateByDestCity($ori_prid, $dest_ktid)
    // {
    //     return DB::select("SELECT * FROM t_conf_rates WHERE dlt is null AND is_active = " . ConfRates::IS_ACTIVE . " AND vdr_id != 0 AND vhc_type != 0 AND origin_prov = ? AND dest_city = ? GROUP BY long_time,sell_cbm,sell_kg ORDER BY fast_time ASC;", [$ori_prid, $dest_ktid]);
    // }

    // public static function getRateByDestDistrict($ori_prid, $dest_kcid)
    // {
    //     return DB::select("SELECT * FROM t_conf_rates WHERE dlt is null AND is_active = " . ConfRates::IS_ACTIVE . " AND vdr_id != 0 AND vhc_type != 0 AND origin_prov = ? AND dest_district = ? GROUP BY long_time,sell_cbm,sell_kg ORDER BY fast_time ASC;", [$ori_prid, $dest_kcid]);
    // }
    // // end packing list data

    // // start without packing list data
    public static function getRateByDestCity($ori_prid, $dest_ktid)
    {
        return DB::select("SELECT * FROM t_conf_rates WHERE dlt is null AND is_active = " . ConfRates::IS_ACTIVE . " AND vdr_id != 0 AND vhc_type != 0 AND origin_prov = ? AND dest_city = ? GROUP BY long_time,sell_ftl ORDER BY fast_time ASC;", [$ori_prid, $dest_ktid]);
    }

    public static function getRateByDestDistrict($ori_prid, $dest_kcid)
    {
        return DB::select("SELECT * FROM t_conf_rates WHERE dlt is null AND is_active = " . ConfRates::IS_ACTIVE . " AND vdr_id != 0 AND vhc_type != 0 AND origin_prov = ? AND dest_district = ? GROUP BY long_time,sell_ftl ORDER BY fast_time ASC;", [$ori_prid, $dest_kcid]);
    }
    // // end without packing list data

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

        if (isset($filter['origin_prov_dest_ktid_dest_kcid_vhc_type_sell_ftl_long_time'])) {
            $where .= ' AND ((origin_prov = ? AND dest_city = ?) OR (origin_prov = ? AND dest_district = ?)) AND vhc_type = ? AND sell_ftl = ? AND long_time = ?';
            array_push($params, $filter['origin_prov'], $filter['dest_city'], $filter['origin_prov'], $filter['dest_district'], $filter['vhc_type'], $filter['sell_ftl'], $filter['long_time']);
        }

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

        return DB::select("SELECT
        rate.*
        $select
        FROM t_conf_rates as rate
        $join
        WHERE rate.dlt is null
        $where
        ORDER BY fast_time ASC;", $params);
    }

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

    public static function addRate($data)
    {
        $rid = DB::table("t_conf_rates")->insertGetId($data);
        return $rid;
    }

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

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

    /**
     * t_conf_lane
     */

    public static function getLanesActive()
    {
        return DB::select("SELECT * FROM t_conf_lanes WHERE dlt is null AND is_active = 1;");
    }
}
