//
//  LoginViewController.m
//  Macalline
//
//  Created by JianjiYuan on 14-12-1.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "LoginViewController.h"
#import "TabBarViewController.h"
#import "SATabBarViewController.h"
#import "BATabBarViewController.h"
#import "BRCommonPopup.h"
#import "GexinPushMgr.h"
#import <NSString+MD5.h>

@interface UserListCell : BaseTableViewCell

@property (nonatomic, retain) UILabel *lblUserName;
@property (nonatomic, retain) UIImageView *imageVContent;
@property (nonatomic, retain) UIButton *btnDelete;

@end

@interface LoginViewController () < UITableViewDelegate, UITableViewDataSource, BRCommonPopupDelegate, BRNetworkMgrDelegate >
{
    UIImageView *imageVBg;
}

@property (nonatomic, strong) UIImageView *imageVLogo;
@property (nonatomic, strong) UIImageView *imageVUserName;
@property (nonatomic, strong) UIImageView *imageVPassword;
@property (nonatomic, strong) UIButton *btnSelectUser;

@property (nonatomic, strong) UILabel *lblUserName;
@property (nonatomic, strong) UITextField *textFUserName;
@property (nonatomic, strong) UILabel *lblPassword;
@property (nonatomic, strong) UITextField *textFPassword;
@property (nonatomic, strong) UIButton *btnLogin;

@property (nonatomic, strong) UITableView *tableVUsers;
@property (nonatomic, strong) NSMutableArray *arrUsers;
@property (nonatomic, retain) BRCommonPopup *commonPopup;

@end

