//
//  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"
#import "AgreementViewController.h"
#import "NSString+Category.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 {
    AgreementViewController *agreementVC = [[AgreementViewController alloc] init];
    agreementVC.isShowNavigationBar = YES;
    [self.navigationController pushViewController:agreementVC animated:YES];
}

- (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 {

    if ([self.phoneNumberTextFiled.text isMobileNumber]) {
        [MBProgressHUD Javen_showError:@"请输入正确的号码" onView:kWindow delay:0.5 complete:nil];
        return;
    }
    
    NSDictionary *parameters = @{@"mobilephone":self.phoneNumberTextFiled.text,
                                 @"template":[NSNull null]};
    WS(weakSelf)
    
    [[HTTPCilent shareCilent] GET:@"app/sendSms" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        CLog(@"------->发送验证码");
        [weakSelf.sendCodeBtn startWithTime:60 title:@"发送验证码" countDownTitle:@"秒" mainColor:nil countColor:nil mainTitleColor:[UIColor colorWithHexString:@"51B2F6"] countTitleColor:[UIColor colorWithHexString:@"888888"]];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}

//注册
- (IBAction)actionNext:(id)sender {
 
    if ([self.phoneNumberTextFiled.text isMobileNumber]) {
        [MBProgressHUD Javen_showError:@"请输入正确的号码" onView:kWindow delay:0.5 complete:nil];
        return;
    }
    
    if (self.passwordTextFiled.text.length < 6 || self.passwordTextFiled.text.length > 12) {
        [MBProgressHUD Javen_showError:@"密码长度6~12位" onView:kWindow delay:0.5 complete:nil];
        return;
    }
    
    
    NSDictionary *parameters = @{@"domain":@"0001",
                                 @"invitationCode":@"",
                                 @"smsCode":self.codeTextField.text,
                                 @"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:kWindow 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
