//
//  HoldingDetailController.m
//  Open
//
//  Created by 雷俊博 on 2020/8/10.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "HoldingDetailController.h"
#import "HoldingDetailTopView.h"
#import "HoldingDetailBottomView.h"
#import "UIView+frameAdjust.h"

#import "SMSVerificationController.h"
#import "ProductPurchaseController.h"

@interface HoldingDetailController ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView* scrollView;

@property (nonatomic,strong)UIView*bottomView;

@end

@implementation HoldingDetailController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:0 target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:bar];
    [GTCountSDK trackCountEvent:@"save_wudangProductHoldingDetailCount" withArgs:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.bottomView];
    
}

#pragma mark - 比财产品详情
- (void)getBiCaiDetailInfoRequest {
    NSString* url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYinShuConfig],BCGetProductInfoRequest];
    NSLog(@"比财产品详情url:%@",url);
    NSMutableDictionary *params = [NetworkPublicParams getBCPublicParams];
    
    [params setObject:[getPeopleID PeopleIphoneNmber] forKey:@"loginPhoneNum"];
    [params setObject:self.bcholdProRetModel.prdIndexId forKey:@"productId"];
    
    
    NSLog(@"params:%@",params);
    
    [KKLoadingAnimationView show];
    [NetworkRequest Post6:url Params:params Success:^(id _Nonnull objc) {
       
        NSLog(@"objc:%@",objc);
        NSString *state = [NSString stringWithFormat:@"%@",objc[@"state"]];
        NSString *describe = [NSString stringWithFormat:@"%@",objc[@"describe"]];
        if ([state isEqualToString:@"200"]) {
            SaveBCProductInfoModel* infoModel = [SaveBCProductInfoModel mj_objectWithKeyValues:objc[@"result"]];

            [self bcGetEleAccountInfoRequest:infoModel];
            return;
            
        }else{
            [Tools showMessage:describe];
        }
        
        [KKLoadingAnimationView dismiss];

    } Failure:^(NSError* error) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"error:%@",error);
        
        [Tools showMessage:@"网络出小差了,请稍后重试"];
        
    }];
}

#pragma mark - 比财 电子账户信息详情
- (void)bcGetEleAccountInfoRequest:(SaveBCProductInfoModel *)proModel {
    
    NSString* url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYinShuConfig],BCGETAccountInfoRequest];
    NSLog(@"比财电子账户详情url:%@",url);
    NSMutableDictionary *params = [NetworkPublicParams getBCPublicParams];
    
    [params setObject:[getPeopleID PeopleIphoneNmber] forKey:@"loginPhoneNum"];
    [params setObject:proModel.orgId forKey:@"orgId"];
    
    
    NSLog(@"params:%@",params);
    
    [NetworkRequest Post6:url Params:params Success:^(id _Nonnull objc) {
      
        NSLog(@"objc:%@",objc);
        NSString *state = [NSString stringWithFormat:@"%@",objc[@"state"]];
        NSString *describe = [NSString stringWithFormat:@"%@",objc[@"describe"]];
        if ([state isEqualToString:@"200"]) {
           
            BCAccountInfoModel* model = [BCAccountInfoModel mj_objectWithKeyValues:objc[@"result"]];
            
            ProductPurchaseController*purchaseVC = [ProductPurchaseController new];
            purchaseVC.title = @"产品购买";
            purchaseVC.type = SaveFromTypeBiCai;
            purchaseVC.bcInfoModel = model;
            purchaseVC.bcProInfoModel = proModel;
            [self.navigationController pushViewController:purchaseVC animated:YES];
            
        } else{
            [self alertShow:describe];
        }
        [KKLoadingAnimationView dismiss];
    } Failure:^(NSError* error) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"error:%@",error);
        
        [self alertShow:@"网络出小差了,请稍后重试"];
        
    }];
    
}

- (void)alertShow:(NSString *)msg
{
    if (@available(iOS 9.0,*)) {
        
        UIAlertController * alertV = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertV addAction:confirm];
        
        [self presentViewController:alertV animated:YES completion:nil];
        
    }else{
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertV show];
    }
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= -cl_kNavigationBarHeight) {
        scrollView.contentOffset = CGPointMake(0, -cl_kNavigationBarHeight);
    }
}

#pragma mark - public methods
-(id)getXiblWithClassName:(NSString *)className
{
    return [[NSBundle mainBundle]loadNibNamed:className owner:self options:nil].lastObject;
}