@implementation LoginViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    imageVBg = [UIImageView newAutoLayoutView];
    imageVBg.image = [UIImage imageNamed:@"Login/mainbg_login.png"];
    [self.view addSubview:imageVBg];
    
    [self setViewLayoutConstraint];
    
    [self registObserverOfTextInputView];    
    
    NSDictionary *dict = [UIApplication getObjectType:Dictionary key:kDictAutoLoginInfo];
    if ([dict valueForKey:kUserName]) {
        self.textFUserName.text = [dict valueForKey:kUserName];
    }
    if ([dict valueForKey:kPassword]) {
        self.textFPassword.text = [dict valueForKey:kPassword];
    }
}
-(void)blendID
{
    [self bindClientId];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)setViewLayoutConstraint
{
    [imageVBg autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    [self.imageVLogo autoPinToTopLayoutGuideOfViewController:self withInset:60];
    [self.imageVLogo autoSetDimensionsToSize:CGSizeMake(138, 64.5)];
    [self.imageVLogo autoAlignAxisToSuperviewAxis:ALAxisVertical];
    
    [self.imageVUserName autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:25];
    [self.imageVUserName autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
    [self.imageVUserName autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.imageVLogo withOffset:70 relation:NSLayoutRelationLessThanOrEqual];
    [self.imageVUserName autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.imageVLogo withOffset:10 relation:NSLayoutRelationGreaterThanOrEqual];
    [self.imageVUserName autoSetDimension:ALDimensionHeight toSize:42];
    
    [self.lblUserName autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.imageVUserName withOffset:10];
    [self.lblUserName autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.imageVUserName];
    [self.lblUserName autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.imageVUserName];
    [self.lblUserName autoSetDimension:ALDimensionWidth toSize:60];
    
    [self.textFUserName autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblUserName];
    [self.textFUserName autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.imageVUserName];
    [self.textFUserName autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.imageVUserName];
    [self.textFUserName autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:70];
    
    [self.btnSelectUser autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.textFUserName];
    [self.btnSelectUser autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.textFUserName withOffset:0];
    [self.btnSelectUser autoSetDimensionsToSize:CGSizeMake(40, 40)];
    
    [self.imageVPassword autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:25];
    [self.imageVPassword autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
    [self.imageVPassword autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.textFUserName withOffset:15];
    [self.imageVUserName autoSetDimension:ALDimensionHeight toSize:42];
    
    [self.lblPassword autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.imageVPassword withOffset:10];
    [self.lblPassword autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.imageVPassword];
    [self.lblPassword autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.imageVPassword];
    [self.lblPassword autoSetDimension:ALDimensionWidth toSize:60];
    
    [self.textFPassword autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblPassword];
    [self.textFPassword autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.imageVPassword];
    [self.textFPassword autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.imageVPassword];
    [self.textFPassword autoSetDimension:ALDimensionHeight toSize:42];
    [self.textFPassword autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
    
    [self.btnLogin autoSetDimension:ALDimensionHeight toSize:40];
    [self.btnLogin autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
    [self.btnLogin autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:25];
    [self.btnLogin autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.textFPassword withOffset:40 relation:NSLayoutRelationLessThanOrEqual];
    [self.btnLogin autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.textFPassword withOffset:20 relation:NSLayoutRelationGreaterThanOrEqual];
}

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

- (BOOL)checkInputValid
{
    if (_textFUserName.text.length == 0) {
        [UIApplication showToastViewMessage:@"请输入用户名"];
        return NO;
    }else if (_textFPassword.text.length == 0) {
        [UIApplication showToastViewMessage:@"请输入密码"];
        return NO;
    }
    [self.view endEditing:YES];
    return YES;
}

- (void)btnLoginClick:(id)sender
{
    if ([self checkInputValid]) {
        
        [UIApplication showLoadingView:@"正在登录..请稍后"];
        
        NSString *interface = @"loginEX";
        NSDictionary *dicParam = @{@"username":_textFUserName.text, @"password":[[NSString stringWithFormat:@"%@%@",_textFUserName.text,_textFPassword.text]  MD5Digest]};
//        NSDictionary *dicParam = @{@"username":@"bigAreaManager", @"password":[[NSString stringWithFormat:@"%@%@",@"zongbuceshi",@"111111"]  MD5Digest]};
        
        DLog(@"%@", [[NSString stringWithFormat:@"%@%@",_textFUserName.text,_textFPassword.text]  MD5Digest]);
        AFHTTPRequestOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                                    interface:interface
                                                                       params:dicParam
                                                                     dataType:BRNetMgrDataTypeJSON
                                                                       method:BRNetMgrMethodPost];
        [self.networkMgr startOperation:operation];
    }
    
}

- (void)bindClientId
{
    NSString *interface = @"bindClientIdEX";
    NSDictionary *dicParam = @{@"platForm":@"2", @"clientId":[UIApplication getObjectType:String key:kClientIdOfApplication]};
    AFHTTPRequestOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                                interface:interface
                                                                   params:dicParam
                                                                 dataType:BRNetMgrDataTypeJSON
                                                                   method:BRNetMgrMethodPost];
    
    [operation setUserInfo:@{@"interface":interface, kDealExtraWhenErrorOcur:@YES}];
    [self.networkMgr startOperation:operation];
}

- (void)btnSelectUserClick:(id)sender
{
    self.arrUsers = [NSMutableArray arrayWithArray:[UIApplication getObjectType:Array key:kHistoryUserList]];
    
    self.commonPopup = [[BRCommonPopup alloc] initWithFrame:CGRectZero];
    
    [self.view addSubview:_commonPopup];
    
    CGRect rect = [self.view convertRect:self.textFUserName.frame toView:_commonPopup];
    rect.origin.y = CGRectGetMaxY(rect);
    rect.size.width = self.view.bounds.size.width-50;
    rect.origin.x = 25;
    rect.size.height = 43*self.arrUsers.count>150?150:43*self.arrUsers.count;
    
    self.tableVUsers = [[UITableView alloc] initWithFrame:rect
                                                     style:UITableViewStylePlain];
    _tableVUsers.delegate = self;
    _tableVUsers.dataSource = self;
    _tableVUsers.backgroundColor = [UIColor clearColor];
    _tableVUsers.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableVUsers.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    
    _commonPopup.viewContainer = _tableVUsers;
    _commonPopup.popupArrow = BRCommonPopupArrowRight;
    _commonPopup.isDropdown = YES;
    _commonPopup.backgroundColor = [UIColor clearColor];
    _commonPopup.delegate = self;
    [_tableVUsers reloadData];
    [_commonPopup showWithAnimated:YES];
    
   
   
    [UIView animateWithDuration:0.3 animations:^{
        self.imageVPassword.transform = CGAffineTransformMakeTranslation(0, rect.size.height);
        self.lblPassword.transform = CGAffineTransformMakeTranslation(0, rect.size.height);
        self.textFPassword.transform = CGAffineTransformMakeTranslation(0, rect.size.height);
        self.btnLogin.transform = CGAffineTransformMakeTranslation(0, rect.size.height);
        self.btnSelectUser.selected = YES;
        
        self.btnSelectUser.transform = CGAffineTransformMakeRotation(180.0/180*M_PI);
        
    }];
    
    

}

#pragma mark =============  BRCommonPopup Delegate  =============

- (void)commonPopupDidDismiss:(BRCommonPopup *)owner
{
    [UIView animateWithDuration:0.3 animations:^{
        self.imageVPassword.transform = CGAffineTransformIdentity;
        self.lblPassword.transform = CGAffineTransformIdentity;
        self.textFPassword.transform = CGAffineTransformIdentity;
        self.btnLogin.transform = CGAffineTransformIdentity;
        self.btnSelectUser.selected = NO;
        
        self.btnSelectUser.transform = CGAffineTransformIdentity;
    }];
}

#pragma mark =============  BRNetworkMgr Delegate  =============

- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"loginEX"]) {
        
        // [EXT] 登录成功，才接受消息
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blendID) name:@"shouldBlendClientId" object:nil];
        
        [[GexinPushMgr getInstance] applicationDidBecomeActive];
        [[GexinPushMgr getInstance] registerRemoteNotification];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shouldBlendClientId" object:nil];
