CNLiveApplyRefundController.m 13.4 KB
//
//  CNLiveApplyRefundController.m
//  CNLiveMyOrderModule
//
//  Created by 殷巧娟 on 2019/11/27.
//

#import "CNLiveApplyRefundController.h"
#import "CNLiveMineOrderView.h"
#import "CNLiveSelectedRefundReasonView.h"
#import "CNLiveUploadVoucher.h"
#import "CNLiveOrderDetailContentView.h"
#import "CNLiveRefundBasicCell.h"
#import "CNLiveBaseKit.h"
#import "CNLiveOrderList.h"
#import "UIColor+CNLiveExtension.h"
#import "Masonry.h"
#import "UIImage+CNLiveAdd.h"
#import "HUDHelper.h"
#import "CNLiveNetworking.h"
#import "CNUserInfoManager.h"
#import "CNLiveEnvironment.h"
#import "AFNetworking.h"
#import "CNLiveOrderHeader.h"
@interface CNLiveApplyRefundController ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource,CNLiveUploadVoucherHeightDelegate>
@property (nonatomic, strong)   NSArray                     *images;
@property (nonatomic, strong)   CNLiveOrderListOrderItem    *orderItem;
@property (nonatomic, strong)   UITableView                 *tableView;
@property (nonatomic, assign)   CGFloat                     height;  //最后一个分组的高度
@property (nonatomic, strong)   UIButton                    *submitBtn; //提交按钮
@property (nonatomic, strong)   UIView                      *footerView; //tableView的footerView
@property (nonatomic, copy)     NSString                    *reasonStr; //退款原因
@property (nonatomic, copy)     NSString                    *refundAmount; //退款金额
@property (nonatomic, copy)     NSString                    *refundInstruct; //退款说明
@property (nonatomic, assign)   CGFloat                     totalPrice; //总价
@property (nonatomic, strong)   NSArray                     *selectedPhotos; //选择图片
@end

