//
//  WorkFlowDetailsController.m
//  smartRhino
//
//  Created by Android on 2019/12/18.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "WorkFlowDetailsController.h"
#import "HistoricalWorkFlowDetailsBaseCell.h"
#import "WorkFlowPersonnelListController.h"
#import "HistoricalWorkFlowDetailsTitleCell.h"
#import "HistoricalWorkFlowDetailsContentCell.h"
#import "MeetingDetailsTimeCell.h"
#import "MyWorkFlowListCell.h"
#import "NoteBookShareVC.h"
#import "MyTDGroupViewController.h"
#import "MyTDTopicCreateVC.h"
#import "MoveViewController.h"
#import "DownFileViewController.h"
#import "HomeDetailController.h"
#import "ChatMsgNoticeDetailVC.h"
#import "MyTDTopicDetailVC.h"
#import "TDInterLeterDetailVC.h"
#import "NoteBookDetailVC.h"
#import "TDGroupInfoListVC.h"
#import "HistoricalWorkFlowDetailsMoreCell.h"
#import "WorkFlowOrderController.h"
#import "WorkFlowFecordController.h"
#import "MyApprovalPageDetail.h"
#import "ShareListVC.h"

@interface WorkFlowDetailsController ()<UITableViewDelegate,UITableViewDataSource,NoteBookShareVCDelegate>

@property (weak, nonatomic) IBOutlet UITableView *detailsTabView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstant;
@property (strong, nonatomic) MyMeetingListModel       * meetingModel;
@property (strong, nonatomic) NoteBookShareVC          * noteBookShareVC;
@end

@implementation WorkFlowDetailsController

- (instancetype)initWithId:(NSInteger)meetingId {
    if (self = [super init]) {
        self.meetingId = meetingId;
    }
    return self;
}
- (IBAction)returnAction:(id)sender {
    [self shareHander];
}

- (void)shareHander{
    [self.noteBookShareVC initNoteBookShareData];
    self.noteBookShareVC.isWork = YES;
    self.noteBookShareVC.view.hidden = !self.noteBookShareVC.view.hidden;
}
- (NoteBookShareVC *)noteBookShareVC{
    if (_noteBookShareVC == nil) {
        _noteBookShareVC = [NoteBookShareVC initNoteBookShareVC];
        [_noteBookShareVC.view setFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_noteBookShareVC.view setHidden:YES];
        _noteBookShareVC.delegate = self;
    }
    return _noteBookShareVC;
}

-(void)userSelectType:(NSString *)typeName WithIndexPath:(NSIndexPath *)indexPath{
    self.noteBookShareVC.view.hidden = !self.noteBookShareVC.view.hidden;
    if ([typeName isEqualToString:@"发给微信好友"]) {
        
    }else if ([typeName isEqualToString:@"发到朋友圈"]){
        
    }else if ([typeName isEqualToString:@"发到微博"]){
        
    }else if ([typeName isEqualToString:@"发给QQ好友"]){
        
    }else if ([typeName isEqualToString:@"发到消息"]){
        [self returnToMessage];
    }else if ([typeName isEqualToString:@"发到小组"]){
        [self returnToGroup];
    }else if ([typeName isEqualToString:@"发到笔记"]){
        [self returnToNote];
    }else if ([typeName isEqualToString:@"发到话题"]){
        [self returnToTopic];
    }else{
    }
}

