//
//  ChangePasswordViewController.m
//  ALand
//
//  Created by Z on 16/5/11.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *labelOldPassword;
@property (weak, nonatomic) IBOutlet UITextField *labelNewPassword;
@property (weak, nonatomic) IBOutlet UIButton *buttonChangePassword;
@property (weak, nonatomic) IBOutlet UIButton *buttonEyes;

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.labelNewPassword.delegate = self;
    self.buttonEyes.imageView.contentMode = UIViewContentModeScaleAspectFit;
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)actionChagneState:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    self.labelNewPassword.secureTextEntry = sender.isSelected;
}

- (IBAction)actionChangePassword:(id)sender {
    
    if (self.labelNewPassword.text.length < 6 || self.labelNewPassword.text.length > 12) {
        [MBProgressHUD Javen_showError:@"密码长度6~12位" onView:kWindow delay:0.5 complete:nil];
        return;
    }
    
    UserInfoReseller *info = [UserInfo shareInstance].reseller;
    [MBProgressHUD Javen_showMessage:@"修改中" onView:self.view];
    NSDictionary *params = @{@"operCtx" : [[UserInfo shareInstance] getOperCtx],
                                   @"uuid" : info.uuid,
                                   @"oldPassword" : [self.labelOldPassword.text MD5String],
                                   @"newPassword" : [self.labelNewPassword.text MD5String],
                                   @"expiry" : @0};
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"reseller/modifyPassword" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view];

        if (kIsResponseSuccess(responseObject)) {
             [[NSUserDefaults standardUserDefaults] setObject:[weakSelf.labelNewPassword.text MD5String] forKey:@"passWord"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [weakSelf reloginAction];
            
            [MBProgressHUD Javen_showSuccess:@"修改成功！" onView:weakSelf.view delay:0.7 complete:^{
                [weakSelf popAction];
            }];
        }else{
            [MBProgressHUD Javen_showError:responseObject[@"message"] onView:weakSelf.view delay:0.7 complete:nil];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view];

    }];
}


- (void)reloginAction {
    NSDictionary *params = @{@"domain":kDomain,
                             @"loginName":[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"] ,
                             @"password":[[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"],
                             @"rememberMe":@"true"};
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"app/resellerLogin" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([responseObject[@"code"] isEqualToNumber:@0]) {
            
            UserInfo *info = [UserInfo shareInstance];
            [info updateModelObjectWithDictionary:responseObject];
            
        }else{
            
            [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"passWord"];
            [MBProgressHUD Javen_showError:responseObject[@"message"] onView:weakSelf.view delay:2 complete:nil];
            [self.tabBarController dismissViewControllerAnimated:YES completion:nil];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view];

    }];

}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.buttonChangePassword.enabled = str.length > 0;
    return YES;
}

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

/*
#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
