//
//  SymCheckerPossibleCondtionViewController.m
//  MedicalHelp
//
//  Created by mac on 6/30/14.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import "SymCheckerPossibleCondtionViewController.h"


@implementation SymCheckerPossibleCondtionViewController

@synthesize tblTablePC,arrayPC,arrayTblPC,arrayIndexfkSymptomid,arraypkPCid,indexpkPCid,lblTotalPossibleCondition;

- (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];
    
    arrayTblPC = [[NSMutableArray alloc]initWithArray:[req getList:[NSString stringWithFormat:@"select * from tblpossiblecondition inner join tblpossibleconditionforsymptom on pkpcid = fkpcid inner join tblsymptom on pksid = fksid %@",[NSString stringWithString:[self formatForignKey]]]]];
    
    
    //Setup array Each Array Resepresented One Column of Table
    arraypkPCid  = [[NSMutableArray alloc]init];
    arrayPC = [[NSMutableArray alloc]init];
    //arrayfkBPid = [[NSMutableArray alloc]init];
    
    
    //Convert Individual Array.Each Array Resepresented One Column of Table
    for (NSDictionary *dict in arrayTblPC) {
        [arraypkPCid addObject:[dict objectForKey:@"pkPCid"]];
        [arrayPC addObject:[dict objectForKey:@"PossibleCondition"]];
    }
    
    //Set Total Possible condition to lable
    lblTotalPossibleCondition.text = [NSString stringWithFormat:@"Total Possible Condition Found: %d",arraypkPCid.count];
    
    //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:@"Possible Condition(s)"];
    
    //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.tblTablePC.frame];
    self.tblTablePC.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 = [arrayPC 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 arrayPC.count;
}

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

-(NSString *)formatForignKey
{
    //this will generate:(according to multiple selection) like:-
    //where fksid = 2 or fksid = 3 or fksid = 4
    
    NSMutableString *str = [[NSMutableString alloc]initWithString:@"where "];
    
    if (arrayIndexfkSymptomid.count>=2) {
        for (int i = 0; i<=arrayIndexfkSymptomid.count-2; i++) {
            str = [NSMutableString stringWithString:[str stringByAppendingFormat:@"fksid = %@ or ",[arrayIndexfkSymptomid objectAtIndex:i]]];
        }
    }
    else
    {
        str = [NSMutableString stringWithString:[str stringByAppendingFormat:@"fksid = %@ or ",[arrayIndexfkSymptomid objectAtIndex:0]]];
    }
    
    str = [NSMutableString stringWithString:[str stringByAppendingFormat:@"fksid = %@",[arrayIndexfkSymptomid objectAtIndex:arrayIndexfkSymptomid.count-1]]];
    
    return str;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexpkPCid = [[arraypkPCid objectAtIndex:indexPath.row] integerValue];
    
    SymCheckerTreatmentViewController *v1 = [[SymCheckerTreatmentViewController alloc]init];
    
    v1.indexpkPCid = self.indexpkPCid;
    
    [self.navigationController pushViewController:v1 animated:YES];
}
@end

