<?php
class Vehiculo{

	private $cn = null;

	function __construct(){
		$this->cn = conectarDB();
	}

	public function registrar($usuario,$contrasena,$patente,$marca,$modelo,$carroceria){
		$query = 'INSERT INTO vehiculo(veh_patente, veh_marca, veh_modelo, carroceria_car_id) VALUES(?, ?, ?, ?);';
		$rs = $this->cn->Execute($query,array($patente,$marca,$modelo,$carroceria));
		$id = $this->cn->insert_ID();
		if($this->cn->Affected_Rows()==1){
			$usu = new UMovil();
			return $usu->vincularVehiculo($usuario,$contrasena,$id);
		}
		return false;
	}

	public function obtener($patente){
		$query = 'SELECT veh_id AS id, veh_modelo AS modelo, veh_marca AS marca, car_nombre AS carroceria
					FROM vehiculo JOIN carroceria ON carroceria_car_id = car_id
					WHERE veh_patente = ? LIMIT 1;';
		$rs = $this->cn->Execute($query,array($patente));
		if(!empty($rs) && !$rs->EOF){
			$vehiculo = $rs->GetArray();
			$rs->Close();
			return $vehiculo;
		}
		return false;
	}

	public function carrocerias(){
		$query = 'SELECT car_id AS id, car_nombre AS nombre
					FROM carroceria
					ORDER BY id ASC;';
		$rs = $this->cn->Execute($query);
		if(!empty($rs) && !$rs->EOF){
			$carrocerias = $rs->GetArray();
			$rs->Close();
			return $carrocerias;
		}
		return false;
	}

	public function eliminar($ids){
		$query = 'DELETE FROM usu_posee_veh WHERE';
		$query2 = 'DELETE FROM vehiculo WHERE';
		foreach($ids as $posicion => $id){
			$query .= (($posicion==0)?'':' OR').' vehiculo_veh_id = '.$id;
			$query2 .= (($posicion==0)?'':' OR').' veh_id = '.$id;
		}
		$query .= ';';
		$query2 .= ';';
		$rs = $this->cn->Execute($query);
		if($this->cn->Affected_Rows()>=1){
			$rs2 = $this->cn->Execute($query2);
			if($this->cn->Affected_Rows()>=1){
				return true;
			}
		}
		return false;
	}

	function __destruct(){
		$this->cn->Close();
	}
}
?>