<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module  : Kvcodes CRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
*****************************************/

$page_security = 'CRM_SETTINGS';
$path_to_root="../..";
include_once($path_to_root . "/includes/session.inc");
add_access_extensions();
$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_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/modules/KvcodesCRM/includes/crm.inc" );
include_once($path_to_root . "/includes/date_functions.inc");
page(_("Picklists")); 
 
simple_page_mode(true);
//----------------------------------------------------------------------------------------------------
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(isset($_POST['type_']) && trim($_POST['type_']) != '')
			$_POST['type'] = Insert('kv_crm_picklist', array('picklist' => $_POST['type_']));
    	if ($selected_id != -1)     	{
    		Update('kv_crm_picklist_values', array('id' => $selected_id), array('picklist_id' => $_POST['type'], 'description' => $_POST['description']));    		
			$note = _('Selected Picklist Item has been updated');
    	}   else  	{
    		$id2 = GetNextPickNumber($_POST['type']);
    		Insert('kv_crm_picklist_values', array('picklist_id' => $_POST['type'], 'id2' => $id2, 'description' => $_POST['description']));
			$note = _('New Picklist Item has been added');
    	}    
		meta_forward($_SERVER['PHP_SELF'], "type=".$_POST['type']);
		display_notification($note); 
		$Ajax->activate('total_tbl');
	}
} 
if(isset($_POST['RemovePick'])){
	Delete('kv_crm_picklist_values', array('picklist_id' => $_POST['RemovePick']));
	Delete('kv_crm_picklist', array('id' => $_POST['RemovePick']));
	display_notification(_('Selected Picklist has been deleted'));
	meta_forward($_SERVER['PHP_SELF'], " ");
	$Ajax->activate('total_tbl');
}
if ($Mode == 'Delete'){
	
	$cancel_delete = 0;	// PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'
	if (key_in_foreign_table($selected_id, 'kv_crm_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_crm_picklist_values', array('id' => $selected_id));
		display_notification(_('Selected Picklist Item has been deleted'));
	} //end if DeletePicklist Item
	$Mode = 'RESET';
} 

/*if ($Mode == 'RESET'){
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	if ($sav) $_POST['show_inactive'] = 1;
}*/
//-------------------------------------------------------------------------------------------------
if(isset($_GET['type']))
	$_POST['type'] = $_GET['type'];
if(list_updated('type')) {
	$type = get_post('type');   
	$Ajax->activate('totals_tbl');
}else
	$type = 0;

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

	start_table(TABLESTYLE_NOBORDER);			
		start_row();
		crm_picklist_type_cells(_("Pick Type"). ' :', 'type', null, _("Select Pick"), true, false);
		end_row();
	end_table();
	br();
	if(get_post('type')){
		start_table(TABLESTYLE, "width=30%");
			$_POST['id'] = $_POST['description'] = $_POST['inactive'] = ''; 
			$th = array (_('ID'), 'Description',  '','');		
			table_header($th);
			$k =  0;
			
			$result = GetAll('kv_crm_picklist_values', array('picklist_id' => get_post('type')));
			foreach($result as $myrow){			
				label_cell($myrow["id2"]);	
				label_cell($myrow["description"]);				
				inactive_control_cell($myrow["id"], $myrow["inactive"], 'kv_picklist', 'id');
				edit_button_cell("Edit".$myrow['id'], _("Edit"));
				delete_button_cell("Delete".$myrow['id'], _("Delete"));
				end_row();
			}		
		end_table();
	}
		br(2); 
	//----------------------------------------------------------------
	div_start('PickDetails');
	start_table(TABLESTYLE2);
		table_section_title(_("Description Entry"));
		if ($selected_id != -1){
		 	if ($Mode == 'Edit') {
				$myrow = GetRow('kv_crm_picklist_values', array('id' => $selected_id));

				$_POST['description']  = $myrow['description'];
				$_POST['id']  = $myrow['id2'];
				$_POST['inactive']  = $myrow['inactive'];
				$Ajax->activate('PickDetails')				;
			}
			hidden('selected_id', $selected_id);
			label_row(_("Pick ID"), $_POST['id']);
		}
		text_row(_("Description :"), 'description', null, 40, 80);	
		check_row(_("Inactive:"), 'inactive', null, false, false);
	end_table(1);
	div_end();
	submit_add_or_update_center($selected_id == -1, '', 'both');
end_form();
 
end_page();
 
?>