<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\Orders;

class Drivers extends Model
{
    const DEFAULT_PHONE_CODE = 62;

    const STTS_ACTIVE = 1;
    const STTS_INACTIVE = 2;

    // const IN_ORD_YES = 1;
    // const IN_ORD_NO = 2;
    const IN_ORD_YES = 2;
    const IN_ORD_NO = 1;

    private const defaultSelectDriver = "d.*,dt.*,d.id,d.crt,d.crt_by,d.updt,d.updt_by,d.dlt,d.dlt_by";

    public static function listDrivers($auth, $filter = [])
    {
        $params = [];
        $select = "";
        $join = "";
        $where = "";

        if ($auth->role == Users::ROLE_VENDOR) {
            $where .= " AND d.vendor_id = " . $auth->uid;
        }

        if (isset($filter["company"])) {
            $where .= " AND client.id = ?";
            $params[] = $filter["company"];
        }

        // d.nik,d.fullname,d.phone,d.phone_code,d.email,d.dob,d.age,d.gender,d.blood,d.fulladdress,d.crt,d.crt_by,d.updt,d.updt_by,
        // dt.ktp_img,dt.npwp_img,dt.npwp_number,dt.npwp_string,dt.license_img,dt.license_number,dt.license_exp,dt.em_fullname,dt.em_phone,dt.em_phone_code,dt.em_relationship
        return DB::select(
            "SELECT
		" .
                self::defaultSelectDriver .
                "
        ,client.c_name as company_name
        " .
                $select .
                "
		FROM t_drivers as d
		INNER JOIN t_drivers_detail AS dt ON d.id = dt.did
        LEFT JOIN t_users AS vdr ON d.vendor_id = vdr.id
        LEFT JOIN t_clients AS client ON vdr.client_group_id = client.id
        " .
                $join .
                "
		WHERE d.dlt is null
        " .
                $where .
                "
        ORDER BY d.id ASC;",
            $params
        );
    }

    public static function getDrivers($auth, $filter = [])
    {
        $params = [];
        $where_vendor = "";
        $join_vendor = "";
        // if ($auth->role != Users::ROLE_ADMIN) {
        //     $where_vendor .= " AND d.vendor_id = " . $auth->uid;
        // }
        if (isset($filter["status"])) {
            $where_vendor .= " AND d.status = ?";
            array_push($params, $filter["status"]);
        }
        return DB::select("SELECT * FROM t_drivers as d WHERE dlt is null " . $where_vendor . ";", $params);
    }

    public static function showDriverById($did)
    {
        return DB::select(
            "SELECT
        " .
                self::defaultSelectDriver .
                "
        FROM t_drivers AS d
        INNER JOIN t_drivers_detail AS dt ON d.id = dt.did
        WHERE d.dlt is null AND d.id = ? LIMIT 1;",
            [$did]
        );
    }

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

    public static function getDriverByEmail($email)
    {
        return DB::select("SELECT * FROM t_drivers WHERE dlt is null AND email = ? LIMIT 2;", [$email]);
    }

    public static function getDriverByPhone($phone)
    {
        return DB::select("SELECT * FROM t_drivers WHERE dlt is null AND phone = ? LIMIT 2;", [$phone]);
    }

    public static function getDriverByNik($nik)
    {
        return DB::select("SELECT * FROM t_drivers WHERE dlt is null AND nik = ? LIMIT 2;", [$nik]);
    }

    public static function getDriverByIdAllData($did)
    {
        return DB::select(
            "SELECT
        *,d.id as drv_id
        FROM t_drivers as d
        INNER JOIN t_drivers_detail as dt ON d.id = dt.did
        WHERE d.dlt is null
        AND d.id = ?
        LIMIT 1;",
            [$did]
        );
    }

