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

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()

@end

@implementation ChangePasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (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)onChange:(UIButton *)sender {
    
    NSString* oldpass = self.editOld.text;
    NSString* newpass = self.editNew.text;
    NSString* confirmpass = self.editConfirm.text;
    ApexMobileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSString* user = appDelegate.user;
    if(oldpass.length==0||oldpass.length==0||confirmpass.length==0)
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_error", nil) message:NSLocalizedString(@"alert_msg_fieldscanotempty", 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;
    }
    if(! [newpass isEqualToString:confirmpass])
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_error", nil) message:NSLocalizedString(@"alert_msg_password_notequal", 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.btnChange.enabled = false;
    
    self.mum.hidden=false;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        int ret=[ApexMobileNetwork ChangePassword:newpass user:user oldpass:oldpass];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.mum.hidden=true;
            self.btnChange.enabled = true;

            
            if (ret==RESULT_TRUE)
            {
                 ApexMobileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
                appDelegate.password = newpass;
                
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_success", nil) message:NSLocalizedString(@"alert_msg_change_success", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
                [alert show];
                [self.navigationController popViewControllerAnimated:true ];
            }
            else
            {
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_error", nil) message:[NSString stringWithFormat:NSLocalizedString(@"alert_msg_failed_change_password", nil),ret] delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
                [alert show];
            }
            
            
            
        });
    });
    
}
- (IBAction)onCancel:(UIButton *)sender {
}
- (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
