//
//  uoloadFileRecordViewController.m
//  隐私保护
//
//  Created by xd h on 2023/11/13.
//

#import "uploadFileRecordViewController.h"
#import "uploadFileManager.h"
#import "uploadFileDataModel.h"
#import "uploadFileRecordheadView.h"
//#import "uploadFileRecordBodyView.h"
#import "uploadFileRecordEditBottomView.h"
#import "uploadFileRecordBgScorllView.h"
#import "uploadFileRecordTableView.h"
#import "uploadFileRecordTableViewHeadView.h"
#import "ComontAlretViewController.h"
#import "backupsFilerecordTableView.h"
#import "backupsFileManager.h"
#import "photosBackupsTaskModel.h"
#import "downloadManager.h"
#import "downloadFileRecordTableView.h"
#import "MySetViewController.h"

@interface uploadFileRecordViewController ()<UIScrollViewDelegate>

//选中的项
@property (nonatomic, strong) NSMutableArray *indexPathsForSelectedItems;
@property (nonatomic, strong) uploadFileRecordheadView *uploadFileRecordheadV;

@property (nonatomic, strong) uploadFileRecordBgScorllView *uploadFileRecordBgScorllV;
@property (nonatomic, strong) uploadFileRecordTableView *leftTableView;
@property (nonatomic, strong) downloadFileRecordTableView *midTableView;
@property (nonatomic, strong) backupsFilerecordTableView *rightTableView;

//@property (nonatomic,strong) uploadFileRecordTableViewHeadView *leftHeadView;
//@property (nonatomic,strong) uploadFileRecordTableViewHeadView *midHeadView;


//@property (nonatomic, strong) NSMutableArray *allDataArr;

@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) uploadFileRecordEditBottomView *uploadFileRecordEditBottomV;
@property (nonatomic,assign) BOOL isEditType;
@property(nonatomic,assign) NSInteger selectIndex;// 从1开始

@property (nonatomic,assign) BOOL didGetUploadDataType;
@property (nonatomic,assign) BOOL didGetDownloadDataType;
@property (nonatomic,assign) BOOL didGetbackupsloadDataType;
@end

@implementation uploadFileRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //[self.view setBackgroundColor:HWF5F7FAColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadFileRefreshFun:)  name:uploadFileRefreshNotification  object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backupsFileRefreshFun:)  name:backupsFileRefreshNotification  object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadFileRefreshFun:)  name:downloadFileRefreshNotification  object:nil];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    self.titleLabel.text = NSLocalizedString(@"my_set_no_File_Transfer_List",nil) ;
    
    [self.toolBar setHidden:YES];
    [self.navigationBar setHidden:YES];
    [self.navBarBGView setHidden:NO];
    self.navBarBGView.backgroundColor = [UIColor whiteColor];
    
    [self initbaseUIFun];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getAllDataFun];
}

#pragma mark 获取数据
- (void)getAllDataFun
{
    [SVProgressHUD show];;
    
    if(!_isUploadingType){
        [self getUploadDataInDatabaseFun];
    }
    
    if(!_isDownloadingType){
        [self getDownloadDataInDatabaseFun:YES];
    }
    else{
        KWeakSelf
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf delayChangeUIFun];
        });
    }
    
    [self getBackupsDatabaseFun];
}

- (void)handleRemoveIndicator
{
    if(_didGetUploadDataType
       && _didGetDownloadDataType
       && _didGetbackupsloadDataType){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }
   
}

- (void)delayChangeUIFun
{
    _selectIndex = 2;
    [self didClickHeadFun:_selectIndex];
    [self didScorllBodyFun:_selectIndex];
}

#pragma mark UI
- (void)initbaseUIFun
{
    _uploadFileRecordheadV = [[uploadFileRecordheadView alloc] init];
    [self.view addSubview:_uploadFileRecordheadV];
    
    [_uploadFileRecordheadV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.navBarBGView.mas_bottom).offset(15.0);
        make.height.mas_equalTo(50);
    }];
    _selectIndex = 1;
    KWeakSelf
