<?php
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добаления/редактирования цен.
 * 
 * Файл: /admin/shop/action/form_edit_add_prices.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */ 
if (isset($id))
{
	$edit_prices = $id;
}
else
{
	$edit_prices = 0;
}

$admin_forms_fields = new admin_forms_fields();
$form_params = array();
$form_params['title'] = $edit_prices==0?$GLOBALS['MSG_shops']['prices_add_form_title']:$GLOBALS['MSG_shops']['prices_edit_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddPrice';
$shop = & singleton('shop');
$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);

$DataBase = & singleton('DataBase');

$kernel = & singleton('kernel');

$row = $shop->GetShop($shop_shops_id);

$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

// Преобразуем идентификаторы групп в ссылки на группы.
$new_path_array = array();
$new_path_array[0]['name'] = $GLOBALS['MSG_shops']['header_admin__list_forms'];
$new_path_array[0]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');
$new_path_array[0]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');
$new_path_array[1]['name'] = $row['shop_shops_name'];
$new_path_array[1]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');
$new_path_array[1]['onclick'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');

if ($path_array)
{
	foreach ($path_array as $key => $value)
	{

		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'name' => $value);
	}
}
$new_path_array[] = array("name" => $GLOBALS['MSG_shops']['show_prices_title'], "link" =>  $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id", 68, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id", 68, 'load_data'));

$form_params['path_array'] = $new_path_array;

$admin_forms_fields = new admin_forms_fields($form_params);

// Редактировние налога
if ($edit_prices)
{
	$type = 1;

	$result = $shop->GetPrice($edit_prices);

	$shop_list_of_prices_id = to_int($result['shop_list_of_prices_id']);

	$shop_shops_id = to_int($result['shop_shops_id']);

	$shop_list_of_prices_name = to_str($result['shop_list_of_prices_name']);

	$shop_list_of_prices_percent_to_basic = to_float($result['shop_list_of_prices_percent_to_basic']);

	$site_users_group_id = to_int($result['site_users_group_id']);

	$users_id = to_int($result['users_id']);
	
	$shop_list_of_prices_cml_id = to_str($result['shop_list_of_prices_cml_id']);
}
// Добавление налога
else
{
	$shop_list_of_prices_id = 0;

	$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

	$shop_list_of_prices_name = '';

	$shop_list_of_prices_percent_to_basic = '';

	$site_users_group_id = 0;

	$users_id = false;
	
	$shop_list_of_prices_cml_id = $kernel->Guid();
}

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab']);

// Название
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'prices_name';
$params['caption'] = $GLOBALS['MSG_shops']['prices_add_form_name'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_list_of_prices_name;
$params['attributes']['class'] = 'large';
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Заполнение поля "Группа"
$arr = array();

if (class_exists('SiteUsers'))
{
	$site_users = & singleton('SiteUsers');

	$row = $site_users->SelectSiteUsersGroups();

	while ($res = mysql_fetch_assoc($row))
	{
		$arr[$res['site_users_group_id']] = $res['site_users_group_name'];
	}
}

// Группа
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'prices_users_group';
$params['caption'] = $GLOBALS['MSG_shops']['items_catalog_user_group'];
$params['type'] = 2; // Выпадающий список.
$params['items'] = $arr;
$params['value'] = $site_users_group_id;

$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Процент к базовой
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'prices_percent_to_basic';
$params['caption'] = $GLOBALS['MSG_shops']['prices_add_form_percent_to_basic'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_list_of_prices_percent_to_basic;
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 10;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Редактирование цены товаров
if ($edit_prices)
{	
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'recalculate_price';
	$param['html_id'] = 'recalculate_price_id';
	$param['caption'] = $GLOBALS['MSG_shops']['prices_add_form_recalculate'];
	$param['type'] = 1; // Флажок
	$param['value'] = 0;
	//$param['div_attributes']['style'] = 'float: left;';
	//$param['attributes']['onclick'] = "document.getElementById('value_prices_to_item_{$row['shop_list_of_prices_id']}').disabled = !this.checked; if (this.checked) {document.getElementById('value_prices_to_item_{$row['shop_list_of_prices_id']}').value = (document.getElementById('items_catalog_price').value * {$row['shop_list_of_prices_percent_to_basic']} / 100).toFixed(2);}";
	$admin_forms_fields->AddField($param);
}
else
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'apply_for_all';
	$param['html_id'] = 'apply_for_all_id';
	$param['caption'] = $GLOBALS['MSG_shops']['prices_add_form_apply_for_all'];
	$param['type'] = 1; // Флажок
	$param['value'] = 1;
	//$param['div_attributes']['style'] = 'float: left;';
	//$param['attributes']['onclick'] = "document.getElementById('value_prices_to_item_{$row['shop_list_of_prices_id']}').disabled = !this.checked; if (this.checked) {document.getElementById('value_prices_to_item_{$row['shop_list_of_prices_id']}').value = (document.getElementById('items_catalog_price').value * {$row['shop_list_of_prices_percent_to_basic']} / 100).toFixed(2);}";
	$admin_forms_fields->AddField($param);
}

$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab_yandex_market']);

// CML ID
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_list_of_prices_cml_id';
$param['caption'] = $GLOBALS['MSG_shops']['1c_commerce_ml_price'];
$param['type'] = 0; // Поле ввода.
$param['value'] = $shop_list_of_prices_cml_id;
$param['format']['maxlen']['value'] = 100;
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);


// передеам скрытое поле с идентификатором адреса если он есть
if ($edit_prices)
{
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'edit_prices';
	$params['caption'] = '';
	$params['type'] = 4; // hidden.
	$params['value'] = $edit_prices;
	$admin_forms_fields->AddField($params);
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'action';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = 'show_prices';
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_shops_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_shops_id;
$admin_forms_fields->AddField($params);

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_prices';
$param['caption'] = $GLOBALS['MSG_shops']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$params = array();
$params['name'] = 'add_prices';
$params['caption'] = $GLOBALS['MSG_shops']['apply'];
$params['image'] = '/admin/images/add.gif';
$params['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).")";
$params['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($params);

// Отображение формы.
$admin_forms_fields->ShowForm();
?>