//
//  LoginViewController.m
//  RealEstateManagement
//
//  Created by Z on 16/6/24.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "LoginViewController.h"
#import "IQUITextFieldView+Additions.h"
#import "LoginViewModel.h"
#import "RootNavigationController.h"
@interface LoginViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFieldName;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPWD;
@property (weak, nonatomic) IBOutlet UIButton *btnLogin;
@property (weak, nonatomic) IBOutlet UIButton *btnShop;
@property (weak, nonatomic) IBOutlet UIButton *btnMall;
@property (weak, nonatomic) IBOutlet UIImageView *imgBg;
@property (weak, nonatomic) IBOutlet UILabel *bottomInfo;
@property (strong, nonatomic) LoginViewModel *viewModel;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkUpdate];
    if (AppGlobal.isMall) {
        self.btnMall.selected = YES;
    } else {
        self.btnShop.selected = YES;
    }
    self.textFieldName.keyboardDistanceFromTextField = 60;
    self.textFieldPWD.keyboardDistanceFromTextField = 60;
    self.bottomInfo.text = [NSString stringWithFormat:@"上海海鼎信息工程股份有限公司  V%@", APP_VERSION];
    self.textFieldName.text = AppGlobal.savedName;
    self.textFieldPWD.text = AppGlobal.savedPWD;
    [self configUIWithTargetType];
}

/**
 根据target的类型配置不同的界面样式
 */
- (void)configUIWithTargetType {
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)checkUpdate {
//    WS(weakSelf);
//    MBProgressHUD *hud = [MBProgressHUD j_loadingOnView:self.view text:@"检查版本更新中…"];
}

//登录
- (IBAction)ActionLogin:(UIButton *)sender {
    [MBProgressHUD j_loading:@"登陆中…"];
    self.viewModel.loginRequest.principal = self.textFieldName.text;
    self.viewModel.loginRequest.password = self.textFieldPWD.text;
    self.viewModel.loginRequest.type = AppGlobal.isMall ? LoginTypeMall : LoginTypeTenant;
    WS(weakSelf);
    [self.viewModel login:^(id responseObject, NSError *error) {
        [MBProgressHUD j_hideLoadingView];
        AppGlobal.savedPWD = weakSelf.textFieldPWD.text;
        AppGlobal.savedName = weakSelf.textFieldName.text;
        [AppGlobal saveBaseInfo];
    }];
}

//tag 0 商户／ 1 内部
- (IBAction)actionSwitch:(UIButton *)sender {
    AppGlobal.isMall = sender.tag == 1 ? YES : NO;
}

- (void)loginSuccessAction {
   
}

ZJLazy(LoginViewModel, viewModel);

@end
