KWMShippingVC.m 3.78 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
//
//  KWMShippingVC.m
//  iCemarose
//
//  Created by HouWeiBin on 2016/11/4.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMShippingVC.h"
#import "KWMShippingCell.h"

@implementation KWMShippingVC

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitle];
    //獲取配送方式數據
    [self requestShippingData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

+(NSString *)kwmTag{
    return @"KWMShippingVC";
}

- (void)initTitle{
    self.title = @"配送方式";
}

-(void)requestShippingData{
    if(self.checkout==nil){
        return;
    }
    [self showLoading];
    [self.client getShippingRatesForCheckoutWithToken:self.checkout.token completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) {
        [self hideLoading];
        if (shippingRates!=nil && [shippingRates count] > 0 && !error) {
            self.shippingRates = shippingRates;
            [self.tbvShipping reloadData];
        }
        else {
            [self showError:error];
            NSLog(@"Failed to retrieve shipping rates: %@", error);
        }
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    }];
}

//设置配送费到订单
-(void)setShippingToCheckout:(BUYShippingRate *)shippingRate{
    if(shippingRate == nil){
        return;
    }
    [self showLoading];
    [self.checkout setShippingRate:shippingRate];
    [self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
        [self hideLoading];
        if (error == nil && checkout) {
            [self showToast:@"设置配送方式成功"];
            if(self.delegate!=nil){
                [self.delegate kwm_selectShippingRate:checkout];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            [self showError:error];
            [self showToast:@"设置配送方式失败"];
            NSLog(@"Error applying checkout: %@", error);
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.shippingRates!=nil){
        return self.shippingRates.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMShippingCell";
    KWMShippingCell *shippingCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!shippingCell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        shippingCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    if(self.shippingRates!=nil){
        BUYShippingRate *shippingRate =[self.shippingRates objectAtIndex:indexPath.row];
        [shippingCell setData:shippingRate];
        [shippingCell setSelected:NO];
        if(shippingRate!=nil && self.checkout!=nil && self.checkout.shippingRate!=nil){
            NSString *identity1 = shippingRate.shippingRateIdentifier;
            NSString *identity2 = self.checkout.shippingRate.shippingRateIdentifier;

            if(identity1!=nil && identity2!=nil && [identity1 isEqualToString:identity2]){
                [shippingCell setShipSelected:YES];
            }
        }
    }
    
    return shippingCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.shippingRates!=nil){
        BUYShippingRate *shippingRate =[self.shippingRates objectAtIndex:indexPath.row];
        if(shippingRate){
            [self setShippingToCheckout:shippingRate];
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
}



@end