@implementation CNLiveApplyRefundController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请退款";
    [self.view addSubview:self.tableView];
    self.view.userInteractionEnabled = YES;
    self.tableView.tableFooterView = self.footerView;
    self.tableView.tableFooterView.height = 75;
    self.height = (KScreenWidth - 20 - 2*8 - 4)/3 - 8 + 55;
}
- (void)setList:(CNLiveOrderList *)list {
    _list = list;
    self.totalPrice = list.orderItem.totalPrice;
}
#pragma mark - 更新图片数量代理
- (void)updateSelectedPhotos:(NSArray *)selectedPhotos {
    self.selectedPhotos = selectedPhotos;
    CGFloat margin = 8;
    CGFloat itemH = (KScreenWidth - 20 - 2*margin - 4)/3 - margin;
    CGFloat collectionH = 0;
    NSInteger column = 3; //列
    NSInteger row = 0; //行
    NSInteger isSelectPhoto = 0;
    if (selectedPhotos.count > 0) {
        if (selectedPhotos.count == 9) {
            isSelectPhoto = 0;
        }else {
            isSelectPhoto = 1;
        }
    }else {
        isSelectPhoto = 1;
    }
    if ((selectedPhotos.count + isSelectPhoto) %column <=0) {
        row = (selectedPhotos.count + isSelectPhoto) /column;
    }else {
        row = (selectedPhotos.count + isSelectPhoto ) /column + 1;
    }
    collectionH = 2*margin + (row - 1) *8 + row*itemH + 55;
    self.height = collectionH;
    [self.tableView reloadData];
    
}
#pragma mark - TargetAction
- (void)submitBtnAction {
    if (CNLiveStringIsEmpty(self.reasonStr)) {
        [[HUDHelper sharedInstance] tipMessage:@"退款原因不能为空"];
        return;
    }
    if (CNLiveStringIsEmpty(self.refundAmount)) {
        [[HUDHelper sharedInstance]tipMessage:@"退款金额不能为空"];
        return;
    }
    if ([self.refundAmount floatValue] <= 0) {
        [[HUDHelper sharedInstance]tipMessage:@"退款金额要大于0"];
        return;
    }
    NSNumber *totalPriceNumber = [NSNumber numberWithFloat:self.totalPrice];
    NSNumber *refundPriceNumber = [NSNumber numberWithFloat:[self.refundAmount floatValue]];
    if ([refundPriceNumber compare:totalPriceNumber] == NSOrderedDescending) {
        [[HUDHelper sharedInstance]tipMessage:@"退款金额不能大于总金额"];
        return;
    }
    if (![CNLiveNetworking isNetworking]) {
        [[HUDHelper sharedInstance]tipMessage:@"网络已断开"];
        return;
    }
    NSDictionary *param = @{
                            @"sid":@"10511059",
                            @"appId":AppId,
                            @"orderId":self.list.orderItem.orderId,
                            @"note":CNLiveStringIsEmpty(self.refundInstruct) ? @"":self.refundInstruct,
                            @"refundSum":CNLiveStringIsEmpty(self.refundAmount)? @"":self.refundAmount,
                            @"cause":CNLiveStringIsEmpty(self.reasonStr)?@"":self.reasonStr
                        };
    
    [CNLiveNetworking uploadNetworkWithURLString:CNApplyRefundUrl Param:param FormDataBlock:^(id<AFMultipartFormData> formData) {
        [self.selectedPhotos enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIImage *image = (UIImage *)obj;
            NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
            NSString *fileName = [NSString stringWithFormat:@"Image%d",(int)idx+1];
            NSString *name = [NSString stringWithFormat:@"Image%d",(int)idx+1];
            /*
                        *该方法的参数
                        1. appendPartWithFileData:要上传的照片[二进制流]
                        2. name:对应网站上[upload.php中]处理文件的字段(比如upload)
                        3. fileName:要保存在服务器上的文件名
                        4. mimeType:上传的文件的类型
                        */
            [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"image/jpeg"];
        }];
    } ProgressBlock:^(NSProgress *progress) {
        
    } CompletionBlock:^(NSURLSessionTask *requestTask, id responseObject, NSError *error) {
        [QMUITips hideAllTipsInView:self.view];
        if (!error) {
            if ([responseObject[@"errorCode"] integerValue] == 0) {
                [self.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter]postNotificationName:CNApplyRefundSuccessNotification object:nil];
            }else {
                [[HUDHelper sharedInstance]tipMessage:@"申请退款失败"];
            }
        }else {
            [[HUDHelper sharedInstance]tipMessage:error.description];
        }
    }];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 6;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return 1;
    }else if (section == 3){
        return 4;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    static NSString *basicIdentifier = @"CNLiveRefundBasicCell";
    static NSString *basicPriceIdentifier = @"CNLiveRefundBasicPriceCell";
    static NSString *canOrNORefIdentifier = @"CNLiveCanOrNoRefundCell";
    static NSString *goodsPriceeIdentifier = @"CNLiveRefundGoodsPriceCell";
    static NSString *reasonIdentifier = @"CNLiveRefundReasonCell";
    static NSString *instructionsIdentifier = @"CNLiveRefundInstructionsCell";
    static NSString *credentialIdentifier = @"CNLiveRefundCredentialCell";
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            CNLiveRefundBasicPriceCell *basicPriceCell = [tableView dequeueReusableCellWithIdentifier:basicPriceIdentifier];
            if (!basicPriceCell) {
                basicPriceCell = [[CNLiveRefundBasicPriceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:basicPriceIdentifier];
            }
            basicPriceCell.list = self.list;
             return basicPriceCell;
        }else {
                NSArray *basicTitles = @[@"基本信息",@"",@"服务内容",@"服务费",@"运费",@"配送方式"];
            CNLiveRefundBasicCell *basicCell = [tableView dequeueReusableCellWithIdentifier:basicIdentifier];
            if (!basicCell) {
                basicCell = [[CNLiveRefundBasicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:basicIdentifier indexPath:indexPath];
            }
            NSString *text = basicTitles[indexPath.row];
            basicCell.text = text;
            basicCell.list = self.list;
            return basicCell;
        }
    }else if (indexPath.section == 1){
        CNLiveCanOrNoRefundCell *canOrNoRefCell = [tableView dequeueReusableCellWithIdentifier:canOrNORefIdentifier];
        if (!canOrNoRefCell) {
            canOrNoRefCell = [[CNLiveCanOrNoRefundCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:canOrNORefIdentifier];
        }
        canOrNoRefCell.list = self.list;
        return canOrNoRefCell;
    }else if (indexPath.section == 2){
        CNLiveRefundGoodsPriceCell *goodsPriceCell = [tableView dequeueReusableCellWithIdentifier:goodsPriceeIdentifier];
        if (!goodsPriceCell) {
            goodsPriceCell = [[CNLiveRefundGoodsPriceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsPriceeIdentifier];
        }
        goodsPriceCell.list = self.list;
        return goodsPriceCell;
    }else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            CNLiveRefundReasonCell *reasonCell = [tableView dequeueReusableCellWithIdentifier:reasonIdentifier];
            if (!reasonCell) {
                reasonCell = [[CNLiveRefundReasonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reasonIdentifier];
            }
            reasonCell.refundReasonBlock = ^(NSString * _Nonnull reasonStr) {
                weakSelf.reasonStr = reasonStr;
            };
            return reasonCell;
        }else if ((indexPath.row == 1) || (indexPath.row == 2)) {
            CNLiveRefundInstructionsCell *instructionsCell = [tableView dequeueReusableCellWithIdentifier:instructionsIdentifier];
            if (!instructionsCell) {
                instructionsCell = [[CNLiveRefundInstructionsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:instructionsIdentifier indexPath:indexPath];
            }
            instructionsCell.refundDetailsBlock = ^(NSString * _Nonnull refundText) {
                if (indexPath.row == 1) {
                    weakSelf.refundAmount = refundText;
                }else if (indexPath.row == 2){
                    weakSelf.refundInstruct = refundText;
                }
            };
            return instructionsCell;
        }else if (indexPath.row == 3){
            CNLiveRefundCredentialCell *credentialCell = [tableView dequeueReusableCellWithIdentifier:credentialIdentifier];
            if (!credentialCell) {
                credentialCell = [[CNLiveRefundCredentialCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:credentialIdentifier];
            }
            credentialCell.delegate = self;
            return credentialCell;
        }
    }
    return nil;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            return 85;
        }else {
            return 45;
        }
    }else if (indexPath.section == 1){
        return 45;
    }else if (indexPath.section == 2){
        return 125;
    }else if (indexPath.section == 3){
        if (indexPath.row == 3) {
            return self.height;
        }else{
            return 40;
        }
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView  *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = CNLiveColorWithHexString(@"F5F5F8");
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    }else {
          return 10;
    }
    return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - Lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, KScreenWidth, KScreenHeight - kNavigationBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = (KScreenWidth - 20 - 2*8 - 4)/3 - 8 + 55;
        _tableView.userInteractionEnabled = YES;
        _tableView.backgroundColor = CNLiveColorWithHexString(@"F5F5F8");
    }
    return _tableView;
}
- (UIButton *)submitBtn {
    if (!_submitBtn) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.frame = CGRectMake(15, 10, KScreenWidth - 30, 55);
        [_submitBtn setBackgroundImage:CNLiveUIImageWithColor(KYellowColor) forState:UIControlStateNormal];
        [_submitBtn setBackgroundImage:CNLiveUIImageWithColor(KYellowHightColor) forState:UIControlStateHighlighted];
        [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        _submitBtn.layer.cornerRadius = 3;
        _submitBtn.layer.masksToBounds = YES;
        [_submitBtn addTarget:self action:@selector(submitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}
- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 75)];
       [_footerView addSubview:self.submitBtn];
    }
    return _footerView;
}

@end