<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Extended HRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
/*  Pick List for Employment Type, Location work, Mode of Pay
*****************************************/
$page_security = 'SA_OPEN';
$path_to_root="../../..";
include($path_to_root . "/includes/session.inc");
$version_id = get_company_prefs('version_id');
$js = '';
if($version_id['version_id'] == '2.4.1'){
	if ($SysPrefs->use_popup_windows) 
		$js .= get_js_open_window(900, 500);	

	if (user_use_date_picker()) 
		$js .= get_js_date_picker();
	
}else{
	if ($use_popup_windows)
		$js .= get_js_open_window(900, 500);
	if ($use_date_picker)
		$js .= get_js_date_picker();
}
 
include($path_to_root . "/includes/ui.inc");
include($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc" );
include_once($path_to_root . "/includes/date_functions.inc");
include($path_to_root . "/modules/ExtendedHRM/includes/db/picklist.inc" );

if (isset($_GET['type'])){
	$_POST['type'] = $_GET['type'];
	$_POST['title'] = GetSingleValue('kv_empl_pick_type', 'description', array('id' => $_GET['type']));
} else {
	$_POST['title'] = _('Picklist');
}
page($_POST['title']);

simple_page_mode(true);
//----------------------------------------------------------------------------------------------------
$type = get_post('type','');

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM'){
	$input_error = 0;
	if (strlen($_POST['description']) == 0) {
		$input_error = 1;
		display_error(_("The Picklist Item description cannot be empty."));
		set_focus('description');
	}

	if ($input_error != 1)	{
    	if ($selected_id != -1)     	{
    		Update('kv_empl_picklist', array('id' => $selected_id), array('type' => $_POST['type'], 'description' => $_POST['description'], 'inactive' => check_value('inactive')));    		
			$note = _('Selected Picklist Item has been updated');
    	}   else  	{
    		Insert('kv_empl_picklist', array('type' => $_POST['type'], 'description' => $_POST['description'], 'inactive' => check_value('inactive')));
			$note = _('New Picklist Item has been added');
    	}    
		display_notification($note); 
		$Mode ='RESET';
	}
} 

if ($Mode == 'Delete'){
	$cancel_delete = 0;
	// PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'
	if (key_in_foreign_table($selected_id, 'kv_empl_job', 'department'))	{
		$cancel_delete = 1;
		display_error(_("Cannot delete this Picklist Item because Employees have been created using this Picklist Item."));
	} 
	if ($cancel_delete == 0) {
		Delete('kv_empl_picklist', array('id' => $selected_id));
		display_notification(_('Selected Picklist Item has been deleted'));
	} //end if DeletePicklist Item
	$Mode = 'RESET';
} 

if ($Mode == 'RESET'){
	$Ajax->activate('totals_tbl');
	set_focus('description');
}
//-------------------------------------------------------------------------------------------------
if(list_updated('type')) {
	$type = get_post('type');   
		$Ajax->activate('totals_tbl');
}else
	$type = 0;

//----------------------------------------------------------------------------------------------------
div_start('totals_tbl');
start_form();

	if(!isset($_GET['type'])){
		start_table(TABLESTYLE_NOBORDER);			
		echo "<tr><td class='label'>"._("Pick Type")." :"."</td><td nowrap>";
		echo hrm_pick_list_type('type', null, true, 1, true);
		echo "</td>\n</tr>\n";
		end_table();
		br(2);	
	} else
		hidden('type', $_POST['type']);

		start_table(TABLESTYLE, "width=30%");
		$_POST['id'] = $_POST['description'] = $_POST['inactive'] = ''; 
		$th = array (_('ID'), 'Description',  '','');		
		table_header($th);
		$k =  0;
		$type = $_POST['type'];
		if($type > 0 ){
			$result = get_all_picks($type);
			foreach($result as $myrow){			
				label_cell($myrow["id"]);	
				label_cell($myrow["description"]);				
				inactive_control_cell($myrow["id"], $myrow["inactive"], 'kv_empl_picklist', 'id');
			 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
			 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
				end_row();
			}	
		} else
			label_cell(_("No Type selected"), 'colspan = "5" style="text-align: center; "')	;
	end_table();
		br(2); 
	//----------------------------------------------------------------
	
		if ($selected_id != -1){

		 	if ($Mode == 'Edit') {
				$myrow = get_pick($selected_id);

				$_POST['description']  = $myrow['description'];
				$_POST['id']  = $myrow['id'];
				$_POST['inactive']  = $myrow['inactive'];
				$Ajax->activate('PickDetails');		
			}
			hidden('selected_id', $selected_id);
		}
	div_start("PickDetails");
	start_table(TABLESTYLE2);
		table_section_title(_("Description Entry"));
		text_row(_("Description").":", 'description', null, 40, 80);	
		kv_check_row(_("Is This Inactive").":", 'inactive', null, false, false);
		//check_row(_("Inactive"), 'inactive', null);
		//hidden('type', $type);
	end_table(1);
	if(get_post('type') >0)
		submit_add_or_update_center($selected_id == -1, '', 'both');
	else
		display_error(_("Please Select Pick Type."));
		
	div_end();

end_form();
//div_end();
end_page();
?> 