<?php
/*=======================================================\
|                 Point of Sales Module                  |
|--------------------------------------------------------|
|   Creator: Phương                                      |
|   Date :   18-Dec-2017                                 |
|   Description: ERP Point of Sales Extension            |
|   Website: http://notrinos.com                         |
|                                                        |
\=======================================================*/

$page_security = 'SA_SALESMANAGER';
$path_to_root  = '../../..';

include_once($path_to_root . '/includes/session.inc');
add_access_extensions();

include_once($path_to_root . '/includes/ui.inc');
include_once($path_to_root . '/modules/pos/includes/pos_ui.inc');
include_once($path_to_root . '/modules/pos/includes/pos_db.inc');

//--------------------------------------------------------------------------

page(_($help_context = 'Manage POS Sales People'));
simple_page_mode(true);

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') {

	if(empty(trim($_POST['name']))) {
		display_error( _('Name field cannot be empty.'));
		set_focus('name');
	}
	else {
		$id = $selected_id == -1 ? false : $selected_id;
		write_salesman($id, $_POST['name'], $_POST['phone'], $_POST['email']);
		
    	if ($selected_id != -1)
			display_notification(_('Selected salesman has been updated'));
    	else
			display_notification(_('New salesman has been added'));
		
		$Mode = 'RESET';
	}
}

if ($Mode == 'Delete') {

	if(salesman_used($selected_id))
		display_error( _('This salesman cannot be deleted.'));
	else {
		delete_pos_salesman($selected_id);
		display_notification(_('Selected salesman has been deleted'));
	}
	$Mode = 'RESET';
}

if($Mode == 'RESET') {
	$selected_id = -1;
	$_POST['name'] = '';
	$_POST['phone'] = '';
	$_POST['email'] = '';
}

//--------------------------------------------------------------------------

start_form();

start_table(TABLESTYLE);
$th = array(_('Id'), _('Name'), _('Phone'), _('Email'), '', '');
inactive_control_column($th);
table_header($th);

$result = db_query(get_pos_salesman(false, check_value('show_inactive')));
$k = 0;
while ($myrow = db_fetch($result)) {
	alt_table_row_color($k);
	label_cell($myrow['id']);
	label_cell($myrow['name']);
	label_cell($myrow['phone']);
	label_cell($myrow['email']);
	inactive_control_cell($myrow['id'], $myrow['inactive'], 'pos_salesman', 'id');
	edit_button_cell('Edit'.$myrow['id'], _('Edit'));
	delete_button_cell('Delete'.$myrow['id'], _('Delete'));
	end_row();
}
inactive_control_row($th);
end_table(1);

start_table(TABLESTYLE2);

if($selected_id != -1) {
	
 	if ($Mode == 'Edit') {
		
		$myrow = get_pos_salesman($selected_id);
		$_POST['name']  = $myrow['name'];
		$_POST['phone']  = $myrow['phone'];
		$_POST['email']  = $myrow['email'];
		hidden('selected_id', $selected_id);
		label_row(_('ID:'), $myrow['id']);
 	}
}

text_row_ex(_('Name').':', 'name', 37, 50);
text_row_ex(_('Phone').':', 'phone', 37, 50);
text_row_ex(_('Email').':', 'email', 37, 50);

end_table(1);

submit_add_or_update_center($selected_id == -1, '', 'both');

end_form();
end_page();