//
//  ElectAccountOPenSuccessController.m
//  Open
//
//  Created by 雷俊博 on 2020/7/30.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "ElectAccountOPenSuccessController.h"
#import "ElectAccountSuccessView.h"
#import "KKActivityPackageModel.h"
#import "SaveWelfareAlertView.h"
#import "CTAPIBaseManager.h"

@interface ElectAccountOPenSuccessController ()

@property (nonatomic, strong)UIScrollView* scrollView;

@end

@implementation ElectAccountOPenSuccessController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //隐藏返回按钮
    self.navigationItem.hidesBackButton = YES;
    //禁止页面左侧滑动返回，注意，如果仅仅需要禁止此单个页面返回，还需要在viewWillDisapper下开放侧滑权限
    // 禁用返回手势
    //禁止手势返回
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.scrollView];
    

    if (self.type == SaveFromTypeBiCai) {
        
    }else{
        NSData*data = [UserDefault objectForKey:@"activityPackageModel"];
        KKActivityPackageModel*model = nil;
        BOOL activityClick = [UserDefault boolForKey:@"activityClick"];
        BOOL openActivity = [UserDefault boolForKey:@"openActivity"];
        if (activityClick && (openActivity == NO)) {
            if (data) {
                model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            }
            if (model) {
                NSArray* timeArr = [model.activeTime componentsSeparatedByString:@"-"];
                if (timeArr.count == 2) {
                    NSString*startDate = timeArr[0];
                    NSString* endDate = timeArr[1];
                    if ([self validateWithStartTime:startDate withExpireTime:endDate]) {
                        [UserDefault setBool:YES forKey:@"openActivity"];
                        [SaveWelfareAlertView showAlertViewWithVC:self];
                        [self saveOPenAndRechargeRequest:model];
                    }
                }
                
            }
            
        }
    }
    
    
}

#pragma mark - 保存开户和存款活动信息
//开户保存信息(int i = activeType * 100 + 1;   开户  int i = activeType * 100 + 2;  购买)
- (void)saveOPenAndRechargeRequest:(KKActivityPackageModel *)model
{
    NSString*province = [UserDefault objectForKey:@"locationProvince"];
    NSString*city = [UserDefault objectForKey:@"locationCity"];
    NSString*county = [UserDefault objectForKey:@"locationCounty"];
    NSString* url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYinShuConfig],YSMarkActivitySuccessRequest];
    NSDictionary*params = @{
        @"activetype":@(model.activeType.intValue *100 + 1),
        @"telephone":[EnercryTool encryptUseDES:[getPeopleID PeopleIphoneNmber] key:DESKEY],
        @"amount":@"0",
        @"areaProvince":[Tools isBlankString:province] ?@"" :province,
        @"areaCity":[Tools isBlankString:city] ?@"" :city,
        @"areaCounty":[Tools isBlankString:county] ?@"" :county,
        @"systemType":@"iOS"
    };
    
    NSLog(@"保存开户和存款活动信息url:%@",url);
    NSLog(@"params:%@",params);
    
    
    [NetworkRequest Post:url Params:params Success:^(id _Nonnull objc) {
        
        NSLog(@"objc:%@",objc);
        
    } Failure:^(NSError* error) {
        NSLog(@"error:%@",error);
    }];
    

    
}


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

#pragma mark - event response
- (void)buyBtnClick:(UIButton *)sender
{
    self.navigationController.tabBarController.selectedIndex = 0;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - private methods
/**
 *  判断当前时间是否处于某个时间段内
 *
 *  @param startTime        开始时间
 *  @param expireTime       结束时间
 */

- (BOOL)validateWithStartTime:(NSString *)startTime withExpireTime:(NSString *)expireTime {
    NSDate *today = [NSDate date];
    
    startTime = [startTime stringByAppendingFormat:@" 00:00:00"];
    expireTime = [expireTime stringByAppendingFormat:@" 23:59:59"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // 时间格式,此处遇到过坑,建议时间HH大写,手机24小时进制和12小时禁止都可以完美格式化
    [dateFormat setDateFormat:@"yyyy.MM.dd HH:mm:ss"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*3600]];
    
    NSDate *start = [dateFormat dateFromString:startTime];
    NSDate *expire = [dateFormat dateFromString:expireTime];
    
    if ([today compare:start] == NSOrderedDescending && [today compare:expire] == NSOrderedAscending) {
        return YES;
    }
    return NO;
}



#pragma mark - getters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, ScreenWidth, ScreenHeight - cl_kNavigationBarHeight - cl_kTabBarHeight)];
        _scrollView.showsVerticalScrollIndicator = NO;
        //_scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(ScreenWidth, 0);
        
        //顶部view
        ElectAccountSuccessView* topView = [self getXiblWithClassName:@"ElectAccountSuccessView"];
       
        
        
        [_scrollView addSubview:topView];
        [topView.buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            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_icon.text = [NSString stringWithFormat:@"%@",[self.bcInfoModel.orgName substringToIndex:1]];
            topView.lb_no.text = [NSString stringWithFormat:@"%@ **** **** **** %@",[self.cardNo substringToIndex:4],[self.cardNo substringFromIndex:self.cardNo.length - 4]];
            topView.lb_bankName.text = self.bcInfoModel.orgName;
            topView.lb_message.text = [NSString stringWithFormat:@"您的%@电子账户开户成功",self.bcInfoModel.orgName];
        }else if (self.type == SaveFromTypeYinShu) {
            topView.lb_no.text = [NSString stringWithFormat:@"%@ **** **** **** %@",[self.cardNo substringToIndex:4],[self.cardNo substringFromIndex:self.cardNo.length - 4]];
        }
        
        
        //_scrollView.contentSize = CGSizeMake(ScreenWidth, topView.maxY + 10);
    }
    return _scrollView;
}


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