//
//  ForumViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/11/24.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ForumViewController.h"
#import "ForumTableViewCell.h"
#import "ForumItemListViewController.h"


@interface ForumViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ForumCategoryResponse *category;


@end

@implementation ForumViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpForumTableView];
}

#pragma mark - UITableView
- (void)setUpForumTableView
{
    self.tableView.rowHeight = 90;
    [Notification addObserver:self selector:@selector(refreshForumList:) name:REFRESH_FROUMLIST object:nil];
}

#pragma mark -选中时间轴刷新列表
- (void)refreshForumList:(NSNotification *)object
{
    self.typeId = object.object;
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 加载数据
- (void)loadWebDataSource
{
    WS(weakSelf);
    [self getForumAllTypeActionCompleted:^(ForumCategoryResponse *result) {
        [weakSelf endRefresh:EndRefreshNotData];
    }];
}

#pragma mark - 获取论坛项Type
- (void)getForumAllTypeActionCompleted:(void(^)(ForumCategoryResponse *result))completed
{
    WS(weakSelf);
    NSString *urlString = [NSString stringWithFormat:SERVERREQUESTURL(FORUMCATEGORYS),self.typeId,[Shoppersmanager manager].shoppers.employee.fid];
    [HTTP networkWithDictionaryRequestWithURL:urlString withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            weakSelf.category = [[ForumCategoryResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(weakSelf.category);
            [weakSelf.tableView reloadData];
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    } withFailureBlock:^(NSError *error) {
        [weakSelf endRefresh:EndRefreshDefault];
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumTableViewCell *forumCell = [tableView dequeueReusableCellWithIdentifier:@"ForumTableViewCell" forIndexPath:indexPath];
    forumCell.categoryEntity = self.category.categories[indexPath.row];
    return forumCell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.category.categories.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumItemListViewController  *itemListVc = [ForumItemListViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_LEARNINGCENTER];
    TOForumCategoryEntity *category = self.category.categories[indexPath.row];
    category.typeId = self.typeId;
    itemListVc.category = category;
    itemListVc.isPosting = [category.name isEqualToString:@"欧普问问"]?0:1;
    [self pushViewController:itemListVc animated:YES];
}



@end