- (void)returnToMessage
{
    ShareListVC * vc = [ShareListVC initShareListVC];
    FlowAttachmentsModel * model = [[FlowAttachmentsModel alloc] init];
    model.SoureTypeId = CollectModel_meetDetail;
    model.Title = self.meetingModel.Name;
    model.SoureId = [self.meetingModel.Id integerValue];
    model.Author = self.meetingModel.UserName;
    vc.sendModel = model;
    vc.isReturn = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)returnToGroup
{
    MyTDGroupViewController * vc = [[MyTDGroupViewController alloc] init];
    FlowAttachmentsModel * model = [[FlowAttachmentsModel alloc] init];
    model.SoureTypeId = CollectModel_meetDetail;
    model.Title = self.meetingModel.Name;
    model.SoureId = [self.meetingModel.Id integerValue];
    model.Author = self.meetingModel.UserName;
    vc.type = CollectModel_Group;
    vc.sendModel = model;
    vc.isReturn = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)returnToNote
{
    MyTDTopicCreateVC * vc = [MyTDTopicCreateVC initMyTDTopicCreateVC];
    vc.type = CollectModel_NoteBook;
    FlowAttachmentsModel * model = [[FlowAttachmentsModel alloc] init];
    model.SoureTypeId = CollectModel_meetDetail;
    model.Title = self.meetingModel.Name;
    model.SoureId = [self.meetingModel.Id integerValue];
    model.Author = self.meetingModel.UserName;
    vc.sendModel = model;
    vc.isReturn = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)returnToTopic
{
    MyTDTopicCreateVC * vc = [MyTDTopicCreateVC initMyTDTopicCreateVC];
    FlowAttachmentsModel * model = [[FlowAttachmentsModel alloc] init];
    model.SoureTypeId = CollectModel_meetDetail;
    model.Title = self.meetingModel.Name;
    model.SoureId = [self.meetingModel.Id integerValue];
    model.Author = self.meetingModel.UserName;
    vc.sendModel = model;
    vc.type = CollectModel_NewTopic;
    vc.isReturn = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.detailsTabView.dataSource = self;
    self.detailsTabView.delegate = self;
    self.detailsTabView.tableFooterView = [UIView new];
    self.detailsTabView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.detailsTabView.separatorColor = [UIColor colorWithHexString:@"F7F7F7"];
    [self.detailsTabView registerNib:[UINib nibWithNibName:@"HistoricalWorkFlowDetailsTitleCell" bundle:nil] forCellReuseIdentifier:@"HistoricalWorkFlowDetailsTitleCell"];
    [self.detailsTabView registerNib:[UINib nibWithNibName:@"HistoricalWorkFlowDetailsContentCell" bundle:nil] forCellReuseIdentifier:@"HistoricalWorkFlowDetailsContentCell"];
    [self.detailsTabView registerNib:[UINib nibWithNibName:@"HistoricalWorkFlowDetailsMoreCell" bundle:nil] forCellReuseIdentifier:@"HistoricalWorkFlowDetailsMoreCell"];
    [self.detailsTabView registerNib:[UINib nibWithNibName:@"MeetingDetailsTimeCell" bundle:nil] forCellReuseIdentifier:@"MeetingDetailsTimeCell"];
    if (self.meetType == MeetingEndType) {
        self.bottomView.alpha = 0;
        self.bottomView.hidden = YES;
    }
    [self getData];
    [[UtilsTools getWindow] addSubview:self.noteBookShareVC.view];
}
- (void)setBottomStatus:(BOOL)hidden
{
    self.bottomView.hidden = hidden;
    if (hidden) {
        self.view.backgroundColor = [UIColor whiteColor];
        self.bottomConstant.constant = 0.f;
    }else{
        self.view.backgroundColor = [UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
        self.bottomConstant.constant = 50.f;
    }
}
- (void)getData
{
    WS(weakSelf);
    NSString * url = [NSString stringWithFormat:@"%@%ld",Host(MeetingDetail),(long)self.meetingId];
    [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} success:^(id  _Nonnull responseObject) {
        [weakSelf.detailsTabView.mj_header endRefreshing];
        [weakSelf.detailsTabView.mj_footer endRefreshing];
        weakSelf.meetingModel = [MyMeetingListModel modelWithDictionary:responseObject];
        if (weakSelf.meetingModel.Status == 0) {
            weakSelf.meetType = MeetingEndType;
        }else{
            weakSelf.meetType = MeetingWillOpenType;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([weakSelf.meetingModel.UserId integerValue] == [AppUserModel sharedAppUserModel].Id) {
                [weakSelf setBottomStatus:NO];
            }else{
                [weakSelf setBottomStatus:YES];
            }
            [weakSelf.detailsTabView reloadData];
        });
    } failure:^(NSError * _Nonnull error) {
        [weakSelf.detailsTabView.mj_header endRefreshing];
        [weakSelf.detailsTabView.mj_footer endRefreshing];
    }];
}
#pragma mark - table delegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        return self.meetingModel.MeetingTimes.count * 26.f + 18.f;
    }