#pragma mark - 获取转换后的日期模式
- (NSString *)getNewStyleWithDateStr:(NSString *)dateString1
{
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [formatter1 setDateFormat:@"yyyyMMdd"];

    // NSDate形式的日期
    NSDate *date =[formatter1 dateFromString:dateString1];
    
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString2 = [formatter2 stringFromDate:date ];
    NSLog(@"%@",dateString2);
    
    return dateString2;
}

#pragma mark - event response
- (void)drawBtnClick:(UIButton *)sender
{
    UIAlertController* alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"提前支取按照活期利率计息" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction*cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:@"继续支取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (self.type == SaveFromTypeBiCai) {
            SMSVerificationController*vc = [SMSVerificationController new];
            vc.title = @"赎回短信验证";
            vc.type = SMSSendTypeBCProRedeem;
            vc.bcholdProRetModel = self.bcholdProRetModel;
            vc.bcAccountInfoModel = self.bcAccountInfoModel;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [GTCountSDK trackCountEvent:@"save_wudangProductSHSMSCount" withArgs:nil];
            SMSVerificationController*vc = [SMSVerificationController new];
            vc.title = @"赎回短信验证";
            vc.type = SMSSendTypeProRedeem;
            vc.buyedProModel = self.model;
            vc.detailModel = self.detailModel;
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        
    }];
    
    [alertVC addAction:cancelAction];
    [alertVC addAction:confirmAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

- (void)buyBtnClick:(UIButton *)sender
{
    if (self.type == SaveFromTypeBiCai) {
        [self getBiCaiDetailInfoRequest];
    }else{
        ProductPurchaseController*purchaseVC = [ProductPurchaseController new];
        purchaseVC.title = @"产品购买";
        purchaseVC.model = self.subModel;
        purchaseVC.infoModel = self.infoModel;
        [self.navigationController pushViewController:purchaseVC animated:YES];
        return;
    }
    
    
}


#pragma mark - getters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - TAB_BAR_HEIGHT)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
        _scrollView.delegate = self;
        
        //topView
        HoldingDetailTopView*topView = [self getXiblWithClassName:@"HoldingDetailTopView"];
        [_scrollView addSubview:topView];
       
        
        
        
        //topView.lb_name
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(ScreenWidth);
            make.height.mas_greaterThanOrEqualTo(50);
        }];
        //bottomView
        HoldingDetailBottomView*bottomView = [self getXiblWithClassName:@"HoldingDetailBottomView"];
        [_scrollView addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(topView.mas_bottom).offset(10 * WIDTHRADIU);
            make.left.mas_equalTo(0);
            make.width.mas_equalTo(ScreenWidth);
            make.height.mas_greaterThanOrEqualTo(50);
            make.bottom.mas_equalTo(0);
        }];
        
        if (self.type == SaveFromTypeBiCai) {
            
            topView.lb_name.text = [NSString stringWithFormat:@"%@ | %@",self.bcholdProRetModel.orgName,self.bcholdProRetModel.prdName];
            
            topView.lb_leijishouyiTitle.text = @"当前预估收益(元)";
            
            topView.lb_proAmount.text = [NSString stringWithFormat:@"%@",self.bcholdProRetModel.dynamicList.amount.fieldValue];
            
            if ([Tools isBlankString:self.bcholdProRetModel.profit]) {
                topView.lb_syAmount.text = @"--";
            }else{
                topView.lb_syAmount.text = self.bcholdProRetModel.profit;
            }
            
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.amount.fieldName]) {
                topView.lb_title1.text = @"本金";
            }else{
                topView.lb_title1.text = self.bcholdProRetModel.dynamicList.amount.fieldName;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.valueDate.fieldName]) {
                topView.lb_title2.text = @"起息日";
            }else{
                topView.lb_title2.text = self.bcholdProRetModel.dynamicList.valueDate.fieldName;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.endDate.fieldName]) {
                topView.lb_title3.text = @"到期日";
            }else{
                topView.lb_title3.text = self.bcholdProRetModel.dynamicList.endDate.fieldName;
            }
            
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.transDate.fieldName]) {
                topView.lb_title4.text = @"交易时间";
            }else{
                topView.lb_title4.text = self.bcholdProRetModel.dynamicList.transDate.fieldName;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.rate.fieldName]) {
                topView.lb_title5.text = @"利率";
            }else{
                topView.lb_title5.text = self.bcholdProRetModel.dynamicList.rate.fieldName;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.income.fieldName]) {
                topView.lb_title6.text = @"预期利息";
            }else{
                topView.lb_title6.text = self.bcholdProRetModel.dynamicList.income.fieldName;
            }
            
            
            
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.amount.fieldValue]) {
                topView.lb_startDate.text = @"--";
            }else{
                topView.lb_startDate.text = self.bcholdProRetModel.dynamicList.amount.fieldValue;
            }
            
            
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.valueDate.fieldValue]) {
                topView.lb_dqlx.text = @"--";
            }else{
                topView.lb_dqlx.text = self.bcholdProRetModel.dynamicList.valueDate.fieldValue;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.endDate.fieldValue]) {
                topView.lb_endDate.text = @"--";
            }else{
                topView.lb_endDate.text = self.bcholdProRetModel.dynamicList.endDate.fieldValue;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.transDate.fieldValue]) {
                topView.lb_nextDay.text = @"--";
            }else{
                topView.lb_nextDay.text = self.bcholdProRetModel.dynamicList.transDate.fieldValue;
            }
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.rate.fieldValue]) {
                topView.lb_weekAmount.text = @"--";
            }else{
                topView.lb_weekAmount.text = self.bcholdProRetModel.dynamicList.rate.fieldValue;
            }
            
            
            if ([Tools isBlankString:self.bcholdProRetModel.dynamicList.income.fieldValue]) {
                topView.lb_yflx.text = @"--";
            }else{
                topView.lb_yflx.text = self.bcholdProRetModel.dynamicList.income.fieldValue;
            }
        }else{
            NSString*name = nil;
            if ([self.detailModel.PRO_CODE isEqualToString:@"35"]) {
                name = [NSString stringWithFormat:@"%@ | 黔宝源",self.model.bank];
            }else if ([self.detailModel.PRO_CODE isEqualToString:@"36"]){
                name = [NSString stringWithFormat:@"%@ | 黔宝聚",self.model.bank];
            }
            topView.lb_name.text = name;
            
            topView.lb_proAmount.text = [NSString stringWithFormat:@"%.2f",self.detailModel.BALANCE.doubleValue];
            
            topView.lb_syAmount.text =  [NSString stringWithFormat:@"%.2f",self.detailModel.CURR_INT.doubleValue];
            
            
            if ([Tools isBlankString:self.detailModel.BEG_INT]) {
                topView.lb_startDate.text = @"--";
            }else{
                topView.lb_startDate.text = [self getNewStyleWithDateStr:self.detailModel.BEG_INT];
            }
            
            if ([Tools isBlankString:self.detailModel.END_INT]) {
                topView.lb_dqlx.text = @"--";
            }else{
                topView.lb_dqlx.text = self.detailModel.END_INT;
            }
            
            if ([Tools isBlankString:self.detailModel.ENDDATE]) {
                topView.lb_endDate.text = @"--";
            }else{
                topView.lb_endDate.text = [self getNewStyleWithDateStr:self.detailModel.ENDDATE];
            }
            
            if ([Tools isBlankString:self.detailModel.NET_INTDATE]) {
                topView.lb_nextDay.text = @"--";
            }else{
                topView.lb_nextDay.text = [self getNewStyleWithDateStr:self.detailModel.NET_INTDATE];
            }
            
            if ([Tools isBlankString:self.detailModel.PER_INTAMT]) {
                topView.lb_weekAmount.text = @"--";
            }else{
                topView.lb_weekAmount.text = self.detailModel.PER_INTAMT;
            }
            
            
            if ([Tools isBlankString:self.detailModel.PRE_INT]) {
                topView.lb_yflx.text = @"--";
            }else{
                topView.lb_yflx.text = self.detailModel.PRE_INT;
            }
        }
        
        
        
    }
    return _scrollView;
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.scrollView.maxY, ScreenWidth, TAB_BAR_HEIGHT)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        
        //再次购买
        UIButton* buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [buyBtn setTitle:@"再次购买" forState:UIControlStateNormal];
        [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        buyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        buyBtn.backgroundColor = [UIColor orangeColor];
        buyBtn.frame = CGRectMake(0, 0, ScreenWidth/2, 44);
        [buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:buyBtn];
        
        //提前支取
        UIButton* drawBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [drawBtn setTitle:@"提前支取" forState:UIControlStateNormal];
        [drawBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        drawBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        drawBtn.backgroundColor = [UIColor colorWithHex:0x137ef6];
        drawBtn.frame = CGRectMake(ScreenWidth/2, 0, ScreenWidth/2, 44);
        [drawBtn addTarget:self action:@selector(drawBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:drawBtn];
        
    }
    return _bottomView;
}

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