CardBeenUseViewController.m 7.38 KB
//
//  CardBeenUseViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/10/19.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CardBeenUseViewController.h"
#import "CardCollectionViewCell.h"
#import "CardOrderInformationReusableView.h"
#import "CardAmplificationViewController.h"

@interface CardBeenUseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic,strong) RsJingDongECardRequest *requestModel;
@property (nonatomic,strong) WYPopoverController *settingsPopoverController;

/**
 *  数据源
 */
@property (nonatomic,strong) NSMutableArray *datasArray;

@end

@implementation CardBeenUseViewController

#pragma mark - lazy
- (RsJingDongECardRequest *)requestModel
{
    if (!_requestModel) {
        _requestModel = [[RsJingDongECardRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
        page.page = ZERO;
        page.rows = KROWS;
        page.order = @"desc";
        _requestModel.guideIdEquals = [Shoppersmanager manager].shoppers.employee.fid;
        _requestModel.stateEquals = self.cardState;
        _requestModel.page = page;
    }
    return _requestModel;
}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
    self.tempIndex = @"0";
}

#pragma mark -获取数据
- (void)loadWebDataSource
{
    WS(weakSelf);
    if (self.pullPageIndex == ZERO) {
        [self.datasArray removeAllObjects];
    }
    self.requestModel.page.page = self.pullPageIndex;
    [self getCardDatasActionCompleted:^(RsJingDongECardResponse *result) {
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
        }
    }];
}


#pragma mark - 获取卡劵
- (void)getCardDatasActionCompleted:(void(^)(RsJingDongECardResponse *result))completed
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(CARDINFORMATION)  withRequestType:ZERO withParameter:self.requestModel withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            RsJingDongECardResponse *cardInformation = [[RsJingDongECardResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(cardInformation);
            [weakSelf.datasArray addObjectsFromArray:cardInformation.list];
            [weakSelf.collectionView reloadData];
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [weakSelf endRefresh:EndRefreshDefault];
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


#pragma mark - 使用京东卡劵
- (void)useJDECard:(NSString *)JDECardNumber
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(USEJDECARD),JDECardNumber]  withRequestType:ZERO withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [weakSelf deleteUsedJDECard:JDECardNumber];
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


#pragma mark - 区分已经使用的E卡
- (void)deleteUsedJDECard:(NSString *)eCardNumber
{
    //@property (nonatomic, strong) NSMutableArray<TOJingdongEcardEntity> *eCards;
    // 手动改为可变数组
    for (int i=0; i<self.datasArray.count; i++) {
        GroupByOrderNumber *order = self.datasArray[i];
        for (int j=0; j<order.eCards.count; j++) {
            TOJingdongEcardEntity *ecard = order.eCards[j];
            if ([eCardNumber isEqualToString:ecard.cardNumber]) {
                ecard.state = USED;
                [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:j inSection:i]]];
            }
        }
    }
}

#pragma mark - UICollectionView
- (void)setupCollectionView
{
    self.beenUseLayout.itemSize = CGSizeMake((ScreenWidth-48-20*4)/3, 200);
    self.beenUseLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.beenUseLayout.minimumLineSpacing = 20;
    self.beenUseLayout.minimumInteritemSpacing = 20;
    [self.collectionView registerNib:[UINib nibWithNibName:@"CardOrderInformationReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView"];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CardCollectionViewCell" forIndexPath:indexPath];
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cell.Cardmodel = model.eCards[indexPath.row];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    GroupByOrderNumber *model = self.datasArray[section];
    return model.eCards.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CardOrderInformationReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView" forIndexPath:indexPath];
    headerView.model = self.datasArray[indexPath.section];
    headerView.uploadReceiptsButton.hidden = true;
    return headerView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0, 80);
}

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.cardState isEqualToString:CHECK]) {
        return;
    }
    CardAmplificationViewController *cardVC = [[CardAmplificationViewController alloc]init];
    self.settingsPopoverController = [[WYPopoverController alloc] initWithContentViewController:cardVC];
    self.settingsPopoverController.theme.fillBottomColor = [UIColor clearColor];
    self.settingsPopoverController.theme.fillTopColor = [UIColor clearColor];
    [self.settingsPopoverController presentPopoverAsDialogAnimated:YES
                                                           options:WYPopoverAnimationOptionFadeWithScale];
    [self.settingsPopoverController beginThemeUpdates];
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cardVC.cardModel = model.eCards[indexPath.row];
    cardVC.preferredContentSize = CGSizeMake(500, 370);
    [self.settingsPopoverController endThemeUpdates];
    // 更新E卡状态
    WS(weakSelf);
    [cardVC setRefreshJDCardList:^(NSString *cardNumber) {
        [weakSelf.settingsPopoverController dismissPopoverAnimated:YES completion:^{
            [weakSelf useJDECard:cardNumber];
        }];
    }];
}


- (void)dealloc
{
    [Notification removeObserver:self];
}


@end