//
//  SearchTableAdapter.m
//  Apex Mobile
//
//  Created by Ray on 14-3-1.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//
 
#import "SearchTableAdapter.h"

@implementation SearchTableAdapter
- (id)init
{
    self = [super init];
    if (self) {
        fields = [[NSMutableDictionary alloc] init];
        names = [[NSMutableArray alloc] init];
        
    }
    return self;
}
-(id) get_item : (long) index
{
    
//    [fields objectForKey:@"<#string#>"]
    return [fields objectForKey:names[index] ];
}
-(long) get_itemcount
{
    return fields.count ;
}
-(void)initfields : (NSString*) function_name
{
    
    
    ApexMobileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    //    NSString* required_ver=[objheader objectForKey:@"client_ver"] ;
    //
    //    appDelegate.sessionid = [objheader valueForKey:@"sessionid"];
    //    appDelegate.user = user;
    //    appDelegate.password = password;
    
    sqlite3 *db =[ApexMobileDB get_db];
    NSString *quary = [NSString stringWithFormat:@"select aname,field_type,name,_id from fields_info where function_name='%@' and user='%@' and behavior=%d and show=1 order by priority,aname",function_name, appDelegate.user,BEHAVIOR_SEARCH];
    
    
    
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(db, [quary UTF8String], -1, &stmt, nil) == SQLITE_OK) {
           DebugLog(@"sql:%@",quary);
        while (sqlite3_step(stmt)==SQLITE_ROW)
        {
            NSString *aname = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
            NSString *field_type = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
            NSString *name = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];
//            int _id = sqlite3_column_int(stmt, 4);
            NSMutableDictionary * field =[[NSMutableDictionary alloc] init];
            
              DebugLog(@"name=%@;    type=%@;    aname=%@",name,field_type,aname);
            
            [field setValue:name forKey:@"name"];
            [field setValue:field_type forKey:@"type"];
            [field setValue:aname forKey:@"aname"];
            [field setValue:@"" forKey:@"value"];
            [field setValue:@"" forKey:[NSString stringWithFormat:@"%@_from",name]];
            [field setValue:@"" forKey:[NSString stringWithFormat:@"%@_to",name]];
            

            
            [fields setObject:field forKey:name];
            [names addObject:name];
            

            
            
        }  
        
        sqlite3_finalize(stmt);  
    }  
    //用完了一定记得关闭，释放内存  
    sqlite3_close(db);
}

@end
