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

class Farm extends Model {

	// Busca para relatórios
	public static function getFilterFarms($offset, $limit, $filter) {
		$data = Farm::select()
			->where('status', $filter['fs_status'], $filter['f_status'])
			->where($filter['f_state'], $filter['fs_state'], $filter['fv_state'])            
            ->where($filter['f_client'], $filter['fs_client'], $filter['fv_client'])
			->orderBy(['status' => '!= 1', 'name' => 'asc'])
			->offset($offset)
			->limit($limit)			
		->get();

		return $data;
	}

	public static function getFarms($client) {
		$data = Farm::select()
			->where('id_client', $client)
		->get();

		return $data;
	}

	public static function getFarm($farm) {
		$data = Farm::select()
			->where('id', $farm)
		->one();

		return $data;
	}

	public static function getFarmName($farm) {
		$data = Farm::select('name')
			->where('id', $farm)
		->one();

		if($data) {
			return $data['name'];
		}
	}

	public static function existsFarm($stateReg) {
		$data = Farm::select('id')
			->where('state_registration', $stateReg)
		->count();

		if($data > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static function saveFarm($farm) {
		Farm::insert($farm)
		->execute();
	}

	public static function updateFarm($farm, $idFarm) {
		Farm::update($farm)
			->where('id', $idFarm)
		->execute();
	}

	public static function deleteAttachment($farm) {
		Farm::update(['attachment' => null])
			->where('id', $farm)
		->execute();
	}
}