//
//  PartnerCenterViewController.m
//  Open
//
//  Created by 雷俊博 on 2020/4/27.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "PartnerCenterViewController.h"
#import "UIView+frameAdjust.h"

#import "PartnerCenterViewController.h"
#import "PartnerCenterCell.h"
#import "PartnerCenterBottomCell.h"


@interface PartnerCenterViewController ()

@property (nonatomic, strong)UIScrollView* scrollview;

@property (nonatomic, strong)UIView *topBackView;

@property (nonatomic, strong)UIView *profitWhiteCardView;

@property (nonatomic, strong)UIView * clearView;


@property (nonatomic, strong)UIView* partnerView;

@property (nonatomic, strong)UILabel* lb_WithDrawableProfit;

@property (nonatomic, strong)UILabel* lb_lastdDayEstimateProfit;

@property (nonatomic, strong)PartnerCenterCell* allProfitView;

@property (nonatomic, strong)PartnerCenterCell* lastMonthProfitView;

@property (nonatomic, strong)PartnerCenterCell* currentMonthProfitView;

@property (nonatomic, strong)UIButton* cashBtn;

@property (nonatomic, strong)UIButton* InsuranceBtn;

@property (nonatomic, strong)PartnerCenterBottomCell* profitTableView;

@property (nonatomic, strong)PartnerCenterBottomCell* myPartnersView;

@property (nonatomic, strong)PartnerCenterBottomCell* partnerApplyView;



@end

@implementation PartnerCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor  = [UIColor colorWithHex:0xf4f4f4];
    [self configUI];
}
- (void)configUI
{
   [self.view addSubview:self.scrollview];
    
    [_scrollview addSubview:self.topBackView];
    
    [_scrollview addSubview:self.profitWhiteCardView];
    [_scrollview addSubview:self.clearView];
    [_scrollview addSubview:self.partnerView];
    
    _scrollview.contentSize = CGSizeMake(ScreenWidth, self.partnerView.maxY + 40);
}

#pragma mark - 车险api请求
- (NSDictionary *)aiDaTokenRequestPara
{
    NSDictionary* params = @{
        @"loginId":[NSNumber numberWithInteger:[getPeopleID PeopleID]].stringValue,
        @"telephone":[getPeopleID PeopleIphoneNmber]
    };
    return params;
}

- (void)aiDaTokenRequest:(void (^)(bool success, NSString*token))response
{
    NSString*url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipAdDaConfig],AiDaTokenRequest];
    NSLog(@"小马云保token的url:%@",url);
    
    NSLog(@"params:%@",[self aiDaTokenRequestPara]);
    
    [NetworkRequest Get3:url Params:[self aiDaTokenRequestPara] Success:^(id _Nonnull responseObject) {
        
        NSLog(@"responseObject:%@",responseObject);
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSString*state = [NSString stringWithFormat:@"%@",responseObject[@"state"]];
            NSString*describe = [NSString stringWithFormat:@"%@",responseObject[@"describe"]];
            if ([state isEqualToString:@"200"]) {
                
                NSString*token = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
                
                
                response(YES,token);
                
            }else{
                [SVProgressHUD showErrorWithStatus:describe];
            }
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"服务器异常"];
        }
        
    } Failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络异常"];
    }];
}

- (NSDictionary *)aidaUrlRequestPara
{
    NSDictionary* params = @{
        @"name":[getPeopleID Peopletitle],
        @"userid":[NSNumber numberWithInteger:[getPeopleID PeopleID]].stringValue
    };
    return params;
}

- (void)aidaUrlRequestWithToken:(NSString *)token
{
    NSString*url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipAdDaUrlConfig],MYUrlRequest];
    NSLog(@"小马云保url的url:%@",url);
    
    NSLog(@"params:%@",[self aidaUrlRequestPara]);
    
    [NetworkRequest Get2:url Params:[self aidaUrlRequestPara] Token:token Success:^(id  _Nonnull responseObject) {
        
         NSLog(@"responseObject:%@",responseObject);
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSString*state = [NSString stringWithFormat:@"%@",responseObject[@"state"]];
            NSString*describe = [NSString stringWithFormat:@"%@",responseObject[@"describe"]];
            if ([state isEqualToString:@"200"]) {
                
                NSString*url = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
               url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

                
                if ([self.navigationController.visibleViewController isKindOfClass:[PartnerCenterViewController class]]) {
                    
                    //[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];

                    Web2ViewController * vc = [Web2ViewController new];
                    vc.title= @"车险";
                    vc.skipUrl = [NSURL URLWithString:url];
                   // vc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                
              
                
                
            }else{
                [SVProgressHUD showErrorWithStatus:describe];
            }
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"服务器异常"];
        }
        
    } Failure:^(NSError * _Nonnull error) {
        
        NSLog(@"error:%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络异常"];
        
    }];
}


