//
//  HomeCollectionViewController.m
//  ALand
//
//  Created by Z on 16/3/22.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "HomeCollectionViewController.h"
#import "MoneyView.h"
#import "HomeCollectionViewCell.h"
#import "CommodityListViewController.h"
#import "AddCommodityViewController.h"
#import "SDCycleScrollView.h"
#import "CommodityManagementViewController.h"
#import "OrderViewController.h"
#import "StoreManagermentViewController.h"
#import "MyTeamViewController.h"
#import "CustomerManagementViewController.h"
#import "InvitePartnerViewController.h"
#import "JavenDetailReseller.h"
#import "JavencommissionAccountModel.h"
#import "MyAcountViewController.h"
#import "HomeCollectionFooterReusableView.h"
#import "ShopWebViewController.h"

#define kHomeCollectionViewID @"HomeCollectionViewID"
#define kCollectionViewHeadHeight 230

@interface HomeCollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,SDCycleScrollViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) MoneyView *viewMoney;
@property (nonatomic, strong) NSMutableArray *arrImages;
@property (nonatomic, strong) NSArray *arrTitles;

@property (nonatomic, strong) JavenDetailReseller *reseller;
@property (nonatomic, strong) JavencommissionAccountModel *accountModel;
@end

@implementation HomeCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _arrImages = [NSMutableArray array];
    for (int i = 0;  i < 8; i++) {
        NSString *imageName = [NSString stringWithFormat:@"homeCollection%d",i];
        [_arrImages addObject:imageName];
    }
    
    self.arrTitles = @[@"商品管理",@"订单管理",@"客户管理",@"我的团队",@"店铺设置",@"邀请合伙人",@"代客下单",@"个人中心"];
    
    //添加collectionview
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    layout.itemSize = CGSizeMake((ScreenSize.width - 3) / 4, (ScreenSize.width - 3) / 4 + 20);
    layout.footerReferenceSize = CGSizeMake(kWidth, 150);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    _collectionView.backgroundColor = kBacroundColor;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    _collectionView.contentInset = UIEdgeInsetsMake(kCollectionViewHeadHeight, 0, 0, 0);
    
    //注册cell
    [_collectionView registerNib:[UINib nibWithNibName:@"HomeCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomeCollectionViewID];
    [_collectionView registerClass:[HomeCollectionFooterReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

    [self.view addSubview:_collectionView];
    
    _collectionView.sd_layout.topEqualToView(self.view).leftEqualToView(self.view).rightEqualToView(self.view).bottomSpaceToView(self.view, 49);
    [_collectionView updateLayout];
    
    
    //中间的收入
    MoneyView *viewMoney = [MoneyView viewWithNibName:@"MoneyView"];
    self.viewMoney = viewMoney;
    viewMoney.frame = CGRectMake(0, 0 - kCollectionViewHeadHeight, ScreenSize.width, 310);
    [self.collectionView addSubview:viewMoney];
    
    kButtonAddAction(self.viewMoney.buttonGoAmount, actionGoAcount);
    
    //添加商品按钮
    UIView *viewAdd = [[UIView alloc] initWithFrame:CGRectMake(0, -51, ScreenSize.width, 50)];
    viewAdd.backgroundColor = [UIColor whiteColor];
    [self.collectionView addSubview:viewAdd];
    viewAdd.sd_layout.topSpaceToView(viewMoney, 5);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [viewAdd addSubview:button];
    [button addTarget:self action:@selector(touchAddAction) forControlEvents:UIControlEventTouchUpInside];
    button.sd_layout.centerXEqualToView(viewAdd).centerYEqualToView(viewAdd).widthIs(100).heightIs(23);
    [button setImage:[UIImage imageNamed:@"addbutton"] forState:UIControlStateNormal];
    
    //轮播图
    //轮播网络图片
//    SDCycleScrollView *scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, 0, 0) delegate:self placeholderImage:[UIImage imageNamed:@"cyclescrollplaceholder"]];
//    scrollView.imageURLStringsGroup = @[@"0",@"0"];
    
    //轮播本地图片
//    SDCycleScrollView *scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, self.collectionView.height - 315, kWidth, 120) imageNamesGroup:@[@"cyclescrollplaceholder",@"cyclescrollplaceholder"]];
//    
//    
//    scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
//    scrollView.autoScrollTimeInterval = 3;
//    [self.collectionView addSubview:scrollView];

    

    // Do any additional setup after loading the view.
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    

    HomeCollectionFooterReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
    return footer;

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    WS(weakSelf)
    NSString *strGetResseller = [NSString stringWithFormat:@"commission/getAccount/%@", [UserInfo shareInstance].uuid];
    [[HTTPCilent shareCilent] GET:strGetResseller parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@0]) {

            weakSelf.accountModel = [JavencommissionAccountModel modelObjectWithDictionary:responseObject[@"commissionAccount"]];
            [weakSelf.viewMoney setMoneyView:weakSelf.accountModel];
        }
        
        CLog(@"d");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)setMoneyView {
    
}

#pragma mark - action

- (void)touchAddAction {
    CLog(@"添加商品");
    AddCommodityViewController *addVC = [[AddCommodityViewController alloc] init];
    addVC.hidesBottomBarWhenPushed = YES;
    addVC.isShowNavigationBar = YES;
    [self.navigationController pushViewController:addVC animated:YES];

}


- (void)actionGoAcount {
    MyAcountViewController *acountVC = [[MyAcountViewController alloc] init];
    acountVC.isShowNavigationBar = NO;
    acountVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:acountVC animated:YES];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrImages.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomeCollectionViewID forIndexPath:indexPath];
    cell.imgView.image = [UIImage imageNamed:self.arrImages[indexPath.row]];
    cell.labelTitle.text = self.arrTitles[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 0://商品管理
        {
            CommodityManagementViewController *managementVC = [[CommodityManagementViewController alloc] init];
            managementVC.hidesBottomBarWhenPushed = YES;
            managementVC.isShowNavigationBar = YES;
            [self.navigationController pushViewController:managementVC animated:YES];
            
        }
            break;
        case 1://订单管理
        {
            OrderViewController *orderVC = [[OrderViewController alloc] init];
            orderVC.hidesBottomBarWhenPushed = YES;
            orderVC.isShowNavigationBar = YES;
            [self.navigationController pushViewController:orderVC animated:YES];
        }
            
            break;
        case 2://客户管理
        {
            CustomerManagementViewController *customerVC = [[CustomerManagementViewController alloc] init];
            customerVC.isShowNavigationBar = YES;
            customerVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:customerVC animated:YES];
        }
            
            break;
        case 3://我的团队
        {
            MyTeamViewController *myTeamVC = [[MyTeamViewController alloc] init];
            myTeamVC.isShowNavigationBar = YES;
            myTeamVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:myTeamVC animated:YES];
        
        }
            break;
        case 4://店铺设置
        {
            StoreManagermentViewController *storeVC = [[StoreManagermentViewController alloc] initWithNibName:@"StoreManagermentViewController" bundle:[NSBundle mainBundle]];
            storeVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:storeVC animated:YES];
            
        }
            
            break;
        case 5://邀请合伙人
        {
            InvitePartnerViewController *inviteVC = [[InvitePartnerViewController alloc] init];
            inviteVC.isShowNavigationBar = YES;
            inviteVC.title = @"邀请合伙人";
            inviteVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:inviteVC animated:YES];
        }
            
            break;
        case 6://课堂
        {
            ShopWebViewController *shopVC = [[ShopWebViewController alloc] init];
            shopVC.isShowNavigationBar = YES;
            shopVC.title = @"微商城";
            shopVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:shopVC animated:YES];
        }
            
            break;
        case 7://榜
        {
            
            [self.tabBarController setSelectedIndex:self.tabBarController.viewControllers.count - 1];
        }
            
            break;
            
        default:
            break;
    }
}

- (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
