//
//  logViewViewController.m
//  melody app
//
//  Created by Odin on 04/10/2016.
//  Copyright © 2016 demain un autre jour. All rights reserved.
//

/*
 
 +----------------------------------------------------------------------------+
 | - This file is for the logView implementation							  |
 +----------------------------------------------------------------------------+

*/

#import "logViewViewController.h"

@interface logViewViewController ()

@end

@implementation logViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
	self.tablePeople.delegate = self;
	self.tablePeople.dataSource = self;
	// Initialize the dbManager property.
	self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"baseData.sql"];
	[self loadData];
}

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


//*****************************************//
//******** starting implementation ********//
//*****************************************//

-(void)loadData{
	// Form the query.
	NSString *query = @"select * from logs";
	
	// Get the results.
	if (self.arrPeopleInfo != nil) {
		self.arrPeopleInfo = nil;
	}
	self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
	
	// Reload the table view.
	[self.tablePeople reloadData];
}

- (IBAction)addNewRecord:(id)sender {
}


//*****************************************//
//******** how many rows in table ? *******//
//*****************************************//

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	// Dequeue the cell.
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCellRecord" forIndexPath:indexPath];
	NSInteger indexOfTitle = [self.dbManager.arrColumnNames indexOfObject:@"titre"];
	NSInteger indexOfDate = [self.dbManager.arrColumnNames indexOfObject:@"date"];
	
	// Set the loaded data to the appropriate cell labels.
	[cell.textLabel setFont:[UIFont fontWithName:@"Arial" size:12]];
	cell.textLabel.text = [NSString stringWithFormat:@"L' article  %@ à été envoyé le %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfTitle], [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfDate]];
	
	return cell;
}

//*****************************************//
//********* tables rows counting **********//
//*****************************************//

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}

//*****************************************//
//********* numbers of subrows ************//
//*****************************************//

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

//*****************************************//
//************** size of celss ************//
//*****************************************//

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 60.0;
}

- (IBAction)return:(id)sender {
	[self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"infoView"] animated:YES];
}
@end