//        [self bindClientId];
        
        //保存历史登录用户
        NSArray *arrayUsers = [UIApplication getObjectType:Array key:kHistoryUserList];
        if (![arrayUsers containsObject:_textFUserName.text]) {
            NSMutableArray *arrNewUser = [NSMutableArray arrayWithArray:arrayUsers];
            [arrNewUser addObject:_textFUserName.text];
            [UIApplication saveObject:arrNewUser type:Array key:kHistoryUserList];
        }
        
        //保存登录信息
        //    [[NSString stringWithFormat:@"%@%@",_textFUserName.text,_textFPassword.text]
        NSDictionary *dictAutoLogin = @{kUserName:_textFUserName.text,
                                        kPasswordMD5:[[NSString stringWithFormat:@"%@%@",_textFUserName.text,_textFPassword.text]  MD5Digest],
                                        kPassword:_textFPassword.text,
                                        kAutoLogin: @YES};
        [UIApplication saveObject:dictAutoLogin type:Dictionary key:kDictAutoLoginInfo];
        
        if (self.isPresented) {
            [UIApplication saveObject:@YES type:Number key:kShouldRequestDataWhenBack];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [self setCustomNavBar];
            [DataMgr getInstance].userID = result[@"userId"];
            if ([result[@"roleCode"] isEqualToString:kDaqufuzerencode]) {
                [DataMgr getInstance].roleId = @"daqufuzeren";
                [DataMgr getInstance].bigAreaId = result[@"bigAreaId"];
                [DataMgr getInstance].bigAreaName = result[@"areaName"];
                
                BATabBarViewController*vcTabBar = [[BATabBarViewController alloc] initWithNibName:nil bundle:nil];
                [self.navigationController pushViewController:vcTabBar animated:YES];
            }else if([result[@"roleCode"] isEqualToString:kXiaoqufuzerencode])
            {
                [DataMgr getInstance].roleId = @"xiaoqufuzeren";
                [DataMgr getInstance].smallAreaId = result[@"smallAreaId"];
                [DataMgr getInstance].smallAreaName = result[@"smallAreaName"];
                SATabBarViewController*vcTabBar = [[SATabBarViewController alloc] initWithNibName:nil bundle:nil];
                [self.navigationController pushViewController:vcTabBar animated:YES];
            }
            else
            {
                [DataMgr getInstance].roleId = @"guanliyuan";
                [DataMgr getInstance].shopName = result[@"shopName"];
                TabBarViewController *vcTabBar = [[TabBarViewController alloc] initWithNibName:nil bundle:nil];
                [self.navigationController pushViewController:vcTabBar animated:YES];
            }
//            TabBarViewController *vcTabBar = [[TabBarViewController alloc] initWithNibName:nil bundle:nil];
//            SATabBarViewController * vcTabBar = [[SATabBarViewController alloc] initWithNibName:nil bundle:nil];
//            BATabBarViewController*vcTabBar = [[BATabBarViewController alloc] initWithNibName:nil bundle:nil];
//            [self.navigationController pushViewController:vcTabBar animated:YES];
        }
        

        
    }else if ([operation.userInfo[@"interface"] isEqualToString:@"bindClientIdEX"]) {
        if (![result[@"resultCode"] isEqualToString:@"01"]) {
            [self performSelector:@selector(bindClientId) withObject:nil afterDelay:3*60];
        }
    }

}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"bindClientIdEX"]) {
        [self performSelector:@selector(bindClientId) withObject:nil afterDelay:3*60];
    }
}

