//
//  BusinessCollectionViewController.m
//  RealEstateManagement
//
//  Created by Z on 16/7/29.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "BusinessCollectionViewController.h"
#import "BusinessFunctionModel.h"
#import "SaleInputListViewController.h"
@interface BusinessCollectionViewController ()

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrFunction;
@end

@implementation BusinessCollectionViewController

static NSString *const reuseIdentifier = @"Cell";

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBarController.title = @"业务";
    self.layout.itemSize = CGSizeMake((kWidth - 5 * 10) / 4, (kWidth - 5 * 10) / 4 + 25);
    self.layout.minimumLineSpacing = 10;
    self.layout.minimumInteritemSpacing = 10;
    self.layout.headerReferenceSize = CGSizeMake(kWidth, 40);
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"BusinessItems" ofType:@"plist"];
    NSArray *arrData = [[NSArray alloc] initWithContentsOfFile:plistPath];
    CLog(@"%@", arrData);
    self.arrFunction = [NSMutableArray array];
    for (NSDictionary *dic in arrData) {
        
        //增加菜单需要在BusinessFunctionModel里面增加枚举项
#ifdef DEBUG
        //              没有权限控制
        //        BusinessFunctionModel *model = [BusinessFunctionModel modelObjectWithDictionary:dic];
        //
        //              权限控制
        BusinessFunctionModel *model = [[BusinessFunctionModel alloc] initWithDictionaryPermissionControl:dic];
#else
        BusinessFunctionModel *model = [[BusinessFunctionModel alloc] initWithDictionaryPermissionControl:dic];
#endif
        
        
        if (model.function.count > 0) {
            [self.arrFunction addObject:model];
        }
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *header = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"businessHeader" forIndexPath:indexPath];
    }
    
    UILabel *title = (UILabel *)[header viewWithTag:110];
    UIImageView *image = (UIImageView *)[header viewWithTag:111];
    [image.image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 39, 0, 40) resizingMode:UIImageResizingModeTile];
    NSString *txt = [self.arrFunction[indexPath.section] headerTitle];
    title.text = txt;
    
    return header;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.arrFunction.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    BusinessFunctionModel *model = self.arrFunction[section];
    return model.function.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    BusinessFunctionModel *model = self.arrFunction[indexPath.section];
    BusinessFunction *founction = model.function[indexPath.row];
    UILabel *title = (UILabel *)[cell viewWithTag:110];
    title.text = founction.functionName;
    UIImageView *img = (UIImageView *)[cell viewWithTag:111];
    img.image = [UIImage imageNamed:founction.functionPic];
    // Configure the cell
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BusinessFunctionModel *functions = self.arrFunction[indexPath.section];
    BusinessFunction *function = functions.function[indexPath.row];
    switch (function.type) {
        case functionTypeSaleInput:
        {
            SaleInputListViewController *saleVC = [SaleInputListViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_SALEINPUT];
            [self.navigationController pushViewController:saleVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

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


#pragma mark <UICollectionViewDelegate>

/*
 // Uncomment this method to specify if the specified item should be highlighted during tracking
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 // Uncomment this method to specify if the specified item should be selected
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
 return NO;
 }
 
 - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
 return NO;
 }
 
 - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
 
 }
 */

@end
