//
//  RetrievePasswordViewController.m
//  Apex Mobile
//
//  Created by Ray on 14-3-17.
//  Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
//

#import "RetrievePasswordViewController.h"

@interface RetrievePasswordViewController ()

@end

@implementation RetrievePasswordViewController
- (IBAction)OnCancel:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
        ;
    }];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (IBAction)OnOk:(UIButton *)sender {
    
    NSString* user = self.editUser.text;
    NSString* email = self.editEmail.text;

    if(user.length==0||email.length==0)
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_error", nil) message:NSLocalizedString(@"alert_msg_uecanotempty", nil) 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];
        [alert show];
        return;
    }
    
    self.btnOk.enabled = false;
    self.btnCancel.enabled=false;
    
    self.mum.hidden=false;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        int ret=[ApexMobileNetwork RetrievePassword:user email:email];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.mum.hidden=true;
            self.btnOk.enabled = true;
            self.btnCancel.enabled=true;
            
            if (ret==RESULT_TRUE)
            {
                
                
                [self dismissViewControllerAnimated:true completion:^{
                    
                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_retrieve_success", nil) message:NSLocalizedString(@"alert_msg_email_sent", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
                    [alert show];
                    ;
                }];
            }
            else
            {
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_error", nil) message:[NSString stringWithFormat:NSLocalizedString(@"alert_msg_failed_retrieve_password", nil),ret] delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
                [alert show];
            }
            
            
            
        });
    });
}



- (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.
}

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self view] endEditing:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}
/*
#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