#pragma mark =============  UITableView DataSource  =============

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentity = @"UserListCell";
    UserListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (cell == nil) {
        cell = [[UserListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }

    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.lblUserName.text = self.arrUsers[indexPath.row];
    [cell.btnDelete setTag:indexPath.row];
    [cell.btnDelete addTarget:self action:@selector(btnDeleteClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 43;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrUsers.count;
}

#pragma mark =============  UITableView Delegate  =============

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.textFUserName.text = self.arrUsers[indexPath.row];
    self.textFPassword.text = @"";
    [self.commonPopup hideWithAnimated:YES];
}

- (void)btnDeleteClick:(UIButton *)sender
{
   
    [self.arrUsers removeObjectAtIndex:sender.tag];
    [UIApplication saveObject:self.arrUsers type:Array key:kHistoryUserList];
    
    CGRect rect = self.tableVUsers.frame;
    rect.size.height = 43*self.arrUsers.count>150?150:43*self.arrUsers.count;
    self.tableVUsers.frame = rect;
    [_tableVUsers reloadData];
}


#pragma mark =============  UITextField Delegate  =============

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:_textFUserName]) {
        [_textFPassword becomeFirstResponder];
        return NO;
    }
    [textField resignFirstResponder];
    [self btnLoginClick:nil];
    return YES;
}

#pragma mark =============  LazyLoad Properties  =============

- (UIImageView *)imageVLogo
{
    if (!_imageVLogo) {
        self.imageVLogo = [UIImageView newAutoLayoutView];
        //_imageVLogo.image = [UIImage imageNamed:@"Login/logo_login.png"];
        _imageVLogo.autoresizingMask = UIViewAutoresizingNone;
        [self.view addSubview:_imageVLogo];
    }
    return _imageVLogo;
}

- (UIImageView *)imageVUserName
{
    if (!_imageVUserName) {
        self.imageVUserName = [UIImageView newAutoLayoutView];
        _imageVUserName.image = [UIImage imageNamed:@"Login/textfield_login.png"];
        _imageVUserName.autoresizingMask = UIViewAutoresizingNone;
        [self.view addSubview:_imageVUserName];
    }
    return _imageVUserName;
}

- (UIImageView *)imageVPassword
{
    if (!_imageVPassword) {
        self.imageVPassword = [UIImageView newAutoLayoutView];
        _imageVPassword.image = [UIImage imageNamed:@"Login/textfield_login.png"];
        _imageVPassword.autoresizingMask = UIViewAutoresizingNone;
        [self.view addSubview:_imageVPassword];
    }
    return _imageVPassword;
}

- (UIButton *)btnSelectUser
{
    if (!_btnSelectUser) {
        self.btnSelectUser = [UIButton newAutoLayoutView];
        [_btnSelectUser setImage:[UIImage imageNamed:@"Login/arrow_login.png"] forState:UIControlStateNormal];
        [_btnSelectUser addTarget:self action:@selector(btnSelectUserClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btnSelectUser];
    }
    return _btnSelectUser;
}

- (UITextField *)textFUserName
{
    if (!_textFUserName) {
        self.textFUserName = [UITextField newAutoLayoutView];
        _textFUserName.backgroundColor = [UIColor clearColor];
        _textFUserName.textColor = [UIColor colorWithRed:0.2471 green:0.7137 blue:0.9373 alpha:1];
        
        _textFUserName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _textFUserName.textAlignment = NSTextAlignmentLeft;
        _textFUserName.returnKeyType = UIReturnKeyNext;
        [self.view addSubview:_textFUserName];
    }
    return _textFUserName;
}

- (UITextField *)textFPassword
{
    if (!_textFPassword) {
        self.textFPassword = [UITextField newAutoLayoutView];
        _textFPassword.backgroundColor = [UIColor clearColor];
        _textFPassword.textColor = [UIColor colorWithRed:0.2471 green:0.7137 blue:0.9373 alpha:1];
        _textFPassword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _textFPassword.returnKeyType = UIReturnKeyDone;
        _textFPassword.secureTextEntry = YES;
        [self.view addSubview:_textFPassword];
    }
    return _textFPassword;
}

- (UILabel *)lblUserName
{
    if (!_lblUserName) {
        self.lblUserName = [UILabel newAutoLayoutView];
        _lblUserName.text = @"用户名";
        _lblUserName.textColor = [UIColor darkGrayColor];
        _lblUserName.backgroundColor = [UIColor clearColor];
        _lblUserName.textAlignment = NSTextAlignmentLeft;
        _lblUserName.font = [UIFont systemFontOfSize:16];
        [self.view addSubview:_lblUserName];
    }
    return _lblUserName;
}

- (UILabel *)lblPassword
{
    if (!_lblPassword) {
        self.lblPassword = [UILabel newAutoLayoutView];
        _lblPassword.text = @"密    码";
        _lblPassword.textColor = [UIColor darkGrayColor];
        _lblPassword.backgroundColor = [UIColor clearColor];
        _lblPassword.textAlignment = NSTextAlignmentLeft;
        _lblPassword.font = [UIFont systemFontOfSize:16];
        [self.view addSubview:_lblPassword];
    }
    return _lblPassword;
}

- (UIButton *)btnLogin
{
    if (!_btnLogin) {
        self.btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLogin.translatesAutoresizingMaskIntoConstraints = NO;
        _btnLogin.backgroundColor = [UIColor clearColor];
        [_btnLogin setTitle:@"登   录" forState:UIControlStateNormal];
        [_btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnLogin setBackgroundImage:[UIImage imageNamed:@"Login/btn_login.png"] forState:UIControlStateNormal];
        [_btnLogin addTarget:self action:@selector(btnLoginClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btnLogin];
    }
    return _btnLogin;
}
- (void)setCustomNavBar
{
#if kIOSVersion7Later
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
#endif
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           [UIFont boldSystemFontOfSize:19], NSFontAttributeName, nil]];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >=7.0) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar/nav_bar_128.png"]
                                           forBarMetrics:UIBarMetricsDefault];
    }else{
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar/nav_bar_88.png"]
                                           forBarMetrics:UIBarMetricsDefault];
    }
    
}
@end

