//
//  DietViewController.m
//  MedicalHelp
//
//  Created by mac on 7/14/14.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import "DietViewController.h"

@implementation DietViewController

@synthesize tblDiet,arraytblDiet,arraypkDid,arrayTitle,arraySource;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    RequestHandler *req = [[RequestHandler alloc]init];
    
    //Fire query and
    //Recieve Whole table in Array -->Dictonary
    arraytblDiet = [[NSMutableArray alloc]initWithArray:[req getList:@"SELECT * FROM tbldiet ORDER BY pkDid"]];

    
    //Setup array Each Array Resepresented One Column of Table

    arraypkDid = [[NSMutableArray alloc]init];
    arrayTitle = [[NSMutableArray alloc]init];
    arraySource = [[NSMutableArray alloc]init];
    
    //Convert Individual Array.Each Array Resepresented One Column of Table
    for (NSDictionary *dict in arraytblDiet) {
        [arraypkDid addObject:[dict objectForKey:@"pkDid"]];
        [arrayTitle addObject:[dict objectForKey:@"Title"]];
        [arraySource addObject:[dict objectForKey:@"Source"]];
    }
    
    //Add bar button item to top
    UIBarButtonItem *btnHome = [[UIBarButtonItem alloc]initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(goHome)];
    
    [self.navigationItem setRightBarButtonItem:btnHome];
    
    //and set title
    [self.navigationItem setTitle:@"Diet Information"];
    
    //Add Background
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wallpaper1.png"]];
    
    //Add Background in tableview
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wallpaper1.png"]];
    [tempImageView setFrame:self.tblDiet.frame];
    self.tblDiet.backgroundView = tempImageView;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text = [arrayTitle objectAtIndex:indexPath.row];
    
    //cell.detailTextLabel.text = [arrayMainMenuDetail objectAtIndex:indexPath.row];
    
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
    cell.imageView.image = [UIImage imageNamed:@"arrow-big-02.png"];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrayTitle.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DietWebViewController *v1 =[[DietWebViewController alloc]init];
    
    v1.htmlSource = [self.arraySource objectAtIndex:indexPath.row] ;
    
    [self.navigationController pushViewController:v1 animated:YES];
}

-(void)goHome
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