#pragma mark 头部点击事件
    _uploadFileRecordheadV.didClickButFun = ^(NSInteger index) {
        [weakSelf didClickHeadFun: index];
    };
    
    //CGFloat curHeight = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - 50 - 15;
    CGFloat curHeight = SCREEN_H - (H_STATE_BAR + 64.f) - 50 - 15;
    
    
    _uploadFileRecordBgScorllV= [[uploadFileRecordBgScorllView alloc] init];
    _uploadFileRecordBgScorllV.delegate = self;
    [self.view addSubview:_uploadFileRecordBgScorllV];
    [_uploadFileRecordBgScorllV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(_uploadFileRecordheadV.mas_bottom).offset(0.0);
        make.bottom.mas_equalTo(0);
    }];
    
    
    _leftTableView = [[uploadFileRecordTableView alloc] init];
    _leftTableView.tag =100;
    [_uploadFileRecordBgScorllV addSubview:_leftTableView];

    [_leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(SCREEN_W*0.0);
        make.width.mas_equalTo(SCREEN_W);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(curHeight);
    }];

    _leftTableView.didLongPressClick = ^{
        weakSelf.isEditType = YES;
    };
    
    _leftTableView.didClickSectionHeadViewRightButton = ^(NSInteger section) {
        [weakSelf didClikRightButInUploadTableVieFunBy:section];
    };

    _leftTableView.didNeedRefreshGetDatabaseFun = ^{
        [weakSelf RefreshUploadViewFun];
    };
    
    _midTableView = [[downloadFileRecordTableView alloc] init];
    _midTableView.tag = 101;
    [_uploadFileRecordBgScorllV addSubview:_midTableView];

    [_midTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(SCREEN_W*1.0);
        make.width.mas_equalTo(SCREEN_W);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(curHeight);
    }];

    _midTableView.didLongPressClick = ^{
        weakSelf.isEditType = YES;
    };
    
    _midTableView.didClickSectionHeadViewRightButton = ^(NSInteger section) {
        [weakSelf didClikRightButInDownloadTableVieFunBy:section];
    };

    _midTableView.didNeedRefreshGetDatabaseFun = ^{
        [weakSelf getDownloadDataInDatabaseFun:NO];
    };
    
    _rightTableView = [[backupsFilerecordTableView alloc] init];
    _rightTableView.tag = 102;
    [_uploadFileRecordBgScorllV addSubview:_rightTableView];

    [_rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(SCREEN_W*2.0);
        make.width.mas_equalTo(SCREEN_W);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(curHeight);
    }];

    _rightTableView.didLongPressClick = ^{
       // weakSelf.isEditType = YES;
    };
    
 
    _uploadFileRecordBgScorllV.contentSize = CGSizeMake(SCREEN_W*3.0, curHeight);
    
//    _leftHeadView =  [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
//    _leftTableView.tableHeaderView = _leftHeadView;
//    _leftHeadView.didClickButFun = ^{
//        [weakSelf didClickAllSuspendInLoadingFun];
//    };
//
//    _midHeadView =  [[uploadFileRecordTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 40)];
//    _midTableView.tableHeaderView = _midHeadView;
//    _midHeadView.didClickButFun = ^{
//        [weakSelf didClickClearRecordInDoneFun];
//    };
    


  
    _leftButton = [[UIButton alloc] init];
    [_leftButton setTitleColor:[UIColor hwColor:@"#01B7EA" alpha:1.0] forState:UIControlStateNormal];
    _leftButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
    _leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_leftButton addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
    _leftButton.tag = 1;
    [_leftButton setTitle:NSLocalizedString(@"other_cancel",nil) forState:UIControlStateNormal];
    _leftButton.hidden = YES;
    [self.navBarBGView addSubview:_leftButton];
    
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(self.backBtn.mas_height);
        make.centerY.mas_equalTo(self.backBtn.mas_centerY);
    }];
    
    _rightButton = [[UIButton alloc] init];
    [_rightButton setTitleColor:[UIColor hwColor:@"#01B7EA" alpha:1.0] forState:UIControlStateNormal];
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
    _rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_rightButton addTarget:self action:@selector(didClickButFun:) forControlEvents:UIControlEventTouchUpInside];
    _rightButton.tag = 2;
    [_rightButton setTitle:NSLocalizedString(@"File_upload_Record_select_all",nil) forState:UIControlStateNormal];
    _rightButton.hidden = YES;
    [self.navBarBGView addSubview:_rightButton];
    
    //多国语言适配
    NSString *languageCode = [NSLocale preferredLanguages][0];
    if([languageCode rangeOfString:@"ja-"].location != NSNotFound)
    {
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:13.0];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:13.0];
    }
    
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(90);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(self.backBtn.mas_height);
        make.centerY.mas_equalTo(self.backBtn.mas_centerY);
    }];
    
    _uploadFileRecordEditBottomV = [[uploadFileRecordEditBottomView alloc] init];
    _uploadFileRecordEditBottomV.hidden = YES;
    [self.view addSubview:_uploadFileRecordEditBottomV];
    
    [_uploadFileRecordEditBottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(60 + safeArea);
        make.bottom.mas_equalTo(0);
    }];
    
    _uploadFileRecordEditBottomV.didClickButFun = ^{
        //[weakSelf.uploadFileRecordBodyV deleteModelOneByOneFun];
        
        [weakSelf didClickBottomDeleteFun];
    };
}

