//
//  MyTeamViewController.m
//  ALand
//
//  Created by Z on 16/4/15.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "MyTeamViewController.h"
#import "MyTeamCollectionViewCell.h"
#import "MyteamTopView.h"
#import "JavenResellerModel.h"

#define kItemId @"MyTeamCollectionViewCell.h"
@interface MyTeamViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrData;
@property (nonatomic, strong) MyteamTopView *topView;

@end

@implementation MyTeamViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrData = [NSMutableArray array];
    self.navigationItem.title = @"我的团队";
    [self setUpCollectionView];
    [self setUpTotalData];
    [self setUpData];
    // Do any additional setup after loading the view.
}

- (void)setUpCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake(kWidth / 3, kAutoValue(200));
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:layout];
    self.collectionView.contentInset = UIEdgeInsetsMake(140, 0, 0, 0);
    self.collectionView.backgroundColor = kBacroundColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"MyTeamCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kItemId];
    [self.view addSubview:self.collectionView];
    
    [self setUpTopView];
}

- (void)setUpTotalData {
    WS(weakSelf)
    [[HTTPCilent shareCilent] GET:[NSString stringWithFormat:@"reseller/team/getTeamInfo/%@", [UserInfo shareInstance].reseller.uuid] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSNumber *teamNums = responseObject[@"memberCount"];
        NSNumber *bonus = responseObject[@"commissionTotal"];
        
        [weakSelf.topView setTeamNums:teamNums bonus:bonus];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


- (void)setUpData {
    
    
    NSDictionary *params = @{@"defintion" : @{@"conditions" : @[@{@"operation" : @"reseller uuid equals",
                                                                  @"parameters" : @[[UserInfo shareInstance].reseller.uuid]},
                                                                @{@"operation" : @"domain equals",
                                                                  @"parameters" : @[kDomain]}],
                                              @"orders" : @[@{@"direction" : @"desc",
                                                              @"field" : @""}],
                                              @"page" : @0,
                                              @"pageSize" : @999,
                                              @"probePages" : @0},
                             @"fetchParts" : [NSNull null]};
    
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"reseller/team/query" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@0]) {
            for (NSDictionary *dic in responseObject[@"queryResult"]) {
                JavenResellerModel *model = [JavenResellerModel modelObjectWithDictionary:dic];
                [weakSelf.arrData addObject:model];
            }
            [weakSelf.collectionView reloadData];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


- (void)setUpTopView {
    MyteamTopView *topView = [MyteamTopView viewWithNibName:@"MyTeamTopView"];
    self.topView = topView;
    topView.frame = CGRectMake(0, -140 + 20, kWidth, 100);
    [self.collectionView addSubview:topView];
}


#pragma mark =========== collection delegate ===========
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyTeamCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kItemId forIndexPath:indexPath];
    [cell cellWithModel:self.arrData[indexPath.row]];
    return cell;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"myTeam"];//("PageOne"为页面名称，可自定义)
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"myTeam"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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