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

class Provider extends Model {

	public static function saveProvider($provider, $table) {
		Provider::insert($provider)
			->table($table)
		->execute();
	}

	public static function updateProvider($providerId, $provider, $table) {
		Provider::update($provider)
			->table($table)
			->where('id', $providerId)
		->execute();
	}

	public static function getProviders($offset, $limit, $filter, $table) {
		$data = Provider::select()
			->table($table)			
			->where('status', $filter['fs_status'], $filter['f_status'])
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where($filter['f_pstate'], $filter['fs_pstate'], $filter['fp_state'])
			->where($filter['f_provider'], $filter['fs_provider'], $filter['fv_provider'])
			->orderBy(['status' => '!= 1', 'company_name' => 'asc'])
			->offset($offset)
			->limit($limit)
		->get();

		return $data;
	}

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

		return $data;
	}

	public static function getProvider($provider, $table) {
		$data = Provider::select()
			->table($table)
			->where('id', $provider)
		->one();

		return $data;
	}

	public static function getAll() {
		$data = Provider::select()
			->orderBy(['status' => '!= 1', 'company_name' => 'asc'])
		->get();

		return $data;
	}

	public static function getProvidersServices() {
		$data = Provider::select()
			->table('providers_services')
			->orderBy(['status' => '!= 1', 'company_name' => 'asc'])
		->get();

		return $data;
	}

	public static function getProviderName($provider) {
		$data = Provider::select('company_name')
			->where('id', $provider)
		->one();

		return $data['company_name'];
	}

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

		return $c;
	}

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

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

	public static function existsProviderRg($ref, $value) {
		$response = false;
		$c = Provider::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 company_name as name, birth_date, status, first_phone, email, state, 
			(select citys.name from citys where citys.id = providers.city) as city
			FROM providers
			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;
	}
}