#pragma mark 底部删除点击
-(void)didClickBottomDeleteFun
{
    NSMutableArray *selectModelArr ;//= _leftTableView.selectModelArr;
    
    if(_selectIndex == 1){
        selectModelArr = _leftTableView.selectModelArr;
    }
    else if(_selectIndex == 2){
        selectModelArr = _midTableView.selectModelArr;
    }
    else if(_selectIndex == 3){
        return;
    }
    
    if(!selectModelArr || selectModelArr.count== 0){
        
        [[iToast makeText:NSLocalizedString(@"File_upload_Record_delete_noFile",nil)] show];
        
        return;
    }
    
    
    NSString *delTipTip = nil;
    if(_selectIndex == 1){
        delTipTip = NSLocalizedString(@"File_upload_Record_clear_uploading_Tip_msg",nil);
    }
    else if(_selectIndex == 2){
        delTipTip = NSLocalizedString(@"File_upload_Record_clear_done_Tip_msg",nil);
    }
    else if(_selectIndex == 3){
        delTipTip = NSLocalizedString(@"File_upload_Record_clear_fail_Tip_msg",nil);
    }
    
    /*弹窗提示二次确认*/
    KWeakSelf
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
                                                                                     msg:delTipTip
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                 okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        [weakSelf didClickBottomDeleteFunAfterAleartFun];
    } didClickCancel:^{
        
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
  
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];
}


-(void)didClickBottomDeleteFunAfterAleartFun
{

    [SVProgressHUD show];
    if(_selectIndex == 1){
        [_leftTableView deleteModelOneByOneFun];
    }
    else if(_selectIndex == 2){
        [_midTableView deleteModelOneByOneFun];
    }
    else if(_selectIndex == 3){
        //没有操作
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }
}

#pragma mark 头部点击 背景滚动
- (void)didClickHeadFun:(NSInteger)index
{
    //_uploadFileRecordBodyV.selectIndex = index;
    
    _selectIndex = index;
    _uploadFileRecordBgScorllV.contentOffset = CGPointMake(SCREEN_W *(index -1), 0);
}

- (void)didScorllBodyFun:(NSInteger)index
{
    _uploadFileRecordheadV.selectIndex = index;
    
    _selectIndex = index;
}

#pragma mark 1.3版本UI修改后 上传文件列表的内容点击时间
- (void)didClikRightButInUploadTableVieFunBy:(NSInteger)section
{
    if(section == 0){
        [self didClickAllSuspendInLoadingFun];
    }
    else if(section == 1){//成功
        [self didClickClearRecordInDoneFun];
    }
    else if(section == 2){//失败
        [self didClickClearRecordInFailFun];
    }
}

#pragma mark  背景滚动代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView != _uploadFileRecordBgScorllV) return;
    
    CGFloat x = scrollView.contentOffset.x;
    NSInteger index = (x+1)/SCREEN_W;
    
    index += 1;//因为头部是从1开始的
    
     //HLog(@"index:%ld",index);
    [self didScorllBodyFun:index];
}

-(void)gotoUploadFile:(NSMutableArray*)indexPathsForSelectedItems
{
    KWeakSelf
    mainBlock(^{
        weakSelf.didGetUploadDataType = YES;
        [weakSelf handleRemoveIndicator];
    });
    
    [self realGotoUploadFile:indexPathsForSelectedItems];
}

