/*#######################################################################################################################################################
#################################################  Вспомогательные функции для С-Бизнесов		#########################################################
#########################################################################################################################################################*/

stock ReturnIndexSBusinesForID ( id )
{
	for ( new sid = 0; sid < SBUSINESSCount; sid++ ) if ( SBUSINESS [ sid ] [ ID ] == id ) return sid;
	return -1;
}


stock ReturnIndexSalon ( playerid )
{
	if ( IsPlayerInRangeOfPoint ( playerid, 20.00, SBUSINESS [ LS_AUTO ] [ Position ] [ 0 ] , SBUSINESS [ LS_AUTO ] [ Position ] [ 1 ] , SBUSINESS [ LS_AUTO ] [ Position ] [ 2 ] ) ) return LS_AUTO;
	
	else if ( IsPlayerInRangeOfPoint ( playerid, 20.00, SBUSINESS [ SF_AUTO ] [ Position ] [ 0 ] , SBUSINESS [ SF_AUTO ] [ Position ] [ 1 ] , SBUSINESS [ SF_AUTO ] [ Position ] [ 2 ] ) ) return SF_AUTO;
	
	else if ( IsPlayerInRangeOfPoint ( playerid, 20.00, SBUSINESS [ LV_AUTO ] [ Position ] [ 0 ] , SBUSINESS [ LV_AUTO ] [ Position ] [ 1 ] , SBUSINESS [ LV_AUTO ] [ Position ] [ 2 ] ) ) return LV_AUTO;
	
	return -1;
}


stock ReturnNearIndexSBusines ( playerid, Float:Radius  )
{
	for ( new sid = 0; sid < SBUSINESSCount; sid++ )
	{
		if ( !SBUSINESS [ sid ] [ ID ] ) continue;
		
		if ( IsPlayerInRangeOfPoint ( playerid, Radius, SBUSINESS [ sid ] [ Position ] [ 0 ] , SBUSINESS [ sid ] [ Position ] [ 1 ] , SBUSINESS [ sid ] [ Position ] [ 2 ]  ) ) return sid;
	}
	return -1;
}


stock DeleteSbusinesItems ( i )
{
	DestroyDynamicPickup ( SBUSINESS [ i ] [ PickupId ] ) ;
	DestroyDynamicMapIcon ( SBUSINESS [ i ] [ IconId ] [ 0 ] ) ;
	DestroyDynamic3DTextLabel ( SBUSINESS [ i ] [ Text ] ) ;
	
	return 1;
}


stock CreateSbusinesItems ( i )
{
	new text [ 128 ] ;
	
	SBUSINESS [ i ] [ PickupId ] = CreateDynamicPickup ( SBUSINESS [ i ] [ PickupModel  ], 23, SBUSINESS [ i ] [ Position ] [ 0 ], SBUSINESS [ i ] [ Position ] [ 1 ], SBUSINESS [ i ] [ Position ] [ 2 ], 0, 0 ) ;
	
	if ( SBUSINESS [ i ] [ Owner ] == -1 )
	{
		echo ( text, "{FF0000}Продается бизнес \"%s\" (#%d)\nЦена: $%d\n(( Что-бы купить, пиши /купить cбизнес ))", SBUSINESS [ i ] [ Name ], SBUSINESS [ i ] [ ID ], SBUSINESS [ i ] [ Price ] ) ;
	}
	else
	{
		echo ( text, "{00FF00}Бизнес \"%s\" (#%d)\nВладелец: %s", SBUSINESS [ i ] [ Name ], SBUSINESS [ i ] [ ID ], SBUSINESS [ i ] [ OwnerName ] ) ;
	}
	
	SBUSINESS [ i ] [ IconId ] [ 0 ] = CreateDynamicMapIcon ( SBUSINESS [ i ] [ Position ] [ 0 ], SBUSINESS [ i ] [ Position ] [ 1 ], SBUSINESS [ i ] [ Position ] [ 2 ], SBUSINESS [ i ] [ IconId ] [ 1 ], 0, 0, 0, -1, 200.0 ) ;
	
	SBUSINESS [ i ] [ Text ] = CreateDynamic3DTextLabel ( text, -1, SBUSINESS [ i ] [ Position ] [ 0 ], SBUSINESS [ i ] [ Position ] [ 1 ], SBUSINESS [ i ] [ Position ] [ 2 ] + 0.7, 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, 0, 0 ) ;
	
	return 1;
}


/*#######################################################################################################################################################
################################################# 		Загружаем СБизнесны		  	  ###################################################################
#########################################################################################################################################################*/


