<?php

/**
 *		sabreScheduler Module for plugin to Drupal 7 Framework
 *
 *		Discrepancy creation (from customer home screen)
 */


require_once(drupal_get_path('module', 'sabreTools') . '/sabreTools.lib.inc');
require_once(drupal_get_path('module', 'sabreScheduler') . '/sabreScheduler.elogs.inc');


/*
 *	customer_discrepancy_form()
 *
 *  overloads:  hook_form()
 *
 *	Create Discrepancy (only)
 *  Displays a Discrepancy Entry form specifically for Customer entries
 *  Links from the $assignment_id
 */


function customer_discrepancy_form($form, $form_state, 
                                    $assignment_id = 0, $elog_id = 0) {
	
	
	$discrepancy = (object) Null;
  $assignment = (object) Null;
  
  global $user;
  global $base_url;
  
  
  //------------------- ROUTING ---------------------------
	// determine the return routing url
  
	$destination = drupal_get_destination();
	$goto_url = $destination['destination'];

	if( !strlen($goto_url) || ($goto_url == current_path()) ) {
    if ( user_access('view manage menu')) {
      $goto_url = 'manager/home';
    }
    else {
      $goto_url = 'user/home';
    }
	}
  
  

  
	
  //------------------ AFTER BUILD ------------------------
	// use the same after build function as the regular discrepancy_form
	$form['#after_build'][] = 'customer_discrepancy_form_after_build';
  
  
  $form['dr_table_begin'] = array(
    '#markup' => '<table class="qms-plain-table"><tr>',
  );
	
  $assignment = _get_assignment($assignment_id, $elog_id);
  
  $elog = _get_elog($elog_id, $assignment_id);
  
  $elog_crew = _get_elog_crew_info($elog->elog_id);
  $instructor = '';
  if (!empty($elog_crew)) {
    foreach ($elog_crew as $ec) {
      if ($ec->flight_role_1_id == SCH_CREW_INSTRUCTOR) {
        $instructor = $ec->lname . ', ' . $ec->fname;
        break;
      }
    }
  }
  
  
  
	$form['simulator'] = array(
		'#type' => 'item',
		'#title' => t('Simulator'),
		'#markup' => _st_format_sim_name($assignment->sim_name, 
                                     $assignment->device_id_internal),
    '#prefix' => '<td>',
    '#suffix' => '</td>',
	);
  
  $session_desc = $assignment->session_name . 
                  ' [' . $assignment->begin_time_actual . ' - ' . 
                  $assignment->end_time_actual . ']';
  
  $form['session'] = array(
		'#type' => 'item',
		'#title' => t('Session'),
		'#markup' => $session_desc,
    '#prefix' => '<td>',
    '#suffix' => '</td>',
	);
  
  $form['dr_table_row'] = array(
    '#markup' => '</tr><tr>',
  );
  
  global $user;
  $user_name = _get_user_name($user->uid);
  
  /*
  $form['instructor'] = array(
		'#type' => 'item',
		'#title' => t('Reported By'),
		'#markup' => $user_name,
    '#prefix' => '<td>',
    '#suffix' => '</td>',
	);
   * */
  
  $form['customer'] = array(
		'#type' => 'item',
		'#title' => t('Company Name'),
		'#markup' => $assignment->customer,
    '#prefix' => '<td>',
    '#suffix' => '</td>',
	);
  
  $form['dr_table_end'] = array(
    '#markup' => '<td>&nbsp;</td></tr></table>',
  );
	
	
	//------------- ADD DISCREPANCY TEXTAREA  -----------	
		
	$text_settings = array(
		'name' => 'discrepancy_text',
		'title' => t('Describe the Discrepancy'),
		'text' =>	'',
		'required' => True,
		'disabled' => False,
	);
	 
	_st_add_text_editor($form, $text_settings);
	
	
	//  -----------------------------------------	
  
  $form['assignment_id'] = array(
		'#type' => 'textfield',
		'#default_value' => $assignment_id,   
		'#attributes' => array('id' => 'sch-assignment-id',
													 'class' => array('qms-hidden-field')),
	);
  
  $form['elog_id'] = array(
		'#type' => 'textfield',
		'#default_value' => $elog_id,   
		'#attributes' => array('id' => 'sch-elog-id',
													 'class' => array('qms-hidden-field')),
	);
  
  // because we cannot link all tables between scheduler and QMS
  // append basic info to DR text field about the customer & session
  $append_text = '<p><b>' . t('Reported By') . ': &nbsp;</b>' . 
                    $user_name . '<br />' . 
                 '<b>' . t('Instructor') . ': &nbsp;</b>'. 
                    $instructor . '<br />' .
                 '<b>' . t('Company') . ': &nbsp;</b>'. 
                    $assignment->customer . '<br />' . 
                 '<b>' . t('Session') . ': &nbsp;</b>'. 
                    $session_desc . '</p>';
  
  $form['append_text'] = array(
		'#type' => 'textarea',
		'#default_value' => $append_text,   
		'#attributes' => array('id' => 'sch-append-text',
													 'class' => array('qms-hidden-field')),
    '#prefix' => '<div class="qms-hidden">',
    '#suffix' => '</div>',
	);
  
  $form['kiosk_mode'] = array(
    '#markup' => '<div id="qms-kiosk-url" class="qms-hidden">' . 
                  url('kiosk') . '</div>',
  );
	
	
	
	$form['actions'] = array('#type' => 'actions');
	$form['actions']['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Submit'),
    '#name' => 'submit',
		'#attributes' => array('class' => array('qms-btn-submit')),
	);
  
  $form['actions']['done'] = array(
		'#type' => 'button',
		'#value' => t('Done'),
    '#name' => 'done',
		'#attributes' => array('class' => array('qms-btn-done', 'qms-btn-extra'),
                           'onclick' => 'window.location="' . 
                                url($goto_url) . '"; return false;'),
    
    
	);
	
	
	return $form;
}