- (void)realGotoUploadFile:(NSMutableArray *)indexPathsForSelectedItems
{
    KWeakSelf
    [[uploadFileManager shareInstance] handlTZAssetModelToUploadFileDataFunBy:indexPathsForSelectedItems complete:^(NSMutableArray * _Nonnull Arr) {
        //NSMutableArray *arr1 = Arr;
        //[weakSelf setRecordBodyDataFunBy:arr1];
        
        //weakSelf.leftTableView.curDataArr = Arr[0];
        mainBlock(^{
            weakSelf.leftTableView.outSideDataArr = Arr;
            [weakSelf handleUploadingRightButFun:YES];
        });
        
    }];
}

#pragma mark 读取数据库数据
- (void)getUploadDataInDatabaseFun
{
    
    KWeakSelf
    [[uploadFileManager shareInstance] getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
        //[weakSelf setRecordBodyDataFunBy:Arr];
        
        weakSelf.didGetUploadDataType = YES;
        [weakSelf handleRemoveIndicator];
        
        if(!Arr ||Arr.count != 3){
            HLog(@"数据库出错\n\n\n");
            return;
        }
        
        NSMutableArray *curLoadIngArr = Arr[0];
        //不是上传视频和进来的  所以上传状态（上传中和等待）改为暂停
        if(![uploadFileManager shareInstance].isUploadIngType){
            for (uploadFileDataModel * curModel in curLoadIngArr) {
                curModel.curUploadStateType = uploadStateSuspend;
            }
        }
        
        mainBlock(^{
            weakSelf.leftTableView.outSideDataArr = Arr;
            [weakSelf handleUploadingRightButFun:NO];
        });
    }];
    
}

- (void)getBackupsDatabaseFun
{
    KWeakSelf
    [photosBackupsTaskModel bg_findAsync:backups_photos_tableName limit:1 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
        mainBlock(^{
            weakSelf.didGetbackupsloadDataType = YES;
            [weakSelf handleRemoveIndicator];
            weakSelf.rightTableView.curDataArr = array;
        });
            
    }];
}

#pragma mark 读取数据库数据
- (void)getDownloadDataInDatabaseFun:(BOOL)isRegetType
{
    KWeakSelf
    [[downloadManager shareInstance] getDataInDatabaseFun:isRegetType complete:^(NSMutableArray * _Nonnull Arr) {
      
        weakSelf.didGetDownloadDataType = YES;
        [weakSelf handleRemoveIndicator];
        
        if(!Arr ||Arr.count != 3){
            HLog(@"数据库出错\n\n\n");
            return;
        }
        
        NSMutableArray *curLoadIngArr = Arr[0];
        //不是上传视频和进来的  所以上传状态（上传中和等待）改为暂停
        if(![downloadManager shareInstance].isDownloadIngType){
            for (couldPhoneFileModel * curModel in curLoadIngArr) {
                curModel.curDownloadStateType = downloadStateSuspend;
            }
        }
        
        mainBlock(^{
            weakSelf.midTableView.outSideDataArr = Arr;
            [weakSelf.midTableView reloadDataFun];
            [weakSelf handleDownloadingRightButFun];
        });
    }];
    
}


#pragma mark 处理上传中的 全部暂停等
- (void)handleUploadingRightButFun:(BOOL)isUserUploadIngType
{
    if(_leftTableView.uploadingArr.count == 0){
        //不处理
    }
    else{
        NSMutableArray *upLoadingArr = [uploadFileManager shareInstance].fileModelDataArr;
        BOOL isUploadIngType = [uploadFileManager shareInstance].isUploadIngType;

        if(!isUserUploadIngType
           &&(!upLoadingArr ||upLoadingArr.count == 0 || !isUploadIngType)){//不是上传状态
            _leftTableView.uploadingHeadView.rightButton.selected = YES;
        }
        else if(upLoadingArr.count == _leftTableView.uploadingArr.count){
            _leftTableView.uploadingHeadView.rightButton.selected = NO;
        }
        else{
            _leftTableView.uploadingHeadView.rightButton.selected = YES;
        }
    }
}