#pragma mark - event response
- (void)btnClick:(UIButton *)sender
{
    if (sender == self.cashBtn) {
        NSString*urlStr = [NSString stringWithFormat:@"http://nginx.weface.com.cn/appH5/loan/index.html?partnerName=%@&partnerTel=%@&address=%@",[EnercryTool desA:self.model.name],[EnercryTool decryptUseDES:self.model.tel key:DESKEY],self.model.address];
       urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        WebVC * vc = [[WebVC alloc]init];
        vc.title= @"现金借贷";
        vc.url = urlStr;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (sender == self.InsuranceBtn){
        [self aiDaTokenRequest:^(bool success, NSString *token) {
           
            if (success) {
                [self aidaUrlRequestWithToken:token];
            }
            
        }];
    }
}



#pragma mark - getters and setters
- (UIScrollView *)scrollview
{
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, SCREEN_WIDTH, ScreenHeight - cl_kNavigationBarHeight)];
        _scrollview.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
        _scrollview.contentSize = CGSizeMake(ScreenWidth, 0);
        _scrollview.showsHorizontalScrollIndicator = NO;
        _scrollview.showsVerticalScrollIndicator = NO;
        
        
    }
    return _scrollview;
}

- (UIView *)topBackView
{
    if (!_topBackView) {
        
        _topBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150 * WIDTHRADIU)];

        
        _topBackView.backgroundColor = K_MainColor;
        
        //title
        UILabel* lb_title = [[UILabel alloc]init];
        lb_title.text = @"可提现收益(元)";
        lb_title.textColor = [UIColor whiteColor];
        lb_title.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        
        //按钮
       UIButton*DrawProfitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       [DrawProfitBtn setTitle:@"收益提现" forState:UIControlStateNormal];
       [DrawProfitBtn setTitleColor:K_MainColor forState:UIControlStateNormal];
       [DrawProfitBtn setImage:[UIImage imageNamed:@"hehuoren_bluearrow"] forState:UIControlStateNormal];
       DrawProfitBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
       DrawProfitBtn.backgroundColor = [UIColor whiteColor];
       DrawProfitBtn.layer.cornerRadius = 16 * WIDTHRADIU;
        DrawProfitBtn.hidden = YES;
        
       [DrawProfitBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleRight imageTitleSpace:3];
        
        
        [_topBackView addSubview:lb_title];
        [_topBackView addSubview:self.lb_WithDrawableProfit];
        [_topBackView addSubview:self.lb_lastdDayEstimateProfit];
        [_topBackView addSubview:DrawProfitBtn];
        
        
       
        
        
        
        [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(20 * WIDTHRADIU);
            make.top.offset(11 * WIDTHRADIU);
        }];
        
        [DrawProfitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self->_topBackView.mas_centerY).offset(- 10 * WIDTHRADIU);
            make.right.offset(-10 * WIDTHRADIU);
            make.width.offset(100 * WIDTHRADIU);
            make.height.offset(32 * WIDTHRADIU);
        }];
        
        [_lb_WithDrawableProfit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lb_title.mas_bottom).offset(14 * WIDTHRADIU);
            make.left.equalTo(lb_title);
        }];
        
        [_lb_lastdDayEstimateProfit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_lb_WithDrawableProfit.mas_bottom).offset(3 * WIDTHRADIU);
            make.left.equalTo(lb_title);
        }];
        
       
    }
    return _topBackView;
}

