//
//  MyTDTopicUserMoveVC.m
//  smartRhino
//
//  Created by niuzhen on 2019/12/20.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "MyTDTopicUserMoveVC.h"
#import "TopicGroupManageModel.h"
#import "TopicGroupManageCell.h"
#import "MyTDGroupView.h"
#import "ShowNewGroupAlert.h"

@interface MyTDTopicUserMoveVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel  *titleL;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (nonatomic, strong) NSMutableArray     *dataArray;
@end

@implementation MyTDTopicUserMoveVC
+(MyTDTopicUserMoveVC *)initMyTDTopicUserMoveVC;
{
    MyTDTopicUserMoveVC *controller = [StoryboardManager.shared.myTDTopicExtent instantiateViewControllerWithIdentifier:@"MyTDTopicUserMoveVC"];
     return controller;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.titleL.text = @"移动到";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self getData];
    WS(weakSelf);
    [self.addBtn setAction:^{
        [[ShowNewGroupAlert initShowNewGroupAlertWithTitle:@"新建分组" placeholder:@"请输入分组名称" confirm:^(NSString * _Nonnull groupName) {
            NSLog(@"%@",groupName);
            NSDictionary * paraDict = @{@"Name":groupName,
                                        @"UserId":@([AppUserModel sharedAppUserModel].Id),
                                        @"Id":@(0)
            };
            [[HttpManager sharedHttpManager] PUTUrl:Host(APP_Topic_Add_Group) parameters:paraDict success:^(id  _Nonnull responseObject) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf getData];
                });
            } failure:^(NSError * _Nonnull error) {
                NSLog(@"%@",error);
                SHOWERROR([ZYCTool handerResultData:error])
            }];
        } cancle:^{
        }] show];
    }];
}
- (void)getData
{
    [self.dataArray removeAllObjects];
    WS(weakSelf);
    [[HttpManager sharedHttpManager] GETUrl:Host(APP_Topic_List_Group) parameters:@{} responseStyle:JOSN success:^(id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
        NSArray * array = responseObject;
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary * dict = (NSDictionary *)obj;
            TopicGroupManageModel * model = [TopicGroupManageModel modelWithDictionary:dict];
            [weakSelf.dataArray addObject:model];
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 50.f;
    }
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        MyTDGroupView * groupView = [[MyTDGroupView alloc] init];
        [view addSubview:groupView];
        view.backgroundColor = UIColorHex(FFFFFF);
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushSearchVC)];
        [view addGestureRecognizer:tap];
        view.userInteractionEnabled = YES;
        return view;
    }else{
        return [UIView new];
    }
}
- (void)pushSearchVC
{

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicGroupManageModel * model = [self.dataArray objectAtIndex:indexPath.row];
    TopicGroupManageCell * cell = [TopicGroupManageCell configCell0:tableView indexPath:indexPath];
    cell.countL.hidden = YES;
    cell.rightImgV.hidden = YES;
    cell.nameL.text = model.Name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    TopicGroupManageModel * model = [self.dataArray objectAtIndex:indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self moveToindex:model.Id];
}
- (void)moveToindex:(NSInteger)toId
{
    WS(weakSelf);
    SHOWLOADING
    [[HttpManager sharedHttpManager] PUTUrl:Host(APP_Topic_Move) parameters:@{@"TopicGroupId":@(toId),@"Ids":self.selectIdArray} responseStyle:DATA success:^(id  _Nonnull responseObject) {
        REMOVESHOW
        SHOWSUCCESS(@"操作成功");
        for ( int i = 0; i< weakSelf.navigationController.viewControllers.count; i++) {
            if ([weakSelf.navigationController.viewControllers[i] isKindOfClass:NSClassFromString(@"MyTDTopicGroupManageVC")]) {
                [weakSelf.navigationController popToViewController:self.navigationController.viewControllers[i] animated:YES];
            }
        }
    } failure:^(NSError * _Nonnull error) {
        REMOVESHOW
        SHOWERROR([ZYCTool handerResultData:error])
    }];
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end