#pragma mark 处理下载中的 全部暂停等
- (void)handleDownloadingRightButFun
{
    if(_midTableView.downloadingArr.count == 0){
        //不处理
    }
    else{
        NSMutableArray *upLoadingArr = [downloadManager shareInstance].downLoadFileModelDataArr;
        BOOL isDownloadIngType = [downloadManager shareInstance].isDownloadIngType;

        if(!upLoadingArr ||upLoadingArr.count == 0 || !isDownloadIngType){//不是下载状态
            _midTableView.downloadingHeadView.rightButton.selected = YES;
        }
        else if(upLoadingArr.count == _midTableView.downloadingArr.count){
            _midTableView.downloadingHeadView.rightButton.selected = NO;
        }
        else{
            _midTableView.downloadingHeadView.rightButton.selected = YES;
        }
    }
}

- (void)uploadFileRefreshFun:(NSNotification*)not
{
    uploadFileDataModel *uploadFileDataMod = [not object];
    //HLog(@"%@",uploadFileDataMod);
    
    uploadFileDataMod.curTimeInterval = [[NSDate date] timeIntervalSince1970];
    
    NSMutableArray *uploadingArr = _leftTableView.uploadingArr;
    
    for (int i=0; i<uploadingArr.count; i++) {
        uploadFileDataModel*preModel = uploadingArr[i];
        if(preModel.bg_id.integerValue == uploadFileDataMod.bg_id.integerValue){
            [_leftTableView.uploadingArr replaceObjectAtIndex:i withObject:uploadFileDataMod];
            break;
        }
    }
    
    if(uploadFileDataMod.curUploadStateType == uploadStateDone){
        [self RefreshUploadViewFun];
    }
    else{
        [_leftTableView RefreshAllDataFun];
    }
}

#pragma mark 那上传那边的数据来更新页面
- (void)RefreshUploadViewFun
{
    KWeakSelf
    [[uploadFileManager shareInstance] getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
        //[weakSelf setRecordBodyDataFunBy:Arr];
        
        if(!Arr ||Arr.count != 3){
            HLog(@"数据库出错\n\n\n");
            return;
        }
        
        mainBlock(^{
            weakSelf.leftTableView.outSideDataArr = Arr;
            //[weakSelf handleUploadingRightButFun];
        });
    }];
}

#pragma mark 按钮点击
- (void)didClickButFun:(UIButton*)but
{
    NSInteger tag = but.tag;
    if(tag == 1){
        self.isEditType = NO;
        //self.uploadFileRecordBodyV.isEditType = NO;
        
        if(_selectIndex == 1){
            self.leftTableView.isEditType = NO;
        }
        else if(_selectIndex == 2){
            self.midTableView.isEditType = NO;
        }
        else if(_selectIndex == 3){
            //self.rightTableView.isEditType = NO;
        }
        
    }
    else{
        //self.uploadFileRecordBodyV.isSelectAllType = YES;
        
        if(_selectIndex == 1){
            self.leftTableView.isSelectAllType = YES;
        }
        else if(_selectIndex == 2){
            self.midTableView.isSelectAllType = YES;
        }
        else if(_selectIndex == 3){
            //self.rightTableView.isSelectAllType = YES;
        }
    }
}

-(void)setIsEditType:(BOOL)isEditType
{
    _isEditType = isEditType;
    
    _leftButton.hidden = !isEditType;
    _rightButton.hidden = !isEditType;
    self.backBtn.hidden = isEditType;
    
    _uploadFileRecordEditBottomV.hidden = !isEditType;
    _uploadFileRecordheadV.userInteractionEnabled = !isEditType;
    _uploadFileRecordBgScorllV.scrollEnabled = !isEditType;
    
    //_leftHeadView.rightButton.userInteractionEnabled = !isEditType;
    
    if(_isEditType){
       
        CGFloat curHeight = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - 50 - 15 - (60 + safeArea);
        [_uploadFileRecordBgScorllV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(_uploadFileRecordheadV.mas_bottom).offset(0.0);
            make.bottom.mas_equalTo(_uploadFileRecordEditBottomV.mas_top);
        }];
        
        [_leftTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_W*0.0);
            make.width.mas_equalTo(SCREEN_W);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(curHeight);
        }];
        
        [_midTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_W*1.0);
            make.width.mas_equalTo(SCREEN_W);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(curHeight);
        }];
        
        
        [_rightTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_W*2.0);
            make.width.mas_equalTo(SCREEN_W);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(curHeight);
        }];
        
    }
    else{

        CGFloat curHeight = SCREEN_H - CGRectGetMaxY(self.navBarBGView.frame) - 50 - 15;
        [_uploadFileRecordBgScorllV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(_uploadFileRecordheadV.mas_bottom).offset(0.0);
            make.bottom.mas_equalTo(0);
        }];
        
        [_leftTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_W*0.0);
            make.width.mas_equalTo(SCREEN_W);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(curHeight);
        }];
        
        [_midTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_W*1.0);
            make.width.mas_equalTo(SCREEN_W);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(curHeight);
        }];
        
        
        [_rightTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(SCREEN_W*2.0);
            make.width.mas_equalTo(SCREEN_W);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(curHeight);
        }];
    }
}