- (UIView *)profitWhiteCardView
{
    if (!_profitWhiteCardView) {
        _profitWhiteCardView = [[UIView alloc]initWithFrame:CGRectMake(10 * WIDTHRADIU, 112 * WIDTHRADIU, ScreenWidth - 20 * WIDTHRADIU, 83 * WIDTHRADIU)];
        _profitWhiteCardView.backgroundColor = [UIColor whiteColor];
        _profitWhiteCardView.layer.cornerRadius = 10 * WIDTHRADIU;
        _profitWhiteCardView.layer.masksToBounds = YES;
        
        [_profitWhiteCardView addSubview:self.allProfitView];
        [_profitWhiteCardView addSubview:self.lastMonthProfitView];
        [_profitWhiteCardView addSubview:self.currentMonthProfitView];
        
        CGFloat width = (ScreenWidth - 20 * WIDTHRADIU)/3.0;
        CGFloat height = 83 * WIDTHRADIU;
        _allProfitView.frame = CGRectMake(0, 0, width, height);
        _lastMonthProfitView.frame = CGRectMake(width, 0, width, height);
        _currentMonthProfitView.frame = CGRectMake(width * 2, 0, width, height);
    }
    return _profitWhiteCardView;
}

- (UIView *)clearView
{
    if (!_clearView) {
        _clearView = [[UIView alloc]initWithFrame:CGRectMake(0, 213 * WIDTHRADIU, ScreenWidth, 95 * WIDTHRADIU)];
        self.cashBtn.frame = CGRectMake(10 * WIDTHRADIU, 0, 170 * WIDTHRADIU, 95 * WIDTHRADIU);
        self.InsuranceBtn.frame = CGRectMake(ScreenWidth - 180 * WIDTHRADIU, 0, 170 * WIDTHRADIU, 95 * WIDTHRADIU);
        [_clearView addSubview:self.cashBtn];
        [_clearView addSubview:self.InsuranceBtn];
    }
    return _clearView;
}

- (UIView *)partnerView
{
    if (!_partnerView) {
        _partnerView = [[UIView alloc]initWithFrame:CGRectMake(10 * WIDTHRADIU, 323 * WIDTHRADIU, ScreenWidth - 20 * WIDTHRADIU, 135 * WIDTHRADIU)];
        _partnerView.backgroundColor = [UIColor whiteColor];
        _partnerView.layer.cornerRadius = 10 * WIDTHRADIU;
        _partnerView.layer.masksToBounds = YES;
        _partnerView.hidden = YES;
        
        UIView* topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _partnerView.width, 45 * WIDTHRADIU)];
        
          [_partnerView addSubview:topView];
        //title
        UILabel*titleLabel = [[UILabel alloc]init];
        titleLabel.text = @"我的合伙人";
        titleLabel.textColor = [UIColor colorWithHex:0x4d4d4d];
        titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        [topView addSubview:titleLabel];
        
        //button
        UIButton *delegateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [delegateBtn setTitle:@"发展代理" forState:UIControlStateNormal];
        [delegateBtn setTitleColor:[UIColor colorWithHex:0x1887d3] forState:UIControlStateNormal
         ];
        delegateBtn.titleLabel.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
        delegateBtn.layer.cornerRadius = 15 * WIDTHRADIU;
        delegateBtn.layer.borderColor = [UIColor colorWithHex:0x1887d3].CGColor;
        delegateBtn.layer.borderWidth = 0.5;
        [topView addSubview:delegateBtn];
        
        //line
        UIView* lineView = [[UIView alloc]init];
        lineView.backgroundColor = [UIColor colorWithHex:0xd9d9d9];
        [topView addSubview:lineView];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15 * WIDTHRADIU);
            make.centerY.equalTo(topView);
        }];
        
        [delegateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(topView);
            make.right.offset(- 15 * WIDTHRADIU);
            make.width.offset(80 * WIDTHRADIU);
            make.height.offset(30 * WIDTHRADIU);
        }];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10 * WIDTHRADIU);
            make.right.offset(-10 * WIDTHRADIU);
            make.bottom.offset(- 0.5);
            make.height.offset(0.5);
        }];
        [_partnerView addSubview:self.profitTableView];
        [_partnerView addSubview:self.myPartnersView];
        [_partnerView addSubview:self.partnerApplyView];
        
        CGFloat width = (ScreenWidth - 20 * WIDTHRADIU)/3.0;
        CGFloat height = 90 * WIDTHRADIU;
        _profitTableView.frame = CGRectMake(0, topView.maxY, width, height);
        _myPartnersView.frame = CGRectMake(width, topView.maxY, width, height);
        _partnerApplyView.frame = CGRectMake(width * 2, topView.maxY, width, height);
    }
    return _partnerView;
}




