<?php

include_once 'training.inc';

function bee_account_myaccount($op, $user_uid = 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':
			if (!bee_account_myaccount_get_basic_user_info($user_uid) && in_array(BEE_ACCOUNT_ACCESS_BASIC, $user->roles)) {
				drupal_set_message('This profile is empty, you were redirected to create a profile', 'error');
				drupal_goto('manuka/myaccount/edit/');
			}
			$user_info = bee_account_myaccount_get_basic_user_info($user_uid);
			$output = array(
				'#theme' => 'bee_account_my_account_template',
				'#type' => 'markup',
				'#bee_account' => $user_info,
				'#trainings' => bee_account_training_user_show_table($user_uid),
				'#payroll' => bee_account_format_employeer_payroll($user_uid),
				'#timeSpent' => time_spent_show_user_times($user_uid),
			);
			break;
		case 'edit':
			if (user_access(BEE_ACCOUNT_ACCESS_PAYROLL)) {
				drupal_set_message('Sorry, you cannot edit the employeers profile', 'error');
				drupal_goto($_SERVER['HTTP_REFERER']);
			}
			$output = drupal_render(drupal_get_form('bee_account_myaccount_form', $user_uid));
			break;
		default:
			if (!bee_account_myaccount_get_basic_user_info($user_uid) && in_array(BEE_ACCOUNT_ACCESS_BASIC, $user->roles)) {
				drupal_set_message('This profile is empty, you were redirected to create a profile', 'error');
				drupal_goto('manuka/myaccount/edit/');
			}
			$user_info = bee_account_myaccount_get_basic_user_info($user_uid);
			$output = array(
				'#theme' => 'bee_account_my_account_template',
				'#type' => 'markup',
				'#bee_account' => $user_info,
				'#trainings' => bee_account_training_user_show_table($user_uid),
				'#payroll' => bee_account_get_employeer_payroll($user_uid),
				'#timeSpent' => time_spent_show_user_times($user_uid),
			);
			break;
	}
	return $output;
}

