<?php

function bee_account_training($op = 'view', $user_uid = NULL, $utid = NULL) {
	global $user;
	if (empty($user_uid)) {
		$user_uid = $user->uid;
	}
	if (empty($user_uid) || 0 == $user_uid || !user_load($user_uid)) {
		return drupal_access_denied();
	}
	switch ($op) {
		case 'view':
			$output = bee_account_training_view($user_uid);
			break;
		case 'show':
			$output = bee_account_training_user_show_table($user_uid);
			break;
		case 'edit':
			$output = drupal_render(drupal_get_form('bee_account_training_form', $user_uid, $utid));
			break;
		case 'remove':
			if (empty($utid)) {
				drupal_set_message('Sorry, somethig are wrong. Please try again.', 'error');
				return FALSE;
			}
			$output = drupal_render(drupal_get_form('bee_account_training_remove_form', $utid));
			break;
		default:
			$output = bee_account_training_view($user_uid);
			break;
	}
	return $output;
}

function bee_account_training_form($form, &$form_state, $user_uid, $utid) {
	if (!empty($$user_uid)) {
		return FALSE;
	}
	$form['name'] = array(
		'#type' => 'textfield',
		'#title' => t('Trainings attended'),
		'#description' => t(''),
		'#default_value' => '',
	);
	$form['date'] = array(
		'#type' => 'date_popup',
		'#date_format' => 'm-d-Y',
		'#date_year_range' => '-10:0',
		'#title' => t('Year Employed'),
		'#date_label_position' => 'none',
	);
	$form['grade'] = array(
		'#type' => 'textfield',
		'#title' => t('Grade'),
		'#description' => t('eg: 80'),
		'#default_value' => '',
	);
	$form['button'] = array(
		'#type' => 'submit',
		'#value' => t('Save'),
	);
	$form['cancel'] = array(
		'#type' => 'submit',
		'#value' => t('Cancel'),
		'#submit' => array('')
	);
	$form['user_uid'] = array(
		'#type' => 'hidden',
		'#value' => $user_uid,
	);
	if (!empty($utid)){

	}

	return $form;
}

function bee_account_training_form_submit($form, &$form_state) {
	$date = (!empty($form_state['values']['date'])) ? strtotime($form_state['values']['date']) : NULL;
	$user_uid = $form_state['values']['user_uid'];
	$trainingSave = db_insert('bee_account_trading_activity')
			->fields(array(
		'name' => $form_state['values']['name'],
		'date' => $date,
		'grade' => $form_state['values']['grade'],
		'user_uid' => $user_uid,
	));
	$trainingSave->execute();
	drupal_set_message('Successfully');
	overlay_close_dialog();
	$form_state['redirect'] = FALSE;
}

function bee_account_training_view() {
	$query = db_select('users', 'u');
	$query->fields('u');
	$result = $query->execute();
	$output = '<dl>';
	while ($record = $result->fetchAssoc()) {
		if (0 != $record['uid']) {
			$output .= '<dl><dt><h3>' . $record['name'] . '</h3></dt>';
			$output .= '<dd><a href="' . base_path() . 'manuka/training/edit/' . $record['uid'] . '">Edit</a></dd>';
			$output .= '<dd><a href="' . base_path() . 'manuka/training/view/' . $record['uid'] . '">View</a></dd>';
			$output .= '<dd><a href="' . base_path() . 'manuka/training/show/' . $record['uid'] . '">Show</a></dd>';
		}
	}
	$output .= '</dl>';
	return $output;
}

function bee_account_get_user_trainings($user_uid) {
	$query = db_select('bee_account_trading_activity', 'u')
			->fields('u')
			->condition('user_uid', $user_uid, '=')
			->execute()
			->fetchAll();
	return $query;
}

function bee_account_training_user_show_table($user_uid) {
	$trainings = bee_account_get_user_trainings($user_uid);
	$output = '';
	if ($trainings) {
		$output .= '<table class="table table-hover">';
		$output .= '<tr>';
		$output .= '<th>Trainings attended</th>';
		$output .= '<th>Date</th>';
		$output .= '<th>Grade</th>';
		$output .= '<th>Action</th>';
		$output .= '</tr>';
		foreach ($trainings as $value) {
			$date = (!empty($value->date)) ? date('Y-m-d', $value->date) : NULL;
			$output .= '<tr>';
			$output .= '<td>' . $value->name . '</td>';
			$output .= '<td>' . $date . '</td>';
			$output .= '<td>' . $value->grade . '</td>';
			if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
				$output .= '<td><ul class="inline"><li><a href="' . base_path() . 'manuka/training/edit/' . $user_uid . '/' . $value->utid . '" class="btn" type="button">Edit</a></li>';
				$output .= '<li><a href="' . base_path() . 'manuka/training/remove/' . $user_uid . '/' . $value->utid . '" class="btn btn-danger" type="button">Remove</a></li></ul></td>';
			}
			$output .= '</tr>';
		}

		$output .= '</table>';
	}
	if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
		$output .= '<ul class="inline">';
		$output .= '<li><a href="' . base_path() . 'manuka/training/edit/' . $user_uid . '" class="btn" type="button">Add</a></li>';
		$output .= '</ul>';
	}
	return $output;
}

function bee_account_training_remove_form($form, &$form_state, $utid) {
	$form['utid'] = array(
		'#type' => 'hidden',
		'#value' => $utid,
	);
	$form['question'] = array(
		'#markup' => '<h3>' . t('Are you sure you want to delete this email?') . '</h3>'
	);
	return confirm_form($form, t('Are you sure you want to delete this email?'), '', '<p>' . t('You will just remove show email account, your email will not removed.') . '</p>', t('Delete'), t('Cancel'));
}

function bee_account_training_remove_form_submit($form, &$form_state) {
	$utid = $form_state['values']['utid'];
	db_delete('bee_account_trading_activity')
			->condition('utid', $utid, '=')
			->execute();
	overlay_close_dialog();
	$form_state['redirect'] = FALSE;
}