/*------ AFTER BUILD FUNCTION --------
*	 functions called, as it implies, after the form is built
*  this is necessary when custom javascript and/or css files are added as needed
*  at the page level for forms that go through validation with hook_form_validate()
*  Otherwise, if a form fails validation, the page/form is reloaded for the user to correct
*  but the accompanying javascript & css is not reloaded with it. 
*  when $form['#after_build][] is used, the external scripts will be reloaded properly.
*/

/* 
 * customer_discrepancy_form_after_build()
 *
 */
function customer_discrepancy_form_after_build($form, $form_state) {
  //drupal_add_library('system','ui.datepicker');
	//drupal_add_library('system','ui.dialog');
  $sabreTools = drupal_get_path('module', 'sabreTools');
	drupal_add_js($sabreTools . '/js/sabreTools.lib.js');
	drupal_add_js($sabreTools . '/js/sabreTools.ckeditor.js');
  drupal_add_js(drupal_get_path('module', 'sabreScheduler') .'/js/sabreScheduler.customer.discrepancy.js');

  global $base_url;
  drupal_add_js( $base_url . QMS_CKEDITOR_PATH_CONFIG);
  // need to specify preprocess == false otherwise 'Aggregate Javascript' setting in drupal 
  // causes ckeditor not to load properly
  // adding scope to footer speeds up the page load
  drupal_add_js( $base_url . QMS_CKEDITOR_LIB, 
                  array('type' => 'file', 
                        'scope' => 'footer', 
                        'preprocess' => FALSE));
  
//  KIOSK revised -- do not do this here
//  also, timeout reset does not work correctly with CKEditor
//  global $user;
//  
//  $user_customer_id =  _get_user_customer_id();
//  $bKioskMode = (( $user_customer_id == 1 ) && 
//                    user_access('kiosk mode') &&
//                    ($user->uid != 1) );
//  if ( $bKioskMode ) {
//    drupal_add_js(drupal_get_path('module', 'sabreScheduler') . 
//                        '/js/sabreScheduler.kioskreset.js');
//  }
		
	return $form;
}



/*
 *	customer_discrepancy_form_validate()
 *
 *  Overloads hook_form_validate()
 *  Validation for the discrepancy_preflight_form
 */

function customer_discrepancy_form_validate($form, $form_state) {
  
  	
	$dr_text = trim($form_state['values']['discrepancy_text_editor_value']);
	if ( $dr_text == '' ) {
		form_set_error('qms_discrepancy_text', 
                  t('Please enter information about the discrepancy.'));
	}
	
}


/*
 *	customer_discrepancy_form_submit()
 *
 *  Overloads hook_form_submit()
 *  Final submit action for the discrepancy_form
 *  Stores input data to the db
 */

function customer_discrepancy_form_submit($form, $form_state) {
 
  // call 
  _qms_create_discrepancy_report($form, $form_state);
    
}