function bee_account_myaccount_form($form, &$form_state, $user_uid) {
	if (empty($user_uid)) {
		drupal_set_message('We are sorry, something seems to be wrong, please try again', 'error');
		return FALSE;
	}
	$form['basic_info'] = array(
		'#type' => 'fieldset',
		'#title' => t('Basic information'),
		'#weight' => 0,
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
	);
	if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
		$form['basic_info']['bid'] = array(
			'#type' => 'textfield',
			'#title' => t('Bee healthy id'),
			'#description' => t('Bee healthy identification ID'),
			'#default_value' => '',
		);
	}
	$form['basic_info']['name'] = array(
		'#type' => 'textfield',
		'#title' => t('Name'),
		'#description' => t('Full name'),
		'#default_value' => '',
	);
	$form['basic_info']['photo'] = array(
		'#type' => 'file',
		'#title' => t('Photo'),
		'#size' => 22,
	);
	$form['basic_info']['sex'] = array(
		'#type' => 'radios',
		'#title' => t('Sex'),
		'#description' => t(''),
		'#default_value' => 'Male',
		'#options' => array('Male' => 'Male', 'Female' => 'Female')
	);
	$form['basic_info']['birthday'] = array(
		'#type' => 'date_popup',
		'#date_format' => 'm-d-Y',
		'#date_year_range' => '-70:0',
		'#title' => t('Birthday'),
		'#date_label_position' => 'none',
	);
	$form['basic_info']['address'] = array(
		'#type' => 'textfield',
		'#title' => t('Address'),
		'#description' => t(''),
	);
	$form['basic_info']['address_no'] = array(
		'#type' => 'textfield',
		'#title' => t('Home no'),
		'#description' => t('eg: 150, 1550'),
	);
	$form['basic_info']['mobile'] = array(
		'#type' => 'textfield',
		'#title' => t('Mobile no'),
		'#description' => t('eg: 636-5588'),
	);
	$form['basic_info']['alternative_mail'] = array(
		'#type' => 'textfield',
		'#title' => t('Alternative E-mail'),
		'#description' => t('eg: myemail@gmail.com'),
	);
	if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
		$form['bee_healthy'] = array(
			'#type' => 'fieldset',
			'#title' => t('Admin Bee Healthy'),
			'#weight' => 1,
			'#collapsible' => TRUE,
			'#collapsed' => FALSE,
		);
		$form['bee_healthy']['position'] = array(
			'#type' => 'select',
			'#title' => t('Position'),
			'#options' => array(
				0 => t(bee_account_position_map(0)),
				1 => t(bee_account_position_map(1)),
				2 => t(bee_account_position_map(2)),
				3 => t(bee_account_position_map(3)),
				4 => t(bee_account_position_map(4)),
				5 => t(bee_account_position_map(5)),
			),
			'#default_value' => BEE_ACCOUNT_POSITION_STAFF,
			'#description' => t(''),
		);
		$form['bee_healthy']['year_employed'] = array(
			'#type' => 'date_popup',
			'#date_format' => 'm-d-Y',
			'#date_year_range' => '-20:0',
			'#title' => t('Year Employed'),
			'#date_label_position' => 'none',
		);
		$form['bee_healthy']['designation'] = array(
			'#type' => 'textfield',
			'#title' => t('Designation'),
			'#description' => t(''),
		);
	}
	$form['additional_information'] = array(
		'#type' => 'fieldset',
		'#title' => t('Additonal information'),
		'#weight' => 2,
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
	);
	$form['additional_information']['high_school'] = array(
		'#type' => 'textfield',
		'#title' => t('High school'),
		'#description' => t(''),
	);
	$form['additional_information']['college'] = array(
		'#type' => 'textfield',
		'#title' => t('College'),
		'#description' => t(''),
	);
	$form['additional_information']['emergency'] = array(
		'#markup' => '<h4>Emergency contact</h4>'
	);
	$form['additional_information']['emergency_name'] = array(
		'#type' => 'textfield',
		'#title' => t('Name'),
		'#description' => t(''),
	);
	$form['additional_information']['emergency_address'] = array(
		'#type' => 'textfield',
		'#title' => t('Address'),
		'#description' => t(''),
	);
	$form['additional_information']['emergency_contact'] = array(
		'#type' => 'textfield',
		'#title' => t('Contact'),
		'#description' => t('eg: Phone number'),
	);
	$form['additional_information']['emergency_relation'] = array(
		'#type' => 'textfield',
		'#title' => t('Relation'),
		'#description' => t('eg: Parent, husband'),
	);
	$form['additional_information']['notes'] = array(
		'#type' => 'textarea',
		'#title' => t('Notes'),
		'#rows' => 5,
	);
	$form['actions'] = array(
		'#type' => 'fieldset',
		'#weight' => 5,
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
	);
	$form['actions']['button'] = array(
		'#type' => 'submit',
		'#value' => t('Save'),
	);
	$form['actions']['cancel'] = array(
		'#type' => 'submit',
		'#value' => t('Cancel'),
		'#submit' => array('')
	);
	$form['user_uid'] = array(
		'#type' => 'hidden',
		'#value' => $user_uid,
	);
	$form['edit'] = array(
		'#type' => 'hidden',
		'#value' => FALSE,
	);
	$user_info = bee_account_myaccount_get_basic_user_info($user_uid);
	if (!empty($user_info)) {
		$form['basic_info']['name']['#default_value'] = $user_info->name;
		$form['basic_info']['sex']['#default_value'] = $user_info->sex;
		$form['basic_info']['birthday']['#default_value'] = (!empty($user_info->birthday)) ? date('Y-m-d', $user_info->birthday) : NULL;
		$form['basic_info']['address']['#default_value'] = $user_info->address;
		$form['basic_info']['address_no']['#default_value'] = $user_info->address_no;
		$form['basic_info']['mobile']['#default_value'] = $user_info->mobile;
		$form['basic_info']['alternative_mail']['#default_value'] = $user_info->email_alternative;
		$form['additional_information']['high_school']['#default_value'] = $user_info->school;
		$form['additional_information']['college']['#default_value'] = $user_info->college;
		$form['additional_information']['emergency_name']['#default_value'] = $user_info->emergency_name;
		$form['additional_information']['emergency_address']['#default_value'] = $user_info->emergency_address;
		$form['additional_information']['emergency_contact']['#default_value'] = $user_info->emergency_contact;
		$form['additional_information']['emergency_relation']['#default_value'] = $user_info->emergency_relation;
		$form['additional_information']['notes']['#default_value'] = $user_info->notes;
		$form['edit'] = array(
			'#type' => 'hidden',
			'#value' => TRUE,
		);
		if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
			$form['basic_info']['bid']['#default_value'] = $user_info->bid;
			$form['bee_healthy']['year_employed']['#default_value'] = (!empty($user_info->year_employed)) ? date('Y-m-d', $user_info->year_employed) : NULL;
			$form['bee_healthy']['position']['#default_value'] = $user_info->position;
			$form['bee_healthy']['designation']['#default_value'] = $user_info->designation;
		}
	}

	return $form;
}

function bee_account_myaccount_form_validate($form, &$form_state) {
	$user_uid = $form_state['values']['user_uid'];
	if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
		$bid = $form_state['values']['bid'];
		$owner_bid = bee_account_is_bid_exists($bid);
		if ((!empty($owner_bid) && $user_uid != $owner_bid->uid) || empty($bid)) {
			form_set_error('bid', t('Bid already exists or it is null'));
		}
	}
	$image_file = file_save_upload('photo', array('file_validate_is_image' => array()));
	if (!empty($image_file)) {
		$form_state['values']['photo'] = $image_file;
	}
}