- (UILabel *)lb_WithDrawableProfit
{
    if (!_lb_WithDrawableProfit) {
        _lb_WithDrawableProfit = [[UILabel alloc]init];
        _lb_WithDrawableProfit.text = @"0.0";
        _lb_WithDrawableProfit.textColor = [UIColor whiteColor];
        _lb_WithDrawableProfit.font = [UIFont boldSystemFontOfSize:31 * WIDTHRADIU];
    }
    return _lb_WithDrawableProfit;
}

- (UILabel *)lb_lastdDayEstimateProfit
{
    if (!_lb_lastdDayEstimateProfit) {
        _lb_lastdDayEstimateProfit = [[UILabel alloc]init];
        _lb_lastdDayEstimateProfit.text = @"昨日预估(元)：+20.0";
        _lb_lastdDayEstimateProfit.textColor = [UIColor whiteColor];
        _lb_lastdDayEstimateProfit.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
        _lb_lastdDayEstimateProfit.hidden = YES;
    }
    return _lb_lastdDayEstimateProfit;
}

- (PartnerCenterCell *)allProfitView
{
    if (!_allProfitView) {
        _allProfitView = [[NSBundle mainBundle]loadNibNamed:@"PartnerCenterCell" owner:nil options:nil].lastObject;
        _allProfitView.lb_number.text = @"0.0";
        _allProfitView.lb_title.text = @"累计总收益(元)";
    }
    return _allProfitView;
}

- (PartnerCenterCell *)lastMonthProfitView
{
    if (!_lastMonthProfitView) {
        _lastMonthProfitView = [[NSBundle mainBundle]loadNibNamed:@"PartnerCenterCell" owner:nil options:nil].lastObject;
        _lastMonthProfitView.lb_number.text = @"0.0";
        _lastMonthProfitView.lb_title.text = @"上月收益(元)";
    }
    return _lastMonthProfitView;
}

- (PartnerCenterCell *)currentMonthProfitView
{
    if (!_currentMonthProfitView) {
        _currentMonthProfitView = [[NSBundle mainBundle]loadNibNamed:@"PartnerCenterCell" owner:nil options:nil].lastObject;
        _currentMonthProfitView.lineView.hidden = YES;
        _currentMonthProfitView.lb_number.text = @"0.0";
        _currentMonthProfitView.lb_title.text = @"本月预估(元)";
    }
    return _currentMonthProfitView;
}

- (UIButton *)cashBtn
{
    if (!_cashBtn) {
        _cashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cashBtn setBackgroundImage:[UIImage imageNamed:@"hehuoren_cash"] forState:UIControlStateNormal];
        [_cashBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cashBtn;
}

- (UIButton *)InsuranceBtn
{
    if (!_InsuranceBtn) {
        _InsuranceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_InsuranceBtn setBackgroundImage:[UIImage imageNamed:@"hehuoren_insurance"] forState:UIControlStateNormal];
        [_InsuranceBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _InsuranceBtn;
}

- (PartnerCenterBottomCell *)profitTableView
{
    if (!_profitTableView) {
        _profitTableView = [[NSBundle mainBundle]loadNibNamed:@"PartnerCenterBottomCell" owner:nil options:nil].lastObject;
    }
    return _profitTableView;
}

- (PartnerCenterBottomCell *)myPartnersView
{
    if (!_myPartnersView) {
        _myPartnersView = [[NSBundle mainBundle]loadNibNamed:@"PartnerCenterBottomCell" owner:nil options:nil].lastObject;
    }
    return _myPartnersView;
}

- (PartnerCenterBottomCell *)partnerApplyView
{
    if (!_partnerApplyView) {
        _partnerApplyView = [[NSBundle mainBundle]loadNibNamed:@"PartnerCenterBottomCell" owner:nil options:nil].lastObject;
        _partnerApplyView.lineView.hidden = YES;
    }
    return _partnerApplyView;
}


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