    public static function getDriversNoInOrder($auth, $filter = [])
    {
        $params = [];
        $where_vendor = "";
        $join_vendor = "";
        if ($auth->role != Users::ROLE_ADMIN) {
            $where_vendor .= " AND d.vendor_id = " . $auth->uid;
        }
        if (isset($filter["status"])) {
            $where_vendor .= " AND d.status = ?";
            array_push($params, $filter["status"]);
        }
        return DB::select(
            "SELECT
        d.*,ord.status as ord_status
        FROM t_drivers as d
        LEFT JOIN (SELECT MAX(ord_id) as max_ord_id,drv_id FROM t_orders_drivers GROUP BY drv_id) as ord_drv1 ON (d.id = ord_drv1.drv_id)
        LEFT JOIN t_orders as ord ON (ord_drv1.max_ord_id = ord.id)
        WHERE d.dlt is null
        AND (ord.status is null OR ord.status IN (" .
                Orders::STTS_CLIENT_PAY .
                "," .
                Orders::STTS_VENDOR_PAYED .
                "," .
                Orders::STTS_CLOSE .
                "," .
                Orders::STTS_CANCEL .
                "))
        " .
                $where_vendor .
                "
        ;",
            $params
        );
    }

    public static function getDriversNoInOrderNew($auth, $filter = [])
    {
        $params = [];
        $where_vendor = "";
        $join_vendor = "";
        if ($auth->role != Users::ROLE_ADMIN) {
            $where_vendor .= " AND d.vendor_id = " . $auth->uid;
        }
        if (isset($filter["status"])) {
            $where_vendor .= " AND d.status = ?";
            array_push($params, $filter["status"]);
        }
        return DB::select(
            "SELECT
        d.*
        FROM t_drivers as d
        WHERE d.dlt is null
        AND d.is_in_ord = " .
                Drivers::IN_ORD_NO .
                "
        " .
                $where_vendor .
                "
        ;",
            $params
        );
    }

    public static function likeName($name)
    {
        $params = ["%" . $name . "%"];

        return DB::select(
            "SELECT drv.*,drv_dtl.bank_id,drv_dtl.bank_code,drv_dtl.bank_name,drv_dtl.bank_short_name,drv_dtl.bank_branch_name,drv_dtl.bank_acc_number,drv_dtl.bank_acc_name FROM t_drivers as drv INNER JOIN t_drivers_detail as drv_dtl ON drv.id = drv_dtl.did WHERE dlt is null AND fullname LIKE ?;",
            $params
        );
    }

    public static function addDriver($data)
    {
        $did = DB::table("t_drivers")->insertGetId($data);
        return $did;
    }

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

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

    public static function listRelationships()
    {
        // ENUM('wife', 'husband', 'sister', 'brother', 'father', 'mother', 'uncle', 'aunt', 'daughter', 'children', 'grandfather', 'grandmother', 'nephew', 'niece', 'family', 'sibling', 'son', 'cousin')
        return [
            [
                "id" => "wife",
                "name" => "Wife",
            ],
            [
                "id" => "husband",
                "name" => "Husband",
            ],
            [
                "id" => "sister",
                "name" => "Sister",
            ],
            [
                "id" => "brother",
                "name" => "Brother",
            ],
            [
                "id" => "father",
                "name" => "Father",
            ],
            [
                "id" => "mother",
                "name" => "Mother",
            ],
            [
                "id" => "uncle",
                "name" => "Uncle",
            ],
            [
                "id" => "aunt",
                "name" => "Aunt",
            ],
            [
                "id" => "cousin",
                "name" => "Cousin",
            ],
            [
                "id" => "daughter",
                "name" => "Daughter",
            ],
            [
                "id" => "son",
                "name" => "Son",
            ],
            [
                "id" => "children",
                "name" => "Children",
            ],
            [
                "id" => "grandfather",
                "name" => "Grandfather",
            ],
            [
                "id" => "grandmother",
                "name" => "Grandmother",
            ],
            [
                "id" => "nephew",
                "name" => "Nephew",
            ],
            [
                "id" => "niece",
                "name" => "Niece",
            ],
            [
                "id" => "family",
                "name" => "Family",
            ],
            [
                "id" => "sibling",
                "name" => "Sibling",
            ],
        ];
    }
}