function bee_account_myaccount_form_submit($form, &$form_state) {
	$photo_path = NULL;
	$user_uid = $form_state['values']['user_uid'];
	$edit = $form_state['values']['edit'];
	$birthday = (!empty($form_state['values']['birthday'])) ? strtotime($form_state['values']['birthday']) : NULL;
	if (!empty($form_state['values']['photo'])) {
		$photo_file = $form_state['values']['photo'];
		$photo_file->status = 1;
		$foto_loaded = image_load($photo_file->uri);
		$filename = pathinfo($photo_file->filename);
		image_gd_resize($foto_loaded, 150, 200);
		image_save($foto_loaded);
		$result = file_copy($photo_file, BEE_ACCOUNT_PHOTO_BASE_PATH . '/' . $user_uid . '.' . $filename['extension'], FILE_EXISTS_REPLACE);
		$photo_path = $result->uri;
	}
	if (!empty($edit)) {
		$mail_edit = db_update('bee_account_user_profile');
		$fields = array(
			'name' => $form_state['values']['name'],
			'birthday' => $birthday,
			'sex' => $form_state['values']['sex'],
			'address' => $form_state['values']['address'],
			'address_no' => $form_state['values']['address_no'],
			'mobile' => $form_state['values']['mobile'],
			'email_alternative' => $form_state['values']['alternative_mail'],
			'school' => $form_state['values']['high_school'],
			'college' => $form_state['values']['college'],
			'emergency_name' => $form_state['values']['emergency_name'],
			'emergency_address' => $form_state['values']['emergency_address'],
			'emergency_contact' => $form_state['values']['emergency_contact'],
			'emergency_relation' => $form_state['values']['emergency_relation'],
			'notes' => $form_state['values']['notes'],
		);
		if (!empty($photo_path)) {
			$fields['photo'] = $photo_path;
		}
		if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
			$year_employed = (!empty($form_state['values']['year_employed'])) ? strtotime($form_state['values']['year_employed']) : NULL;
			$fields_admin = array(
				'bid' => $form_state['values']['bid'],
				'name' => $form_state['values']['name'],
				'year_employed' => $year_employed,
				'position' => $form_state['values']['position'],
				'designation' => $form_state['values']['designation'],
			);
			foreach ($fields_admin as $key => $value) {
				$fields[$key] = $value;
			}
		}
		$mail_edit->fields($fields);
		$mail_edit->condition('uid', $user_uid, '=');
		$mail_edit->execute();
	} else {
		$mail_save = db_insert('bee_account_user_profile');
		$fields = array(
			'name' => $form_state['values']['name'],
			'photo' => $photo_path,
			'birthday' => $birthday,
			'sex' => $form_state['values']['sex'],
			'address' => $form_state['values']['address'],
			'address_no' => $form_state['values']['address_no'],
			'mobile' => $form_state['values']['mobile'],
			'email_alternative' => $form_state['values']['alternative_mail'],
			'school' => $form_state['values']['high_school'],
			'college' => $form_state['values']['college'],
			'emergency_name' => $form_state['values']['emergency_name'],
			'emergency_address' => $form_state['values']['emergency_address'],
			'emergency_contact' => $form_state['values']['emergency_contact'],
			'emergency_relation' => $form_state['values']['emergency_relation'],
			'notes' => $form_state['values']['notes'],
			'uid' => $user_uid,
		);
		if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
			$year_employed = (!empty($form_state['values']['year_employed'])) ? strtotime($form_state['values']['year_employed']) : NULL;
			$fields_admin = array(
				'bid' => $form_state['values']['bid'],
				'year_employed' => $year_employed,
				'position' => $form_state['values']['position'],
				'designation' => $form_state['values']['designation'],
			);
			foreach ($fields_admin as $key => $value) {
				$fields[$key] = $value;
			}
		}
		$mail_save->fields($fields);
		$mail_save->execute();
	}
	drupal_set_message('Successfully');
	if (user_access(BEE_ACCOUNT_ACCESS_ADMIN)) {
		drupal_goto('manuka/admin/showusers');
	} else {
		drupal_goto('manuka/myaccount/view');
	}
}

function bee_account_myaccount_get_basic_user_info($user_uid) {
	$query = db_select('bee_account_user_profile', 'p')
			->condition('p.uid', $user_uid, '=')
			->fields('p')
			->execute()
			->fetch();

	return $query;
}

function bee_account_is_bid_exists($bid) {
	$query = db_select('bee_account_user_profile', 'p')
			->condition('p.bid', $bid, '=')
			->fields('p', array('uid'))
			->execute()
			->fetch();
	return $query;
}
