//
//  PBDocBaseController.m
//  IphoneBIMe
//
//  Created by zjf on 2018/8/1.
//  Copyright © 2018年 ProBIM. All rights reserved.
//

#import "PBDocBaseController.h"
#import "PBDocListTableViewCell.h"
#import "PBDocListController.h"
#import "PBDocPrivilegeModel.h"
#import "PBPreviewDocViewController.h"
#import "DwgViewController.h"
//#import "PBDocShareView.h"
#import "PBShareView.h"

static NSString *const CellID = @"CellId";
@interface PBDocBaseController ()
//@property (nonatomic, strong) PBDocShareView *docShareView;
@property (nonatomic, strong) PBShareView *shareView;
@property (nonatomic, strong) PBDocModel *previousDocModel;
@end

@implementation PBDocBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
}
- (void)setupTableView {
    self.tableView.estimatedRowHeight = 74.f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[PBDocListTableViewCell class] forCellReuseIdentifier:CellID];
    
    [self.tableView addSubview:self.promptView];
    self.promptView.imageV.image = [UIImage imageNamed:@"Doc_list_empty"];
    self.promptView.textL.text = @"暂无文档，请在WEB端添加文档";
    self.promptView.hidden = YES;
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _docList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBDocListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    cell.docModel = _docList[indexPath.row];
    return cell;
}
#pragma mark - Table view delete
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBDocModel *docModel = self.docList[indexPath.row];
    [self getDocPrivilegeListWith:docModel andAuthName:@"open" andCallBack:^(BOOL auth, NSError *error, NSString *roleId) {
        if (error) {
            [YJProgressHUD showMessage:@"加载权限失败" inView:self.view];
            return;
        }
        if (auth) {
            if ([docModel.FileSize isEqualToString:@"0"]) {
                PBDocListController *docListVC = [[PBDocListController alloc] init];
                docListVC.isOther = YES;
                docListVC.roleIdStr = roleId;
                docListVC.projectModel = self.projectModel;
                docListVC.docModel  = docModel;
                docListVC.title = docModel.FileName;
                [self.navigationController pushViewController:docListVC animated:YES];
            }else {
                if ([[docModel.FileExtensions lowercaseString] isEqualToString:@".dwg"]){
                    [self downDwgFileWith:docModel];
                }else if ([[docModel.FileExtensions lowercaseString] isEqualToString:@".mp4"]){
                    [self downFileWith:docModel];
                
                }else if ([[docModel.FileExtensions lowercaseString] isEqualToString:@".pdf"] || [[docModel.FileExtensions lowercaseString] isEqualToString:@".xlsx"]){
                    [self downFileWith:docModel];
                }else {
//                    PBPreviewDocViewController *previewDocVC  = [[PBPreviewDocViewController alloc] init];
//                    previewDocVC.projectModel = self.projectModel;
//                    previewDocVC.docModel = docModel;
//                    previewDocVC.modalPresentationStyle = UIModalPresentationFullScreen;
//                    [self presentViewController:previewDocVC animated:NO completion:nil];
                    [self getDocumentVersion:docModel];
                }
            }
        }else {
            [YJProgressHUD showMessage:@"暂无权限查看" inView:self.view];
        }
    }];
}
//请求文件数据
- (void)getDocumentVersion:(PBDocModel *)docModel {
    [YJProgressHUD showProgress:@"" inView:self.view];
    NSString *downLoadIP = BimUrl;
    NSArray *arr = [NSString getApiArrData];
    for (NSDictionary *obj in arr) {
        NSString *Check = [obj valueForKey:@"Check"];
        if ([Check isEqualToString:@"1"]) {
            NSString *Code = [obj valueForKey:@"Code"];
            if ([Code isEqualToString:@"300012"]){
                downLoadIP = @"http://localhost:6081";
                break;
            }
        }
    }

    NSDictionary *data = @{
                           @"ProjectID":self.projectModel.bimcomposerid,
                           @"FileID":docModel.FileId };
    NSString *url = [NSString stringWithFormat:@"%@/api/Doc/GetDocumentVersion",BimUrl];
    AFSecurityPolicy * securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.allowInvalidCertificates = YES;//是否允许自建的证书
    securityPolicy.validatesDomainName = NO;
    
    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [manager setSecurityPolicy:securityPolicy];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain",nil];
    [manager POST:url parameters:data headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [YJProgressHUD hide];
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        NSArray *docData = [NSString convertTodictionaryOrArr:str];
        NSString *fileInfoV = [docData[0] valueForKey:@"FileInfoVersionId"];
        NSString *downUrl = [NSString stringWithFormat:@"%@/api/Doc/GetFile?FileInfoVersionId=%@&FileKind=FileVersion&ProjectID=%@", downLoadIP, fileInfoV, self.projectModel.bimcomposerid];
        NSString *downUrlUtF8 = [downUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@":/?=&"].invertedSet];
        NSString *firstUrl = [NSString stringWithFormat:@"%@/api/DocViewer/Viewer/Show?downloadUrl=%@&tempNameAfterDownload=%@", BaseUrl,downUrlUtF8 ,docModel.FileName];
        NSString *docUrl = [NSString stringWithFormat:@"%@/Content/PDFJS/web/Viewer.html?file=%@", BaseUrl, [firstUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@":/?=&%  "].invertedSet]];
        NSLog(@"%@",docUrl);
       
        PBPreviewDocViewController *previewDocVC  = [[PBPreviewDocViewController alloc] init];
        previewDocVC.projectModel = self.projectModel;
        previewDocVC.name = docModel.FileName;
        previewDocVC.docUrl = docUrl;
        previewDocVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:previewDocVC animated:NO completion:nil];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [YJProgressHUD showFailed:@"加载失败" inview:self.view];
        NSLog(@"%@",error);
    }];
    
}
- (void)downPDFFileWith:(PBDocModel *)docModel {
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]){
        [self downLoadPDfFileWith:docModel.FileId andFileExtension:docModel.FileExtensions andName:docModel.FileName];
        return;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString *fileName;
    NSString *fileAbsolutePath = nil;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        if ([fileName isEqualToString:[NSString stringWithFormat:@"%@%@",docModel.FileId, docModel.FileExtensions]]) {
            fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        }
    }
    if (fileAbsolutePath == nil) {
        [self downLoadPDfFileWith:docModel.FileId andFileExtension:docModel.FileExtensions andName:docModel.FileName];
    }else {
        PBPreviewDocViewController *previewDocVC  = [[PBPreviewDocViewController alloc] init];
        previewDocVC.path = fileAbsolutePath;
        previewDocVC.name = docModel.FileName;
        previewDocVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:previewDocVC animated:NO completion:nil];
    }
}
- (void)downLoadPDfFileWith:(NSString *)fileID andFileExtension:(NSString *)extension andName:(NSString *)name {
    [YJProgressHUD showProgress:@"正在下载文件" inView:self.view];
    [[PBNetworkTools sharedTools] RequestDownLoadDwgFileWithProjectID:self.projectModel.bimcomposerid andFileID:fileID andExtension:extension andCallBack:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error) {
            [YJProgressHUD showMessage:@"下载失败" inView:nil];
            NSLog(@"%@",error);
            return;
        }
        NSLog(@"filePath:%@",filePath);
        NSString *str = [filePath absoluteString];
        str = [str stringByReplacingOccurrencesOfString:@"file://" withString:@""];
        [YJProgressHUD hide];
        PBPreviewDocViewController *previewDocVC  = [[PBPreviewDocViewController alloc] init];
        previewDocVC.path = str;
        previewDocVC.name = name;
        previewDocVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:previewDocVC animated:NO completion:nil];
    }];
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    PBDocModel *docModel = self.docList[indexPath.row];
//    UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"分享" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        [self shareWithDocModel:docModel];
//    }];
//    shareAction.backgroundColor = WarningColor;
    UITableViewRowAction *deleteAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要删除吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction  = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *determineAction  = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            //判断权限
            PBDocModel *docModel = self.docList[indexPath.row];
            [self getDocPrivilegeListWith:docModel andAuthName:@"delete" andCallBack:^(BOOL auth, NSError *error, NSString *roleId) {
                if (error) {
                    [YJProgressHUD showMessage:@"加载权限失败" inView:self.view];
                    return;
                }
                if (auth) {
                    [self deleteDocFileOrFolderWithDocModel:docModel andIndexPath:indexPath];
                }else {
                    [YJProgressHUD showMessage:@"暂无权限删除" inView:self.view];
                }
            }];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:determineAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }];
    deleteAction.backgroundColor = ErrorColor;
    if ([docModel.FileSize isEqualToString:@"0"]) {
        return @[deleteAction];
    }else {
//        return @[shareAction, deleteAction];
        return @[deleteAction];
    }
}
//分享权限判断
- (void)shareWithDocModel:(PBDocModel *)docModel {
    [self getDocPrivilegeListWith:docModel andAuthName:@"share" andCallBack:^(BOOL auth, NSError *error, NSString *roleId) {
        if (error) {
            [YJProgressHUD showMessage:@"加载权限失败" inView:self.view];
            return;
        }
        if (auth) {
            [self shareViewTypeWith:docModel];
        }else {
            [YJProgressHUD showMessage:@"暂无权限分享文件" inView:self.view];
        }
    }];
}
//分享类型判断（DWG/普通）
- (void)shareViewTypeWith:(PBDocModel *)docModel {
//    if ([[docModel.FileExtensions lowercaseString] isEqualToString:@".dwg"]) {
//        [self dwgDocShareViewShowWithDocModel:docModel];
//    }else {
//        [self GetDocumentVersionWith:docModel];
//    }
    
    [PBKeyWindow addSubview:self.shareView];
    self.shareView.docId = docModel.FileId;
    self.shareView.desc = docModel.FileName;
    NSString *type = [docModel.FileExtensions stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString *imageName = [NSString stringWithFormat:@"Doc_type_%@",[type lowercaseString]];
    UIImage *image = [UIImage imageNamed:imageName];
    if (image == nil) {
        image = [UIImage imageNamed:@"Doc_type_ unknown"];
    }
    self.shareView.image = image;
    [self.shareView show];
}
//- (void)GetDocumentVersionWith:(PBDocModel *)docModel {
//    [[PBNetworkTools sharedTools] GetDocumentVersionWithProjectID:self.projectModel.bimcomposerid andFileID:docModel.FileId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
//        if (error) {
//            NSLog(@"%@",error);
//            return;
//        }
//        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",str);
//        NSArray *arr = [NSString convertTodictionaryOrArr:str];
//        if (arr.count > 0) {
//            NSDictionary *dict = arr[0];
//            NSString *url = [NSString stringWithFormat:@"%@/api/Doc/GetFile?FileInfoVersionId=%@&FileKind=FileVersion&ProjectID=%@",BimUrl,[dict valueForKey:@"FileInfoVersionId"],self.projectModel.bimcomposerid];
//            [self docShareViewShowWithDocModel:docModel andUrl:url];
//        }else {
//            [YJProgressHUD showMessage:@"" inView:self.view afterDelayTime:1];
//        }
//    }];
//}


//- (void)dwgDocShareViewShowWithDocModel:(PBDocModel *)docModel {
//    [YJProgressHUD showProgress:@"" inView:nil];
//    [[PBNetworkTools sharedTools] RequestIDocViewWithProjectID:self.projectModel.organizeid andFileID:docModel.FileId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
//        if (error) {
//            NSLog(@"%@",error);
//            [YJProgressHUD showMessage:@"加载文件地址失败" inView:nil];
//            return;
//        }
//        [YJProgressHUD hide];
//        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
//        NSDictionary *dict = [NSString convertTodictionaryOrArr:str];
//        NSLog(@"%@",dict);
////            dict = @{
////              @"Ret": @"-1",
////              @"InnerUrl": @"https://bimcomposer.probim.cn/api/Doc/GetFile?ProjectID=0fcfb8cc-175b-417a-927e-2cf0c1877c02&keyValue=e75d9b5a-5f20-4d47-82ff-991240295416&FileKind=File",
////              @"OuterUrl": @"/view/url?url=",
////              @"OuterUrl2": @"&name=",
////              @"DWGUrlPre": @"https://www.probim.cn:9003"
////            };
//        NSNumber *num = [dict valueForKey:@"Ret"];
//        NSInteger ret = [num integerValue];
//        if (ret == -1) {
//            NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
//            NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
//            NSString *dwgUrl = [[dict valueForKey:@"InnerUrl"] stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
//            NSString *url = [NSString stringWithFormat:@"%@/Home/Index2?dwgurlcfg=%@&name=%@",[dict valueForKey:@"DWGUrlPre"], dwgUrl, docModel.FileName];
//            [self docShareViewShowWithDocModel:docModel andUrl:url];
//        }else {
//            [YJProgressHUD showMessage:@"加载失败" inView:nil];
//        }
//    }];
//}
//- (void)docShareViewShowWithDocModel:(PBDocModel *)docModel andUrl:(NSString *)url {
//    [PBKeyWindow addSubview:self.docShareView];
//    self.docShareView.desc = docModel.FileName;
//    self.docShareView.url = url;
//    NSString *type = [docModel.FileExtensions stringByReplacingOccurrencesOfString:@"." withString:@""];
//    NSString *imageName = [NSString stringWithFormat:@"Doc_type_%@",[type lowercaseString]];
//    UIImage *image = [UIImage imageNamed:imageName];
//    if (image == nil) {
//        image = [UIImage imageNamed:@"Doc_type_ unknown"];
//    }
//    self.docShareView.image = image;
//    [self.docShareView show];
//    NSLog(@"文件URL:%@",url);
//}

- (void)setProjectModel:(PBProjectModel *)projectModel {
    _projectModel = projectModel;
}

- (void)setPowerArr:(NSArray *)powerArr {
    _powerArr = powerArr;
}

- (void)setDocModel:(PBDocModel *)docModel {
    self.previousDocModel = docModel;
//    [YJProgressHUD showCustomAnimation:@"" inview:self.view];
    [YJProgressHUD showProgress:@"" inView:self.view];
    [[PBNetworkTools sharedTools] RequestGetAllFolderAndFileByFolderIDWithProjectID:self.projectModel.bimcomposerid andFolderID:docModel.FileId andLikeName:@"" andNormalOrDrawings:@"Normal" andRoleId:self.roleIdStr andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            [YJProgressHUD showMessage:@"加载文档失败" inView:nil];
            return;
        }
        [YJProgressHUD hide];
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSArray<PBDocModel *>*arr = [NSArray yy_modelArrayWithClass:[PBDocModel class] json:str];
        self.docList = arr.mutableCopy;
        [self.tableView reloadData];
        if (self.docList.count == 0) {
            self.promptView.hidden = NO;
        }else {
            self.promptView.hidden = YES;
        }
    }];
}


