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

#import "SymCheckerSymptomViewController.h"

@implementation SymCheckerSymptomViewController

@synthesize arraypkSymptomid,arraySymptom,arrayTblSymptom,lblSymptomSelectionCount,strSBPName,indexfkSBP,arrayIndexpkSymptomid;

- (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
    arrayTblSymptom = [[NSMutableArray alloc]initWithArray:[req getList:[NSString stringWithFormat:@"select pksid,symptom,subbodypart from tblsymptom inner join tblsymptominsubbodypart on pksid =fksid inner join tblsubbodypart on pksbpid = fksbpid where fksbpid = %d",indexfkSBP]]];
    
    //this is used for selection of sysmptm that contain primary key of symptoms
    arrayIndexpkSymptomid = [[NSMutableArray alloc]init];
    
    
    //Setup array Each Array Resepresented One Column of Table
    arraypkSymptomid  = [[NSMutableArray alloc]init];
    arraySymptom = [[NSMutableArray alloc]init];
    //arrayfkBPid = [[NSMutableArray alloc]init];
    
    
    //Convert Individual Array.Each Array Resepresented One Column of Table
    for (NSDictionary *dict in arrayTblSymptom) {
        [arraypkSymptomid addObject:[dict objectForKey:@"pksid"]];
        [arraySymptom addObject:[dict objectForKey:@"symptom"]];
    }
        
    //set default 0 value to selected symptoms lable
    lblSymptomSelectionCount.text = [NSString stringWithFormat:@"Selected Symptom(s): %d",arrayIndexpkSymptomid.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:strSBPName];
    
    //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.tblTableSympt.frame];
    self.tblTableSympt.backgroundView = tempImageView;
}

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

- (IBAction)btnSymptomNext:(id)sender {
    
    if (arrayIndexpkSymptomid.count<=0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Select at least One Symptom" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        
        [alert show];
    }
    else
    {
        
    SymCheckerPossibleCondtionViewController *v1 = [[SymCheckerPossibleCondtionViewController alloc]init];
    
    v1.arrayIndexfkSymptomid = self.arrayIndexpkSymptomid;
    
    [self.navigationController pushViewController:v1 animated:YES];
        
    }
}

-(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 = [arraySymptom objectAtIndex:indexPath.row];
    
    return cell;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *selStr = [arraypkSymptomid objectAtIndex:indexPath.row];
    
    [self.tblTableSympt deselectRowAtIndexPath:indexPath animated:YES];
    
    if (thisCell.accessoryType == UITableViewCellAccessoryNone)
    {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [arrayIndexpkSymptomid addObject:selStr];
    }
    else
    {
        thisCell.accessoryType = UITableViewCellAccessoryNone;
        [arrayIndexpkSymptomid removeObject:selStr];
    }
    
    lblSymptomSelectionCount.text = [NSString stringWithFormat:@"Selected Symptom(s): %d",arrayIndexpkSymptomid.count];
}

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

@end


