//
//  RigistViewController.m
//  ALand
//
//  Created by Z on 16/3/17.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "RigistViewController.h"
#import "UIButton+countDown.h"
#import "AFNetworking.h"
#import "ToLoginViewController.h"
@interface RigistViewController ()
@property (weak, nonatomic) IBOutlet UIButton *sendCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *setPasswordVisible;
@property (nonatomic) BOOL isPasswordVisible;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFiled;

@end

@implementation RigistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isPasswordVisible = NO;
    self.navigationItem.title = @"注 册";
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}


- (IBAction)toAgreement:(UIButton *)sender {
    
}

- (IBAction)setPasswordVisible:(UIButton *)sender {
    self.isPasswordVisible = !self.isPasswordVisible;
    if (self.isPasswordVisible) {
        [self.setPasswordVisible setImage:[UIImage imageNamed:@"visible_icon"] forState:UIControlStateNormal];
        self.passwordTextFiled.secureTextEntry = NO;
    }else{
        [self.setPasswordVisible setImage:[UIImage imageNamed:@"Invisible_icon"] forState:UIControlStateNormal];
        self.passwordTextFiled.secureTextEntry = YES;

    }
}

// 发送验证码
- (IBAction)sendCodeAction:(UIButton *)sender {
    
    
    
    NSDictionary *parameters = @{@"mobilephone":self.phoneNumberTextFiled.text,
                                 @"template":@"1234"};
    WS(weakSelf)
    
    [[HTTPCilent shareCilent] GET:@"app/sendSms" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        [weakSelf.sendCodeBtn startWithTime:60 title:@"发送验证码" countDownTitle:@"s" mainColor:nil countColor:nil mainTitleColor:[UIColor colorWithHexString:@"51B2F6"] countTitleColor:[UIColor colorWithHexString:@"888888"]];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}

//注册
- (IBAction)actionNext:(id)sender {
 
    NSDictionary *parameters = @{@"domain":@"0001",
                                 @"invitationCode":@"",
                                 @"smsCode":@"1234",
                                 @"mobilephone":self.phoneNumberTextFiled.text,
                                 @"password":[self.passwordTextFiled.text MD5String]};

    WS(weakSelf)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPCilent shareCilent] POST:@"app/resellerRegister" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        
        if ([responseObject[@"code"] isEqualToNumber:@0]) {
            
            [[NSUserDefaults standardUserDefaults] setObject:weakSelf.phoneNumberTextFiled.text forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] setObject:[weakSelf.passwordTextFiled.text MD5String] forKey:@"passWord"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];


            
        }else{
            
            [MBProgressHUD Javen_showError:responseObject[@"message"] onView:weakSelf.view delay:2 complete:^{
                
            }];
        }

        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated: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