@implementation UserListCell

- (void)dealloc
{
    self.imageVContent = nil;
    self.lblUserName = nil;
    self.btnDelete = nil;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
//        textfield_login@2x
//        UIImageView* bgImage = [[UIImageView alloc] init];
//        bgImage.image = [UIImage imageNamed:@"Login/textfield_login.png"];
//        [self.contentView addSubview:bgImage];
//        [bgImage autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(-4, -5, 0, -5)];
        
        UIImageView* headImage = [[UIImageView alloc] init];
        headImage.image = [UIImage imageNamed:@"Login/login_headImage.png"];
        [self.contentView addSubview:headImage];
        [headImage autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(5, 10, 5, 0) excludingEdge:ALEdgeRight];
        [headImage autoSetDimension:ALDimensionWidth toSize:33];
        
        [self.lblUserName autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:headImage withOffset:8];
        [self.lblUserName autoPinEdgeToSuperviewEdge:ALEdgeTop];
        [self.lblUserName autoPinEdgeToSuperviewEdge:ALEdgeBottom];
        
        [self.btnDelete autoSetDimensionsToSize:CGSizeMake(17, 17)];
        [self.btnDelete autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.lblUserName withOffset:8];
        [self.btnDelete autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.lblUserName];
        [self.btnDelete autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
        
        UIView *viewLine = [UIView newAutoLayoutView];
        viewLine.backgroundColor = RGBColor(0, 120, 193, 1);
        [self.contentView addSubview:viewLine];
        
        [viewLine autoSetDimension:ALDimensionHeight toSize:0.5];
        [viewLine autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        self.contentView.backgroundColor = RGBColor(240, 247, 155, 1);
    }else {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (selected) {
        self.contentView.backgroundColor = RGBColor(240, 247, 155, 1);
    }else {
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
}
#pragma mark =============  LazyLoad Properties  =============

- (UILabel *)lblUserName
{
    if (!_lblUserName) {
        self.lblUserName = [UILabel newAutoLayoutView];
        _lblUserName.font = [UIFont boldSystemFontOfSize:15];
        _lblUserName.textAlignment = NSTextAlignmentLeft;
        _lblUserName.backgroundColor = [UIColor clearColor];
        _lblUserName.textColor = [UIColor blackColor];
        [self.contentView addSubview:_lblUserName];
    }
    return _lblUserName;
}

- (UIButton *)btnDelete
{
    if (!_btnDelete) {
        self.btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnDelete.translatesAutoresizingMaskIntoConstraints = NO;
        _btnDelete.backgroundColor = [UIColor clearColor];
        [_btnDelete setBackgroundImage:[UIImage imageNamed:@"Login/btn_close_login.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_btnDelete];
    }
    return _btnDelete;
}


@end