forward LoadSBUSINESS ( ) ;
public LoadSBUSINESS ( ) 
{
	new rows, fields, temp [ 50 ] ;
	cache_get_data ( rows, fields ) ;
	SBUSINESSCount = rows;
	if ( rows )
	{
		for ( new i = 0; i < rows; i++ )
		{
			cache_get_field_content ( i, "id", temp ), SBUSINESS [ i ] [ ID ] = strval ( temp ) ;
			cache_get_field_content ( i, "price", temp ), SBUSINESS [ i ] [ Price ] = strval ( temp ) ;
			cache_get_field_content ( i, "cost", temp ), SBUSINESS [ i ] [ Cost ] = strval ( temp ) ;
			cache_get_field_content ( i, "money", temp ), SBUSINESS [ i ] [ Money ] = strval ( temp ) ;
			cache_get_field_content ( i, "month_income", temp ), SBUSINESS [ i ] [ MonthIncome ] = strval ( temp ) ;
			cache_get_field_content ( i, "owner", temp ), SBUSINESS [ i ] [ Owner ] = strval ( temp ) ;
			cache_get_field_content ( i, "player_name", temp ), sscanf ( temp, "s[24]", SBUSINESS [ i ] [ OwnerName ] ) ;
			cache_get_field_content ( i, "name", temp ), sscanf ( temp, "s[50]", SBUSINESS [ i ] [ Name ] ) ;
			cache_get_field_content ( i, "pos_x", temp ), SBUSINESS [ i ] [ Position ] [ 0 ] = floatstr ( temp ) ;
			cache_get_field_content ( i, "pos_y", temp ), SBUSINESS [ i ] [ Position ] [ 1 ] = floatstr ( temp ) ;
			cache_get_field_content ( i, "pos_z", temp ), SBUSINESS [ i ] [ Position ] [ 2 ] = floatstr ( temp ) ;
			cache_get_field_content ( i, "id_pickup", temp ), SBUSINESS [ i ] [ PickupModel  ] = strval ( temp ) ;
			cache_get_field_content ( i, "map_icon", temp ), SBUSINESS [ i ] [ IconId ] [ 1 ] = strval ( temp ) ;
			
			CreateSbusinesItems ( i ) ;
			
		}
		
		printf ( "Загружено С-Бизнесов: %d", SBUSINESSCount ) ;
	}
	else
	{
		print ( "С-Бизнесы не созданы" ) ;
	}
	return 1;
}

/*#######################################################################################################################################################
#################################################			 Сохраняем СБизнесы		  ###################################################################
#########################################################################################################################################################*/

stock SaveSBUSINESS (  ) 
{
	for ( new sid = 0; sid < SBUSINESSCount; sid++ )
	{
		if ( !SBUSINESS [ sid ] [ ID ] ) continue;
		
		printf ( "Сохранение С-Бизнеса: %s ( ID: %d )", SBUSINESS [ sid ] [ Name ], SBUSINESS [ sid ] [ ID ] ) ;
		
		new query [ 512 ] ;
		
		new escape_name [ 50 ] ;
		mysql_real_escape_string ( SBUSINESS [ sid ] [ Name ], escape_name ) ;
		
		echo ( query, 
		"UPDATE `s_business` SET \
		`owner` = '%d', \
		`price` = '%d', \
		`cost` = '%d', \
		`money` = '%d', \
		`month_income` = '%d', \
		`name` = '%s', \
		`pos_x` = '%f', \
		`pos_y` = '%f', \
		`pos_z` = '%f', \
		`id_pickup` = '%d' \
		WHERE `id` = '%d' LIMIT 1", 
		SBUSINESS [ sid ] [ Owner ], 
		SBUSINESS [ sid ] [ Price ], 
		SBUSINESS [ sid ] [ Cost ], 
		SBUSINESS [ sid ] [ Money ], 
		SBUSINESS [ sid ] [ MonthIncome ], 
		escape_name, 
		SBUSINESS [ sid ] [ Position ] [ 0 ],
		SBUSINESS [ sid ] [ Position ] [ 1 ],
		SBUSINESS [ sid ] [ Position ] [ 2 ],
		SBUSINESS [ sid ] [ PickupModel ],
		SBUSINESS [ sid ] [ ID ] ) ;

		mysql_function_query ( dbHandle, query, false, "", "" ) ;
	}
	return 1;
}

stock SaveSBUSINES ( sid ) 
{
	if ( !SBUSINESS [ sid ] [ ID ] ) continue;
	
	printf ( "Сохранение С-Бизнеса: %s ( ID: %d )", SBUSINESS [ sid ] [ Name ], SBUSINESS [ sid ] [ ID ] ) ;
	
	new query [ 512 ] ;
	
	new escape_name [ 50 ] ;
	mysql_real_escape_string ( SBUSINESS [ sid ] [ Name ], escape_name ) ;
	
	echo ( query, 
	"UPDATE `s_business` SET \
	`owner` = '%d', \
	`price` = '%d', \
	`cost` = '%d', \
	`money` = '%d', \
	`month_income` = '%d', \
	`name` = '%s', \
	`pos_x` = '%f', \
	`pos_y` = '%f', \
	`pos_z` = '%f', \
	`id_pickup` = '%d' \
	WHERE `id` = '%d' LIMIT 1", 
	SBUSINESS [ sid ] [ Owner ], 
	SBUSINESS [ sid ] [ Price ], 
	SBUSINESS [ sid ] [ Cost ], 
	SBUSINESS [ sid ] [ Money ], 
	SBUSINESS [ sid ] [ MonthIncome ], 
	escape_name, 
	SBUSINESS [ sid ] [ Position ] [ 0 ],
	SBUSINESS [ sid ] [ Position ] [ 1 ],
	SBUSINESS [ sid ] [ Position ] [ 2 ],
	SBUSINESS [ sid ] [ PickupModel ],
	SBUSINESS [ sid ] [ ID ] ) ;
	
	mysql_function_query ( dbHandle, query, false, "", "" ) ;
	return 1;
}
