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

$form_params = array();
$form_params['title'] = $shop_warehouse_id==0?$GLOBALS['MSG_shops']['main_menu_warehouses_edit_add']:$GLOBALS['MSG_shops']['main_menu_warehouses_edit'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddWarehouse';

$admin_forms_fields = & singleton('admin_forms_fields');

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

$shop = & singleton('shop');

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

$DataBase = & singleton('DataBase');

$warehouse = & singleton('warehouse');

$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->GetOnClickCallDoLoadAjax('/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']['main_menu_warehouses_list'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 181, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 181, 'load_data')
);
$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

if ($shop_warehouse_id)
{
	$result = $warehouse->GetWarehouse($shop_warehouse_id);
	$shop_warehouse_name = $result['shop_warehouse_name'];
	$shop_warehouse_activity = $result['shop_warehouse_activity'];
	$shop_country_id = $result['shop_country_id'];
	$shop_location_id = $result['shop_location_id'];
	$shop_city_id = $result['shop_city_id'];
	$shop_city_area_id = $result['shop_city_area_id'];
	$shop_warehouse_address = $result['shop_warehouse_address'];
	$shop_warehouse_order = $result['shop_warehouse_order'];
	$shop_warehouse_default = $result['shop_warehouse_default'];
}
else
{
	$shop_warehouse_name = '';
	$shop_warehouse_activity = 1;
	$shop_country_id = 0;
	$shop_location_id = 0;
	$shop_city_id = 0;
	$shop_city_area_id = 0;
	$shop_warehouse_address = '';
	$shop_warehouse_order = 0;
	$shop_warehouse_default = 0;
}

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

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_warehouse_name';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_name'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_warehouse_name;
$params['attributes']['class'] = 'large';
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$params['div_attributes']['style'] = 'width: 620px;';
$admin_forms_fields->AddField($params);

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

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_warehouse_order';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_order'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_warehouse_order;
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 200px;';
$admin_forms_fields->AddField($params);

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

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_warehouse_activity';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_activity'];
$params['type'] = 1; // Поле ввода.
$params['value'] = $shop_warehouse_activity;
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 200px; margin-top: 20px;';
$admin_forms_fields->AddField($params);

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

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_warehouse_default';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_default'];
$params['type'] = 1; // Поле ввода.
$params['value'] = $shop_warehouse_default;
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 200px; margin-top: 20px;';
$admin_forms_fields->AddField($params);

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



// Запоняем поле "Страна"
$country_array = array(' ... ');
$res = $shop->GetAllCountries();

if ($res)
{
	while ($row = mysql_fetch_assoc($res))
	{
		$country_array[$row['shop_country_id']] = $row['shop_country_name'];
	}
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'country_id';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_country'];
$params['type'] = 2; // Поле ввода.
$params['value'] = $shop_country_id;
$params['items'] = $country_array;
$params['html_id'] = 'country_id';
$params['attributes']['onchange'] = "doSetLocation(this.options[this.selectedIndex].value)";
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($params);

// Заполняем поле "Область"
$location_array = array(' ... ');

// Расположения заполняем только тогда, когда страна указана
if ($shop_country_id)
{
	$res = $shop->GetAllLocation($shop_country_id);
	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$location_array[$row['shop_location_id']] = $row['shop_location_name'];
		}
	}
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'location_id';
$params['caption'] =$GLOBALS['MSG_shops']['form_edit_add_warehouse_location'];
$params['type'] = 2; // Поле ввода.
$params['value'] = $shop_location_id;
$params['items'] = $location_array;
$params['html_id'] = 'location_id';
$params['attributes']['onchange'] = "doSetCity(this.options[this.selectedIndex].value)";
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($params);

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

// Заполняем поле "Город"
$city_array = array();

$city_array[0] = ' ... ';

// Города заполняем только тогда, когда расположение указано
if ($shop_location_id)
{
	$res = $shop->GetAllCity($shop_location_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$city_array[$row['shop_city_id']] = $row['shop_city_name'];
		}
	}
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'city_id';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_city'];
$params['type'] = 2; // Поле ввода.
$params['value'] = $shop_city_id;
$params['items'] = $city_array;
$params['attributes']['onchange'] = "doSetCityArea(this.options[this.selectedIndex].value)";
$params['html_id'] = 'city_id';
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($params);

// Заполняем поле "Район"
$city_area_array = array(' ... ');

// Города заполняем только тогда, когда расположение указано
if ($shop_city_id)
{
	$res = $shop->GetAllCityArea($shop_city_id);

	if ($res)
	{
		while ($row = mysql_fetch_assoc($res))
		{
			$city_area_array[$row['shop_city_area_id']] = $row['shop_city_area_name'];
		}
	}
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'city_area_id';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_city_area'];
$params['type'] = 2; // Поле ввода.
$params['value'] = $shop_city_area_id;
$params['items'] = $city_area_array;
$params['html_id'] = 'city_area_id';
$params['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($params);

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

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_warehouse_address';
$params['caption'] = $GLOBALS['MSG_shops']['form_edit_add_warehouse_city_address'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_warehouse_address;
$params['div_attributes']['style'] = 'width: 620px;';
$admin_forms_fields->AddField($params);

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

// передаeм скрытое поле с идентификатором адреса если он есть
if ($shop_warehouse_id)
{
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'shop_warehouse_id';
	$params['caption'] = '';
	$params['type'] = 4; // hidden.
	$params['value'] = $shop_warehouse_id;
	$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);

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

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_warehouse';
$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_warehouse';
$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']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$params['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($params);

ob_start();
?>
<script type="text/javascript">
location_select_id = "location_id";
city_select_id = "city_id";
cityarea_select_id = "city_area_id";
</script>
<?php
$js_code = ob_get_clean();

$admin_forms_fields->SetExternalHtml($js_code);

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