#pragma mark 点击上传中的全部暂停
- (void)didClickAllSuspendInLoadingFun
{
    _leftTableView.uploadingHeadView.rightButton.selected = !_leftTableView.uploadingHeadView.rightButton.selected;
    
    if(_leftTableView.uploadingHeadView.rightButton.selected){
        [[uploadFileManager shareInstance] suspendUploadFileFun:YES];
        [_leftTableView reloadDataFun];
    }
    else{
        NSMutableArray *leftArr = _leftTableView.uploadingArr;
        [[uploadFileManager shareInstance] reUploadFileFunBy:leftArr withAll:YES];
    }
}
#pragma mark 点击上传成功的清空记录
- (void)didClickClearRecordInDoneFun
{
    /*弹窗提示二次确认*/
    KWeakSelf
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
                                                                                     msg:NSLocalizedString(@"File_upload_Record_clear_done_Tip_msg",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                 okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        [weakSelf ClearAllRecordInDoneFun];
    } didClickCancel:^{
        
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
  
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];

}

- (void)ClearAllRecordInDoneFun
{
    [[uploadFileManager shareInstance] deleteUploadFileRecordBy:_leftTableView.uploadDoneArr withDelCache:YES complete:^(BOOL isSuccess) {
        HLog(@"isSuccess:%d",isSuccess);

        if(isSuccess){
            [self->_leftTableView.uploadDoneArr removeAllObjects];
            [self->_leftTableView reloadDataFun];
        }
    }];
}

#pragma mark 点击上传失败的清空记录
- (void)didClickClearRecordInFailFun
{
    /*弹窗提示二次确认*/
    KWeakSelf
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
                                                                                     msg:NSLocalizedString(@"File_upload_Record_clear_fail_Tip_msg",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                 okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        [weakSelf ClearAllRecordInFailFun];
    } didClickCancel:^{
        
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
  
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];

}

- (void)ClearAllRecordInFailFun
{
    [[uploadFileManager shareInstance] deleteUploadFileRecordBy:_leftTableView.uploadfailArr withDelCache:YES complete:^(BOOL isSuccess) {
        HLog(@"isSuccess:%d",isSuccess);
        
        if(isSuccess){
            [self->_leftTableView.uploadfailArr removeAllObjects];
            [self->_leftTableView reloadDataFun];
        }
    }];
}



