//
//  HistoryViewController.m
//  Apex Mobile
//
//  Created by Ray on 14-4-24.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "HistoryViewController.h"

@interface HistoryViewController ()

@end

@implementation HistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    self.data =[[HistoryData alloc]init];
    [self.tableview reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.data get_count];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DebugLog(@"cellForRowAtIndexPath");
    
    NSString *CellIdentifier = @"cell_item_history";
    CellItemHistory *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSString* name = [self.data.pagedata[indexPath.row] valueForKey:@"name"];
    NSString* create_time = [self.data.pagedata[indexPath.row] valueForKey:@"create_time"];

    
    cell.name.text = name;
    [cell.name sizeToFit];
    
    cell.time.text =create_time;
    [cell.time sizeToFit];
    
    //    cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ResultViewController *resultViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ResultViewController"];
    
    resultViewController.function_name = [self.data.pagedata[indexPath.row] valueForKey:@"module_name"];

    NSString* str_params =[self.data.pagedata[indexPath.row] valueForKey:@"params"];
    NSData* data_params = [str_params dataUsingEncoding:NSUTF8StringEncoding];
    
    
    
    NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithDictionary:[NSJSONSerialization
                                                                                   JSONObjectWithData:data_params
                                                                                   options:NSJSONReadingMutableLeaves
                                                                                   error:nil] copyItems:true];

    resultViewController.params =params;
    [self.navigationController pushViewController:resultViewController animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    //不能是UITableViewCellEditingStyleNone
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  forRowAtIndexPath:(NSIndexPath *)indexPath {
    sqlite3* db = [ApexMobileDB get_db];
    NSString* sql = [NSString stringWithFormat:@"delete from history where _id=%@",[self.data.pagedata[indexPath.row] valueForKey:@"_id"]];
    [ApexMobileDB execSql:sql db:db];
    sqlite3_close(db);
    
    [self.data.pagedata removeObjectAtIndex:indexPath.row];
    
    [tableView reloadData];
    
}
@end