//    if (indexPath.row == 2) {
//        NSString *address = [NSString stringWithFormat:@"%@ %@",self.meetingModel.Address,self.meetingModel.RoomName];
//        NSDictionary *txtAttribute =  @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
//        CGSize retSize = [address boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 100.f, MAXFLOAT)
//                                               options:\
//                          NSStringDrawingTruncatesLastVisibleLine |
//                          NSStringDrawingUsesLineFragmentOrigin |
//                          NSStringDrawingUsesFontLeading
//                                            attributes:txtAttribute
//                                               context:nil].size;
//        return retSize.height + 24.f;
//    }
    if (indexPath.row == 3) {
        //获取实际内容
        NSString *content = self.meetingModel.Description;
        //获取字符串高度
        UILabel * label = [UILabel new];
        label.font = [UIFont systemFontOfSize:16];
        label.text = content;
        label.numberOfLines = 0;
        CGFloat contentHeight = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH - 36, 10)].height;
        //图片应该展示的高度
        CGFloat imageHeight = self.meetingModel.ImageUrl.length > 0 ? (SCREEN_WIDTH - 30.f) * 0.5 : 0;
        CGFloat H = 48.f;
        if (self.meetingModel.ImageUrl.length > 0) {
            H += 15.f;
        }
        if (self.meetingModel.FlowAttachments.count > 0) {
            H += 15.f;
        }
        if (content.length > 0) {
            H += 15.f;
        }
        //添加其他高度
        return (contentHeight + H + imageHeight  + self.meetingModel.FlowAttachments.count * 87);
    }
    return UITableViewAutomaticDimension;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.meetType == MeetingEndType) {
        return 6;
    }
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf);
    switch (indexPath.row) {
        case 0:{
            HistoricalWorkFlowDetailsTitleCell * cell = (HistoricalWorkFlowDetailsTitleCell *)[tableView dequeueReusableCellWithIdentifier:@"HistoricalWorkFlowDetailsTitleCell" forIndexPath:indexPath];
            [cell.statusBtn setTitle:self.meetingModel.Status == 0 ? @"待开会" : @"已结束" forState:UIControlStateNormal];
            cell.statusBtn.hidden = self.meetType == MeetingEndType ? YES : NO;
            [cell setCellData:self.meetingModel withIndex:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 1:{
            MeetingDetailsTimeCell * cell = (MeetingDetailsTimeCell *)[tableView dequeueReusableCellWithIdentifier:@"MeetingDetailsTimeCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setCellData:self.meetingModel];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 2:{
            HistoricalWorkFlowDetailsTitleCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HistoricalWorkFlowDetailsTitleCell" forIndexPath:indexPath];
            [cell setCellData:self.meetingModel withIndex:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 3:{
            HistoricalWorkFlowDetailsContentCell * cell = (HistoricalWorkFlowDetailsContentCell *)[tableView dequeueReusableCellWithIdentifier:@"HistoricalWorkFlowDetailsContentCell" forIndexPath:indexPath];
            [cell setCellData:self.meetingModel withIndex:indexPath];
            cell.ClickAttrBlock = ^(NSDictionary * _Nonnull dict) {
                [weakSelf pushWithDict:dict];
            };
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 4:{
            HistoricalWorkFlowDetailsMoreCell * cell = (HistoricalWorkFlowDetailsMoreCell *)[tableView dequeueReusableCellWithIdentifier:@"HistoricalWorkFlowDetailsMoreCell" forIndexPath:indexPath];
            [cell setCellData:@"参会人员"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        default:{
            HistoricalWorkFlowDetailsMoreCell * cell = (HistoricalWorkFlowDetailsMoreCell *)[tableView dequeueReusableCellWithIdentifier:@"HistoricalWorkFlowDetailsMoreCell" forIndexPath:indexPath];
            [cell setCellData:@"会议纪要"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 4) {
        WorkFlowPersonnelListController *controller = [[WorkFlowPersonnelListController alloc] init];
        controller.Id = self.meetingModel.Id;
        [self.navigationController pushViewController:controller animated:YES];
    }
    if (indexPath.row == 5) {
        WorkFlowFecordController * vc = [[WorkFlowFecordController alloc] init];
        vc.meetId = [self.meetingModel.Id integerValue];
        vc.isBack = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)pushWithDict:(NSDictionary *)dict
{
    WS(weakSelf);
    NSLog(@"%@",dict);
    switch ([dict[@"SoureTypeId"] integerValue]){
        case CollectModel_Aritle:
        {
            SHOWLOADING
            [[HttpManager sharedHttpManager] GETWithUrl:[NSString stringWithFormat:@"%@%ld",Article_Detail_Get,(long)[dict[@"SoureId"] integerValue]] parameters:@{} success:^(id  _Nonnull responseObject) {
                REMOVESHOW;
                Item *itemModel = [[Item alloc]initWithDictionary:responseObject error:nil];
//                HomeDetailController *homeDetail = [[HomeDetailController alloc] init];
//                [homeDetail loadCurrentModel:itemModel];
//                [weakSelf.navigationController pushViewController:homeDetail animated:YES];
                MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
                vc.type = CollectModel_Aritle;
                vc.Id = itemModel.Id;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } failure:^(NSError * _Nonnull error) {
                SHOWERROR([ZYCTool handerResultData:error]);
            }];
        }
            break;
        case CollectModel_Toipc:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = CollectModel_Toipc;
            vc.Id = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_NewTopic:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = CollectModel_NewTopic;
            vc.Id = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_Collect:
        {
        }
            break;
        case CollectModel_NoteBook:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = CollectModel_NoteBook;
            vc.Id = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_Notice:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = CollectModel_Notice;
            vc.Id = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_InterMail:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = CollectModel_InterMail;
            vc.Id = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_Group:
        {
            TDGroupInfoListVC * vc = [TDGroupInfoListVC initTDGroupInfoListVC];
            vc.GroupId = [dict[@"SoureId"] integerValue];
            vc.titleStr = dict[@"Title"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_meetMian:
        {
            MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
            vc.type = CollectModel_meetMian;
            vc.Id = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case CollectModel_work:{
            MyApprovalPageDetail * vc = [[MyApprovalPageDetail alloc]init];
            vc.pageType = Type_ONEC;
            vc.indexId = [dict[@"SoureId"] integerValue];
            vc.title = dict[@"Title"];
            vc.TodoId = [dict[@"SoureId"] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        default:
        {
            FlowAttachmentsModel * model = [FlowAttachmentsModel modelWithDictionary:dict];
            DownFileViewController *vc = [[DownFileViewController alloc]init];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }

}
#pragma mark - cancelMeeting
- (IBAction)cancelMeetingBtn_Click:(id)sender {
    WS(weakSelf);
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:@"确认预约会议" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [ok setValue:k9 forKey:@"_titleTextColor"];
    UIAlertAction *noOk = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        SHOWLOADING
        [[HttpManager sharedHttpManager] POSTWithUrl:CancelMeeting(self.meetingModel.Id) parameters:@{} success:^(id  _Nonnull responseObject) {
            REMOVESHOW
            SHOWSUCCESS(@"已取消预约会议");
            if (weakSelf.cancelSussBlock) {
                weakSelf.cancelSussBlock();
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } failure:^(NSError * _Nonnull error) {
            REMOVESHOW
            SHOWERROR([ZYCTool handerResultData:error]);
        }];
    }];
    [alertVC addAction:ok];
    [alertVC addAction:noOk];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (IBAction)editMeetingBtn_Click:(id)sender {
    WS(weakSelf);
    WorkFlowOrderController *workFlowOrderController = [[WorkFlowOrderController alloc] initWithRID:[NSString stringWithFormat:@"%ld",(long)self.meetingId]];
    workFlowOrderController.mettingTitle = self.meetingModel.Name;
    workFlowOrderController.meetingModel = self.meetingModel;
    workFlowOrderController.isEdit = YES;
    workFlowOrderController.ChangeSuccessBlock = ^{
        [weakSelf getData];
    };
    [self.navigationController pushViewController:workFlowOrderController animated:YES];
}

@end