angular.module('app').controller('ExamesCtrl', ExamesCtrl)

ExamesCtrl.$inject = ['examesService']

function ExamesCtrl(examesService) {
	var vm = this
	vm.init = init
	vm.buscar = buscar
	vm.open = open
	vm.getListExames = getListExames
	vm.setFiltro = setFiltro
	vm.title = ''
	vm.buscarTexto = ''
	vm.list = []
	vm.filtro = {
		categoria: true,
		exame: false
	}

	function init() {
		getListExames()
	}

	function buscar() {
		//$('#wrap-list').css('display', 'none')
		//console.log(vm.filtro);
	}

	function setFiltro(value) {
		if (value === 'categoria') {
			vm.filtro.categoria
				? (vm.filtro.categoria = false)
				: (vm.filtro.categoria = true)
		} else {
			vm.filtro.exame
				? (vm.filtro.exame = false)
				: (vm.filtro.exame = true)
		}
	}

	function getListExames() {
		vm.title = 'Nova Categoria'
		examesService.getListExames().then(
			function(response) {
				vm.list = response.data
				$('#wrap-list').css('display', 'block')
			},
			function(error) {
				console.log('error')
			}
		)
	}

	function open(element) {
		if (
			$(element)
				.find('.list__item')
				.hasClass('--open')
		) {
			$(element)
				.find('.list__item')
				.removeClass('--open')
			$(element)
				.find('.list__description')
				.removeClass('--open')
		} else {
			close()
			$(element)
				.find('.list__item')
				.addClass('--open')
			$(element)
				.find('.list__description')
				.addClass('--open')
		}
	}

	function close() {
		$('.list').map(function() {
			$(this)
				.find('.list__item')
				.removeClass('--open')
			$(this)
				.find('.list__description')
				.removeClass('--open')
		})
	}
}
