<?php
namespace src\models;
use \core\Model;
use \core\Database;

class Client extends Model {

	public static function saveClient($client, $note) {
		$data = Client::insert($client)
			->execute();
		
		Client::insert(['id_client' => $data, 'note' => $note])
			->table('clients_notes')
		->execute();
	}

	public static function updateClient($client, $note, $clientId) {
		Client::update($client)
			->where('id', $clientId)
		->execute();
		
		Client::update(['note' => $note])
			->table('clients_notes')
			->where('id_client', $clientId)
		->execute();
	}

	public static function getClients($offset, $limit, $filter) {
		$data = Client::select()
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', $filter['fs_status'], $filter['f_status'])
            ->where($filter['f_state'], $filter['fs_state'], $filter['fv_state'])
            ->where($filter['f_type'], $filter['fs_type'], $filter['fv_type'])
			->orderBy(['status' => '!= 1', 'name' => 'asc'])
			->offset($offset)
			->limit($limit)			
		->get();

		return $data;
	}

	public static function getClientsFromSalesman($salesman) {
		$data = Client::select()
			->where('id_salesman', '=', $salesman)
			->orderBy(['status' => '!= 1', 'name' => 'asc'])
		->get();

		return $data;
	}

	public static function getClient($client) {
		$data = Client::select()
			->where('id', $client)
		->one();

		$note = Client::select('note')
			->table('clients_notes')
			->where('id_client', $client)
		->one();

		$data['note'] = $note['note'];
		return $data;
	}

	// Busca de clientes via texto via ajax
	public static function searchByText($text) {
		$data = Client::select()
			->where('name', 'like', '%'.$text.'%')
			->orderBy(['name' => 'asc'])
			->limit(20)
		->get();

		return $data;
	}

	// Busca de clientes via texto e campo expecífico via ajax
	public static function searchBy($where, $text) {
		$data = Client::select()
			->where($where, 'like', '%'.$text.'%')
			->orderBy(['name' => 'asc'])
			->limit(20)
		->get();

		return $data;
	}

	public static function getName($client) {
		$data = Client::select('name')
			->where('id', $client)
		->one();

		return $data['name'];
	}

	public static function getCount($filter) {
		$c = Client::select()
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', $filter['fs_status'], $filter['f_status'])
		->count();

		return $c;
	}	

	public static function existsClient($ref, $value) {
		$response = false;
		$c = Client::select('id')
            ->where($ref, $value)
        ->count();

        if($c > 0) {
            $response = true;
        }
        return $response;
	}

	public static function getBirthdays($dateInit, $dateEnd) {
		$data = array();		
		$db = Database::getInstance();

		$sql = $db->prepare("
			SELECT *,
			(select citys.name from citys where citys.id = clients.city) as city
			FROM clients
			WHERE DATE_FORMAT(birth_date, '%m-%d') BETWEEN :d_ini AND :d_end
			
			ORDER BY DATE_FORMAT(birth_date, '%m-%d') ASC
			");
		$sql->bindValue(':d_ini', date('m-d', strtotime($dateInit)));
		$sql->bindValue(':d_end', date('m-d', strtotime($dateEnd)));		
		$sql->execute();

		if($sql->rowCount() > 0) {
			$data = $sql->fetchAll(\PDO::FETCH_ASSOC);
		}

		return $data;
	}
}