<?php

namespace App\Models;

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

class OrdersVendors extends Model
{
    const STTS_WAIT = 1;
    const STTS_ACC = 2;
    const STTS_REJECT = 3;
    const STTS_IGNORE = 4;
    const STTS_NOT_READY = 5; // want order but expired, bcs not fill data

    const IS_NOT_PAY = 1;
    const IS_PAYED = 2;

    const LINK_WILL_EXP = 1;
    const LINK_NOT_EXP = 2;

    const IS_MAILING_BID_NOT = 0;
    const IS_MAILING_BID_SEND = 1;

    const IS_WANT_IGNORE = 0;
    const IS_WANT_NO = 1;
    const IS_WANT_YES = 2;

    public static function get()
    {
        return DB::select("SELECT * FROM t_orders_vendors;");
    }

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

    public static function getByOrdCode($code)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors WHERE ord_code = ? LIMIT 1;",
            [$code]
        );
    }

    public static function getByOrdCodeByStatus($code, $status)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors WHERE ord_code = ? AND status = ? LIMIT 1;",
            [$code, $status]
        );
    }

    public static function getByOrdIdByStatus($id, $status)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors WHERE ord_id = ? AND status = ? LIMIT 1;",
            [$id, $status]
        );
    }

    public static function getByOrdId($ordid)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors WHERE ord_id = ? LIMIT 1;",
            [$ordid]
        );
    }

    public static function getByBidToken($tkn)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors WHERE bid_token = ? LIMIT 1;",
            [$tkn]
        );
    }

    public static function getByOrdIdAndVdrId($ordid, $vdrid)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors WHERE ord_id = ? AND vdr_id = ? LIMIT 1;",
            [$ordid, $vdrid]
        );
    }

    public static function getNextVendors($ordid, $not_vdr_id = 0)
    {
        // AND is_mailing_bid = " . OrdersVendors::IS_MAILING_BID_NOT . "
        return DB::select(
            "SELECT * FROM t_orders_vendors
        WHERE ord_id = ?
        AND is_want = " .
                OrdersVendors::IS_WANT_IGNORE .
                "
        AND respond_at = 0
        AND vdr_id != ?
        ;",
            [$ordid, $not_vdr_id]
        );
    }

    public static function getOtherVendorsWantThisOrder($ordid, $not_vdr_id = 0)
    {
        return DB::select(
            "SELECT * FROM t_orders_vendors
        WHERE ord_id = ?
        AND is_mailing_bid = " .
                OrdersVendors::IS_MAILING_BID_SEND .
                "
        AND is_want = " .
                OrdersVendors::IS_WANT_YES .
                "
        AND status = " .
                OrdersVendors::STTS_WAIT .
                "
        AND respond_at = 0
        AND vdr_id != ?
        ;",
            [$ordid, $not_vdr_id]
        );
    }

    public static function searchVendorsByRate($filter = [])
    {
        $params = [];

        $query = "SELECT";
        $query .=
            " vendor.id as vdr_id,vendor.first_name,vendor.phone,vendor.phone_code,vendor.email,vendor.fulladdress";
        $query .=
            " ,rate.vdr_id as rate_vdr_id,rate.vhc_type as rate_vhc_type,rate.origin_prov as rate_origin_prov,rate.dest_city as rate_dest_city,rate.dest_district as rate_dest_district";
        $query .=
            " ,rate.fast_time as rate_fast_time,rate.long_time as rate_long_time,rate.sell_kg as rate_sell_kg,rate.sell_cbm as rate_sell_cbm,rate.sell_ftl as rate_sell_ftl";
        $query .= " FROM t_conf_rates AS rate";
        $query .= " INNER JOIN t_vehicles as v ON v.type_id = rate.vhc_type";
        $query .= " INNER JOIN t_users as vendor ON rate.vdr_id = vendor.id";
        $query .= " WHERE rate.vdr_id != 0 AND rate.dest_city != 0";
        $query .=
            " AND rate.origin_prov = ? AND (rate.dest_city = ? OR rate.dest_district = ?) AND rate.sell_ftl = ? AND rate.long_time = ?";
        array_push(
            $params,
            $filter["active_rates"]->origin_prov,
            $filter["active_rates"]->dest_city,
            $filter["active_rates"]->dest_district,
            $filter["active_rates"]->sell_ftl,
            $filter["active_rates"]->long_time
        );
        if ($filter["prefer_truck_type"]) {
            $query .= " AND v.type_id = ?";
            $params[] = $filter["prefer_truck_type"];
        }
        $query .= " GROUP BY rate.vdr_id";
        return DB::select($query, $params);
    }

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

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

    public static function updtByOrdId($ord_id, $data)
    {
        return DB::table("t_orders_vendors")
            ->where("ord_id", $ord_id)
            ->update($data);
    }

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

    public static function dltByOrdId($ord_id)
    {
        return DB::table("t_orders_vendors")
            ->where("ord_id", $ord_id)
            ->delete();
    }
}
