<?php

namespace App\Models;

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

class Clients extends Model
{
    const CSTTS_ACTIVE = 1;
    const CSTTS_INACTIVE = 2;

    const CCREDENTIALS_CREATE = 1;
    const CCREDENTIALS_NOT = 2;

    const DEFAULT_PHONE_CODE = 62;

    const DEFAULT_CID = 1; // swanusa account

    const DISC_TYPE_NO = 0;
    const DISC_TYPE_FIX = 1;
    const DISC_TYPE_PERCENT = 2;

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

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

        if (isset($filter["c_status"])) {
            $where .= " AND c.c_status = ?";
            $params[] = $filter["c_status"];
        }

        return DB::select(
            "SELECT c.*
		,c.id as client_id,u.id as user_id
		,c.crt as join_date
        ,(SELECT COUNT(id) FROM t_orders_clients WHERE c_pt_id = c.id) as count_trx
        " .
                $select .
                "
		FROM t_clients AS c
		LEFT JOIN t_users AS u ON c.id = u.client_id
        " .
                $join .
                "
		WHERE c.dlt is null
        " .
                $where .
                "
        GROUP BY c.id
        ;",
            $params
        );
    }

    public static function showClientById($cid)
    {
        $params = [$cid];
        return DB::select(
            "SELECT c.*
		,c.id as client_id,u.id as user_id
		,c.crt as join_date
		FROM t_clients AS c
		LEFT JOIN t_users AS u ON c.id = u.client_id
		WHERE c.dlt is null
		AND c.id = ?
		LIMIT 1;",
            $params
        );
    }

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

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

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

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

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

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

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

    public static function addClient($data)
    {
        $cid = DB::table("t_clients")->insertGetId($data);
        return $cid;
    }

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

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

    public static function select2Client($cid = null)
    {
        $query = "";
        $params = [];

        if ($cid) {
            $query .= "SELECT id,c_name as name FROM t_clients AS c WHERE dlt is null AND c.id = ?;";
            $params[] = $cid;
        } else {
            $query .= "SELECT id,c_name as name FROM t_clients AS c WHERE dlt is null;";
        }

        return DB::select($query, $params);
    }

    public static function arrDiscountTypes()
    {
        return [Clients::DISC_TYPE_NO, Clients::DISC_TYPE_FIX, Clients::DISC_TYPE_PERCENT];
    }

    public static function select2DiscountTypes()
    {
        return [
            [
                "id" => Clients::DISC_TYPE_NO,
                "name" => "No Discount",
            ],
            [
                "id" => Clients::DISC_TYPE_FIX,
                "name" => "Fix Amount",
            ],
            [
                "id" => Clients::DISC_TYPE_PERCENT,
                "name" => "Percent Amount",
            ],
        ];
    }
}