-(void)backBtnPressed{
    //[super backBtnPressed];

    NSArray *vcArrs = self.navigationController.viewControllers;
    
    BOOL didPopType = NO;
    for (UIViewController *vc in vcArrs) {
        if([vc isKindOfClass:[MySetViewController class]]){
            didPopType = YES;
            [self.navigationController popToViewController:vc animated:YES];
            return;
        }
    }
    
    if(!didPopType){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)backupsFileRefreshFun:(NSNotification*)not
{
    photosBackupsTaskModel *photosBackupsTaskMod = [not object];
    //HLog(@"%@",uploadFileDataMod);
    
    if(photosBackupsTaskMod.didBackupsOneFileType){
        photosBackupsTaskMod.curTimeInterval = 0;
        photosBackupsTaskMod.preTimeInterval = 0;
    }
    else{
        photosBackupsTaskMod.curTimeInterval = [[NSDate date] timeIntervalSince1970];
    }
   
    _rightTableView.photosBackupsIngTaskModel = photosBackupsTaskMod;
}

-(void)gotoDownloadFile:(NSMutableArray*)selectedItems
{
    KWeakSelf
    [[downloadManager shareInstance] handleCouldPhoneFileModelToDownloadFileDataFunBy:selectedItems complete:^(NSMutableArray * _Nonnull Arr) {
        
        weakSelf.didGetDownloadDataType = YES;
        [weakSelf handleRemoveIndicator];
  
        if(!Arr ||Arr.count != 3){
            HLog(@"数据库出错\n\n\n");
            return;
        }
        
        //NSMutableArray *curLoadIngArr = Arr[0];
        
        mainBlock(^{
            weakSelf.midTableView.outSideDataArr = Arr;
            [weakSelf.midTableView reloadDataFun];
            //[weakSelf handleUploadingRightButFun];
        });
    }];
}

- (void)downloadFileRefreshFun:(NSNotification*)not
{
    couldPhoneFileModel *downloadTaskMod = [not object];
    HLog(@"hhhhhhh1 %@ didDownloadBytes:%ld",downloadTaskMod.name,downloadTaskMod.didDownloadBytes);
    
    downloadTaskMod.curTimeInterval = [[NSDate date] timeIntervalSince1970];
    
    NSMutableArray *downloadingArr = _midTableView.downloadingArr;
    
    for (int i=0; i<downloadingArr.count; i++) {
        couldPhoneFileModel*preModel = downloadingArr[i];
        if(preModel.bg_id.integerValue == downloadTaskMod.bg_id.integerValue){
            [_midTableView.downloadingArr replaceObjectAtIndex:i withObject:downloadTaskMod];
            break;
        }
    }
    
    if(downloadTaskMod.curDownloadStateType == downloadStateDone
       ||downloadTaskMod.curDownloadStateType == downloadStateFail){
        [self getDownloadDataInDatabaseFun:NO];
    }
    else{
        [_midTableView RefreshAllDataFun];
    }
}

#pragma mark 1.3版本UI修改后 下载文件列表的头部点击事件
- (void)didClikRightButInDownloadTableVieFunBy:(NSInteger)section
{
    if(section == 0){
        [self didClickAllSuspendInDownLoadingFun];
    }
    else if(section == 1){//成功
        [self didClickClearDownLoadRecordInDoneFun];
    }
    else if(section == 2){//失败
        [self didClickClearRecordInDownLoadFailFun];
    }
}

#pragma mark 点击下载中的全部暂停
- (void)didClickAllSuspendInDownLoadingFun
{
    _midTableView.downloadingHeadView.rightButton.selected = !_midTableView.downloadingHeadView.rightButton.selected;
    
    if(_midTableView.downloadingHeadView.rightButton.selected){
        [[downloadManager shareInstance] suspendDownloadFileFun:YES];
    }
    else{
        NSMutableArray *leftArr = _midTableView.downloadingArr;
        [[downloadManager shareInstance] reDownloadFileFunBy:leftArr withAll:YES];
    }
}


#pragma mark 点击下载成功的清空记录
- (void)didClickClearDownLoadRecordInDoneFun
{
    /*弹窗提示二次确认*/
    KWeakSelf
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
                                                                                     msg:NSLocalizedString(@"File_upload_Record_clear_done_Tip_msg",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                 okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        [weakSelf ClearAllRecordInDownloadDoneFun];
    } didClickCancel:^{
        
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
  
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];

}

- (void)ClearAllRecordInDownloadDoneFun
{
    [[downloadManager shareInstance] deleteDownloadFileRecordBy:_midTableView.downloadDoneArr withDelCache:YES];
}


#pragma mark 点击上传失败的清空记录
- (void)didClickClearRecordInDownLoadFailFun
{
    /*弹窗提示二次确认*/
    KWeakSelf
    ComontAlretViewController *nextVC = [[ComontAlretViewController alloc] initWithTiTle:NSLocalizedString(@"File_upload_Record_clear_Tip_title",nil)
                                                                                     msg:NSLocalizedString(@"File_upload_Record_clear_fail_Tip_msg",nil)
                                                                                imageStr:nil
                                                                             cancelTitle:NSLocalizedString(@"other_cancel",nil)
                                                                                 okTitle:NSLocalizedString(@"other_comment_ok",nil) isOkBtnHighlight:YES
                                                                              didClickOk:^{
        [weakSelf ClearAllRecordInDownloadFailFun];
    } didClickCancel:^{
        
    }];
    nextVC.modalPresentationStyle = UIModalPresentationCustom;
  
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.view.superview.backgroundColor = [UIColor clearColor];
    }];

}

- (void)ClearAllRecordInDownloadFailFun
{
    [[downloadManager shareInstance] deleteDownloadFileRecordBy:_midTableView.downloadfailArr withDelCache:YES];
}


@end
