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

class User extends Model {

	public static function saveUser($user, $note) {
		$user = User::insert($user)
		->execute();

		User::insert(['id_user' => $user, 'note' => $note])
			->table('users_notes')
		->execute();
	}

	public static function updateUser($userId, $user, $note) {
		$user = User::update($user)
			->where('id', $userId)
		->execute();

		User::update(['note' => $note])
			->table('users_notes')
			->where('id_user', $userId)
		->execute();
	}	

	public static function getUser($user) {
		$data = User::select()
			->where('id', $user)	
		->one();

		$note = User::select('note')
			->table('users_notes')
			->where('id_user', $user)
		->one();

		$data['note'] = ($note)?$note['note']:'';
		return $data;
	}

	public static function getName($user) {
		$data = User::select('name')
			->where('id', $user)	
		->one();
		
		return $data['name'];
	}

	public static function getUsers($offset, $limit, $filter) {
		$data = User::select()
			->where($filter['f_others'], $filter['fs_others'], $filter['fo_value'])
			->where('status', $filter['fs_status'], $filter['f_status'])
			->orderBy(['status' => '!= 1', 'name' => 'asc'])
			->offset($offset)
			->limit($limit)			
		->get();

		return $data;
	}

	public static function getCount($filter) {
		$c = User::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 existsUser($ref, $value) {
        $response = false;

        $c = User::select()
        	->where($ref, $value)
        ->count();

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

        return $response;
    }

    public static function deleteUser($user) {
    	User::delete()
    		->where('id', $user)
    	->execute();

    	User::delete()
    		->table('users_notes')
    		->where('id_user', $user)
    	->execute();
    }

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

		$sql = $db->prepare("
			SELECT name, status, phone as first_phone, birth_date, email FROM users
			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;
	}

	public static function getNotification($user) {
		$data = User::select()
			->table('notifications')
			->where('id_user', $user)
			->andWhere('status', 1)
		->one();

		return $data;
	}

	public static function updateNotification($ref, $value) {
		User::update(['status' => $value])
			->table('notifications')
			->where('id', $ref)
		->execute();
	}

	public static function saveNotification($notification) {
		User::insert($notification)
			->table('notifications')
		->execute();
	}

}