<?php

function bee_account_user($op = 'all') {
	switch ($op) {
		case 'all':
			$output = bee_account_users_view();
			break;

		case 'position':
			$output = bee_account_find_position();

			break;
		case 'woutprofiles':
			$output = bee_account_employeer_with_profiles();

			break;

		default:
			$output = bee_account_find_position();

			break;
	}

	return $output;
}

/*
 * Show all users
 */

function bee_account_users_view($condition = array()) {
	$module_path = drupal_get_path('module', 'bee_account');
	drupal_add_css($module_path . '/external/hover-effect/css/fdw.css');
	drupal_add_js($module_path . '/external/hover-effect/js/fdw.js');

	$users = bee_account_user_get_users();
	if (empty($users)) {
		$output = bee_account_error_messages('no employers');
	} else {
		$output = '<ul class = "unstyled">';
		foreach ($users as $userProfile) {
			if (isset($condition['position']) && ($userProfile->position != $condition['position'])) {
				continue;
			}
			$photo_path = ($userProfile->photo) ? file_create_url($userProfile->photo) : file_create_url(BEE_ACCOUNT_PHOTO_DEFAULT);
			$position = (isset($userProfile->position)) ? bee_account_position_map($userProfile->position) : 'empty';
			$output .= '<li class = "span3">';
			$output .= '<article class = "c-two" style = "background-image:url( ' . $photo_path . ')">';
			$output .= '<div class = "fdw-background">';
			$output .= '<h4><a href = "' . base_path() . 'manuka/myaccount/edit/' . $userProfile->uid . '" style = "color:#fff;">' . $userProfile->name . '</a></h4>';
			$output .= '<div class = "fdw-subtitle a-center">is ' . $position . '</div>';
			$output .= '<p class = "fdw-port">';
			$output .= '<a href = "' . base_path() . 'manuka/myaccount/view/' . $userProfile->uid . '">View<span class = "vg-icon">→</span></a>';
			$output .= '</p>';
			$output .= '</div>';
			$output .= '</article>';
			$output .= '</li>';
		}
		$output .= '</ul>';
	}
	return $output;
}

function bee_account_user_get_users() {
	$query = db_select('bee_account_user_profile', 'u');
	$query->fields('u');
	$result = $query->execute()->fetchAll();
	return $result;
}

function bee_account_employeer_with_profiles() {
	$employers = bee_account_get_employers_without_profile();
	if (!empty($employers)) {
		$output = '<dl>';
		foreach ($employers as $value) {
			$userInfo = user_load($value->uid);
			$output .= '<dt><a href="' . BEE_ACCOUNT_PATH_EDIT_PROFILE . $userInfo->uid . '" data-toggle="tooltip" data-placement="right"  title="Add profile">'
					. $userInfo->name . '</a></dt>';
			$output .= "<dd>$userInfo->mail</dd>";
			$output .= '<dd>' . date("F j, Y, g:i a", $userInfo->created) . '</dd>';
		}
		$output .= '</dl>';
	} else {
		return bee_account_error_messages('Great!!! Do not have employers without profile');
	}
	return $output;
}