- (void)downDwgFileWith:(PBDocModel *)docModel {
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]){
        [self downLoadDwgFileWith:docModel.FileId];
        return;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    NSString * fileAbsolutePath = nil;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        if ([fileName isEqualToString:[NSString stringWithFormat:@"%@.dwg",docModel.FileId]]) {
            fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        }
    }
    if (fileAbsolutePath == nil) {
        [self downLoadDwgFileWith:docModel.FileId];
    }else {
//        DwgViewController *destViewController = [DwgViewController new];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"DwgViewController" bundle:nil];
        DwgViewController *destViewController = [storyboard instantiateInitialViewController];
        destViewController.cadFileName = fileAbsolutePath;
        destViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:destViewController animated:YES completion:nil];
        
    }
}
- (void)downFileWith:(PBDocModel *)docModel {
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]){
        [self downLoadFileWith:docModel];
        return;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    NSString * fileAbsolutePath = nil;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        if ([fileName isEqualToString:[NSString stringWithFormat:@"%@%@",docModel.FileId, docModel.FileExtensions]]) {
            fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        }
    }
    if (fileAbsolutePath == nil) {
        [self downLoadFileWith:docModel];
    }else {
        PBPreviewDocViewController *previewDocVC  = [[PBPreviewDocViewController alloc] init];
        previewDocVC.path = fileAbsolutePath;
        previewDocVC.name = docModel.FileName;
        previewDocVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:previewDocVC animated:NO completion:nil];
    }
}
- (void)downLoadDwgFileWith:(NSString *)fileID {
    [YJProgressHUD showProgress:@"正在下载文件" inView:self.view];
    [[PBNetworkTools sharedTools] RequestDownLoadDwgFileWithProjectID:self.projectModel.bimcomposerid andFileID:fileID andExtension:@".dwg" andCallBack:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error) {
            [YJProgressHUD showMessage:@"下载失败" inView:nil];
            NSLog(@"%@",error);
            return;
        }
        NSLog(@"filePath:%@",filePath);
        NSString *str = [filePath absoluteString];
        str = [str stringByReplacingOccurrencesOfString:@"file://" withString:@""];
        [YJProgressHUD hide];
        DwgViewController *destViewController = [DwgViewController new];
        destViewController.cadFileName = str;
        destViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:destViewController animated:YES completion:nil];
    }];
}
- (void)downLoadFileWith:(PBDocModel *)docModel {
    [YJProgressHUD showProgress:@"正在下载文件" inView:self.view];
    [[PBNetworkTools sharedTools] RequestDownLoadDwgFileWithProjectID:self.projectModel.bimcomposerid andFileID:docModel.FileId andExtension:docModel.FileExtensions andCallBack:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error) {
            [YJProgressHUD showMessage:@"下载失败" inView:nil];
            NSLog(@"%@",error);
            return;
        }
        NSLog(@"filePath:%@",filePath);
        NSString *str = [filePath absoluteString];
        str = [str stringByReplacingOccurrencesOfString:@"file://" withString:@""];
        [YJProgressHUD hide];
        PBPreviewDocViewController *previewDocVC  = [[PBPreviewDocViewController alloc] init];
        previewDocVC.path = str;
        previewDocVC.name = docModel.FileName;
        previewDocVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:previewDocVC animated:NO completion:nil];
    }];
}
#pragma mark - 删除文件／文件夹方法
- (void)deleteDocFileOrFolderWithDocModel:(PBDocModel *)docModel andIndexPath:(NSIndexPath*)indexPath {
    [YJProgressHUD showProgress:@"" inView:self.view];
    BOOL isFolder = [docModel.FileSize isEqualToString:@"0"];
    [[PBNetworkTools sharedTools] RequestDeleteFileOrFolderWithIsFolder:isFolder andProjectID:_projectModel.bimcomposerid andFileID:docModel.FileId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            [YJProgressHUD showMessage:@"删除失败" inView:nil];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if ([networkModel.Msg isEqualToString:@"OK"]) {
            [YJProgressHUD hide];
            [self.docList removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadData];
        }else {
            [YJProgressHUD showMessage:@"删除失败" inView:nil];
        }
    }];
}
#pragma mark - 获取权限
- (void)getDocPrivilegeListWith:(PBDocModel *)docModel andAuthName:(NSString *)authName andCallBack:(void (^) ( BOOL auth, NSError *error, NSString *roleId))callBack {
    [YJProgressHUD showProgress:@"" inView:nil];
    NSString *fileId;
    if ([docModel.FileSize isEqualToString:@"0"]) {
        fileId = docModel.FileId;
    }else {
        if (self.previousDocModel) {
            fileId = self.previousDocModel.FileId;
        }else {
            fileId = self.projectModel.bimcomposerid;
        }
    }
    [[PBNetworkTools sharedTools] GetDocRolesAuthByNameWithOrgID:self.projectModel.organizeid andBIMComposerID:self.projectModel.bimcomposerid andFolderID:fileId andAuthName:authName andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            callBack (NO, error, @"");
        }
        [YJProgressHUD hide];
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            PBNetworkModel *networkModel2 = [PBNetworkModel yy_modelWithDictionary:networkModel.Data];
            if (networkModel2.Ret == 1) {
                NSArray *authArr = networkModel2.Data;
                if (authArr.count == 0) {
                    callBack (NO, nil, @"");
                }else {
                    BOOL auth = NO;
                    NSString *roleId = @"";
                    for (NSDictionary *obj in networkModel2.Data) {
                        roleId = [obj valueForKey:@"RoleId"];
                        NSArray *arr = [obj valueForKey:@"RoleDocAuths"];
                        NSDictionary *dict = arr[0];
                        NSString *RoleDocAuthValue = [NSString stringWithFormat:@"%@",[dict valueForKey:@"RoleDocAuthValue"]];
                        if ([RoleDocAuthValue isEqualToString:@"1"]) {
                            auth = YES;
                            break;
                        }
                    }
                   
                    if (auth) {
                        callBack (YES, nil, roleId);
                    }else {
                        callBack (NO, nil, roleId);
                    }
                }
            }else {
                callBack (NO, error, @"");
            }
        }else {
            callBack (NO, error, @"");
        }
    }];
}

- (PBPromptView *)promptView {
    if (_promptView == nil) {
        _promptView = [[PBPromptView alloc] initWithFrame:CGRectMake(0, 0, PBScreenWidth, PBScreenHeight)];
    }
    return _promptView;
}

//- (PBDocShareView *)docShareView {
//    if (_docShareView == nil) {
//        _docShareView = [[PBDocShareView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//        CGFloat height;
//        if (IS_IPHONE_X) {
//            height = 193.f + 34.f;
//        }else {
//            height = 193.f;
//        }
//        _docShareView.visualViewHeight = height;
//        _docShareView.viewController = self;
//        _docShareView.title = @"文档";
//    }
//    return _docShareView;
//}
- (PBShareView *)shareView {
    if (_shareView == nil) {
        _shareView  = [[PBShareView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        CGFloat height;
        if (IS_IPHONE_X) {
            height = 290.f + 34.f;
        }else {
            height = 290.f;
        }
        _shareView.visualViewHeight = height;
        _shareView.viewController = self;
        _shareView.projectID = self.projectModel.bimcomposerid;
        _shareView.title = @"文档";
    }
    return _shareView;
}
- (void)setRoleIdStr:(NSString *)roleIdStr {
    _roleIdStr = roleIdStr;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

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