//
//  OfflineUnlockViewController.m
//  iSales-NPD
//
//  Created by Ray on 10/10/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "OfflineUnlockViewController.h"
#import "RAUtils.h"
#import "iSalesDB.h"
#import "AESCrypt.h"


@interface OfflineUnlockViewController ()

@end

@implementation OfflineUnlockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)OnUnlockClicked:(id)sender {
    NSString* user = self.edituser.text;
    NSString* password = self.editpass.text;
    
    
//    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    
    
    if(user.length==0||password.length==0)
    {
        //        UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Error!" message:@"user or password can not be empty." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
        //        //        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
        //
        //
        //        //[[UILabel appearanceWhenContainedIn:UIAlertView.class, nil] setAppearanceFont:[UIFont boldSystemFontOfSize:10]];
        //        [alert show];
        
        
        [RAUtils alert_view:@"Username or Password cannot be blank." title:@"Unlock offline"];
        return;
    }
    
    sqlite3* db = [iSalesDB get_db];
    int offline_user_exist=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]]];
    
    [iSalesDB close_db:db];
    
    if(offline_user_exist>0)
        [self dismissViewControllerAnimated:YES completion:^{
            
                    if(self.returnValue)
                    self.returnValue(true);

        }];
    else
        [RAUtils alert_view:MSG_USERAUTH_ERROR title:@"Unlock offline"];
    
}
- (IBAction)CloseClick:(id)sender {
    
    [self dismissViewControllerAnimated:true completion:nil];
    if(self.returnValue)
        self.returnValue(false);
    
    
    
}
/*
#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.
}
*/

@end
