//
//  HomeMingShiVC.m
//  smartRhino
//
//  Created by niuzhen on 2020/6/23.
//  Copyright © 2020 tederen. All rights reserved.
//

#import "HomeMingShiVC.h"
#import "HomeSubModel.h"
#import "HomeSubItemModel.h"
#import "HomeTeacherCell.h"
#import "MoveViewController.h"
#import "BookTeacherDetailVC.h"

@interface HomeMingShiVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView     * tableView;
@property (nonatomic, strong) NSMutableArray  * dataSource;
@end

@implementation HomeMingShiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self getData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectSuccess) name:COLLECTSUCCESS object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setHeight:(CGFloat)height
{
    [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height - 45)];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)getData
{
    WS(weakSelf);
    [weakSelf.dataSource removeAllObjects];
    [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_PAGE_Teacher) parameters:@{@"StyleCss":self.style,@"Page":@(1),@"PerPage":@20} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
                HomeSubModel *model = [HomeSubModel modelWithDictionary:responseObject];
            for (NSDictionary * dict in model.Items) {
                HomeSubItemModel *smodel = [HomeSubItemModel modelWithDictionary:dict];
                [weakSelf.dataSource addObject:smodel];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(weakSelf);
    HomeSubItemModel *model = self.dataSource[indexPath.row];
    HomeTeacherCell * cell = [HomeTeacherCell configCell:tableView indexPath:indexPath];
    [cell setDataWithModel:model];
    cell.ClickCollectBlock = ^(NSInteger Id) {
        if (model.IsCollect) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"真的取消收藏吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                    [dic setValue:@(Id) forKey:@"CollectionDataId"];
                    ///     1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8 会议详情 14工作流审批 300 文件 400 会议纪要
                    [dic setValue:@(CollectModel_Teacher) forKey:@"CollectionType"];
                    [dic setValue:@(0) forKey:@"FolderId"];
                    [dic setValue:@(0) forKey:@"SourceUserId"];
                    [[HttpManager sharedHttpManager] PUTUrl:Host(API_CreateCollect) parameters:dic success:^(id  _Nonnull responseObject) {
                        model.IsCollect = NO;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                        });
                    } failure:^(NSError * _Nonnull error) {
                        //                    SHOWERROR([ZYCTool handerResultData:error]);
                    }];
                }];
                UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:actionYes];
                [alert addAction:actionNo];
                [weakSelf presentViewController:alert animated:YES completion:^{
                    
                }];
            });
        }else{
            [weakSelf gotoCollectHander:Id];
        }
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeSubItemModel * model = self.dataSource[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BookTeacherDetailVC * vc = [BookTeacherDetailVC initBookTeacherDetailVC];
    vc.Id = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoCollectHander:(NSInteger)Id{
    MoveViewController *vc = [MoveViewController initMoveViewController];
    vc.CollectionDataId = Id;
    vc.collectType = CollectHanderType_Collect;
    vc.CollectionType = CollectModel_Teacher;
    vc.ParentId = 0;
    vc.titleStr = @"我的收藏";
    vc.TypeId = CreateCollectionType;
    vc.FolderIds = @[].mutableCopy;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)collectSuccess
{
    [self getData];
}

@end