ApplyPrizeViewController.m 7.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  ApplyPrizeViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/11/22.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ApplyPrizeViewController.h"
#import "ModifyShippingAddressView.h"
@interface ApplyPrizeViewController ()<CityselectedDelegate,UITextFieldDelegate>

/**
 *  城市选择器
 */
@property (nonatomic,strong) ModifyShippingAddressView *citySelecteview;

18 19 20 21 22
/**
 保存兑奖单
 */
@property (nonatomic,strong) RsPrizeBill *prizeBill;

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
@end

@implementation ApplyPrizeViewController

#pragma mark -取消弹出的圆角
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.superview.layer.cornerRadius = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTextFieldAction];
}

#pragma mark - 设置UITextField
- (void)setUpTextFieldAction
{
    UIView *leftView1 = [[UIView alloc]init];
    leftView1.frame = CGRectMake(0, 0, 10, 1);
    self.consigneeTextField.leftView = leftView1;
    self.consigneeTextField.leftViewMode = UITextFieldViewModeAlways;
    UIView *leftView2 = [[UIView alloc]init];
    leftView2.frame = CGRectMake(0, 0, 10, 1);
    self.mobileTextField.leftView = leftView2;
    self.mobileTextField.leftViewMode = UITextFieldViewModeAlways;
    UIView *leftView4 = [[UIView alloc]init];
    leftView4.frame = CGRectMake(0, 0, 10, 1);
    self.addressDetailsTextField.leftView = leftView4;
    self.addressDetailsTextField.leftViewMode = UITextFieldViewModeAlways;
54
    [self.addressButton addTarget:self action:@selector(selectedCityClickAction) forControlEvents:UIControlEventTouchUpInside];
55 56
}

57 58
#pragma mark - 选中礼品
- (void)setSelectArray:(NSArray<PrizeListModel *> *)selectArray
59
{
60 61 62 63 64 65
    _selectArray = selectArray;
    CGFloat width = 70;//宽度
    CGFloat interval = 10;//间隔
    for (int i=0; i<_selectArray.count; i++) {
        PrizeListModel *model = _selectArray[i];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*width+i*interval, 0, width, width)];
66
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.attachment.fileUrl] placeholderImage:REPLACEIMAGE];
67
        [self.exchangeScrollView addSubview:imageView];
68
    }
69
    self.exchangeScrollView.contentSize = CGSizeMake(width*_selectArray.count+interval*_selectArray.count-1, 0);
70 71 72 73 74 75 76 77 78 79 80 81
}

#pragma mark -城市选择器
- (void)selectedCityClickAction
{
    self.citySelecteview.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 160);
    [self.view addSubview:self.citySelecteview];
    self.citySelecteview.delegate = self;
    [self.citySelecteview.selectedCityButton addTarget:self action:@selector(completeButton) forControlEvents:UIControlEventTouchUpInside];
    [UIView animateWithDuration:0.2 animations:^{
        self.citySelecteview.frame = CGRectMake(0, self.view.frame.size.height-160, self.view.frame.size.width, 160);
    }completion:^(BOOL finished) {
82
        self.addressButton.enabled = NO;
83 84 85 86 87 88 89 90 91 92 93
    }];
}

#pragma mark -完成按钮点击
- (void)completeButton
{
    [UIView animateWithDuration:0.2 animations:^{
        self.citySelecteview.frame = CGRectMake(0, ScreenHeight, self.view.frame.size.width, 160);
    }completion:^(BOOL finished) {
        [self.citySelecteview removeFromSuperview];
        self.citySelecteview = nil;
94
        self.addressButton.enabled = YES;
95 96 97
    }];
}

98 99 100
#pragma mark - 选择城市后回调
- (void)citySelected:(NSString *)cityString WithprovincesString:(NSString *)provincesString
{
101
    [self.addressButton setTitle:[NSString stringWithFormat:@"%@·%@",provincesString,cityString] forState:UIControlStateNormal];
102
}
103 104 105 106 107 108 109 110 111

#pragma mark - 取消
- (IBAction)cancelButtonClickAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 提交
- (IBAction)submitButtonClickAction:(UIButton *)sender {
    
112
    if ([[self class] isBlankString:self.consigneeTextField.text]) {
113
        [XBLoadingView showHUDViewWithText:@"收货人不能为空"];return;
114 115
    }
    if ([[self class] isBlankString:self.mobileTextField.text]) {
116
        [XBLoadingView showHUDViewWithText:@"联系电话不能为空"];return;
117
    }
118 119 120
    if (![HENLENSONG isValidateMobile:self.mobileTextField.text]) {
        [XBLoadingView showHUDViewWithText:@"手机号码格式不正确"];return;
    }
121
    if ([[self class] isBlankString:self.addressButton.currentTitle]) {
122
        [XBLoadingView showHUDViewWithText:@"地址不能为空"];return;
123 124
    }
    if ([[self class] isBlankString:self.addressDetailsTextField.text]) {
125
        [XBLoadingView showHUDViewWithText:@"详细地址不能为空"];return;
126 127 128 129 130 131 132 133
    }
    [self savePrizeRecord];
}

#pragma mark - 保存兑奖单
- (void)savePrizeRecord
{
    WS(weakSelf);
134 135
    [XBLoadingView showHUDViewWithDefault];
    NSLog(@"%@",[[self.prizeBill toDictionary] JSONString]);
136
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(SAVEPRIZEBILL) WithRequestType:ZERO WithParameter:self.prizeBill WithReturnValueBlock:^(id returnValue) {
137
        
138
        [XBLoadingView hideHUDViewWithDefault];
139
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
140
            [XBLoadingView showHUDViewWithSuccessText:@"申请成功" completeBlock:^{
141 142 143
                if (weakSelf.requestFinishBlock) {
                    weakSelf.requestFinishBlock();
                }
144 145 146
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
            
147
        }else {
148
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
149 150
        }
    } WithFailureBlock:^(NSError *error) {
151
        [XBLoadingView hideHUDViewWithDefault];
152
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
153
    }];
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}



#pragma mark - lazy
/**
 *  城市选择View
 *
 *  @return ModifyShippingAddressView
 */
- (ModifyShippingAddressView *)citySelecteview
{
    if (_citySelecteview == nil) {
        _citySelecteview = [[[NSBundle mainBundle] loadNibNamed:@"ModifyShippingAddressView"
                                                          owner:self options:nil] lastObject];
    }
    return _citySelecteview;
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
- (RsPrizeBill *)prizeBill
{
    if (!_prizeBill) {
        _prizeBill = [[RsPrizeBill alloc]init];
        //收货人
        TOPrizeBillEntity *consignee = [[TOPrizeBillEntity alloc]init];
        consignee.employee = [Shoppersmanager manager].Shoppers.employee.fid;
        consignee.receiver = self.consigneeTextField.text;
        consignee.mobilephone = self.mobileTextField.text;
        consignee.receiveAddress = [NSString stringWithFormat:@"%@%@",self.addressButton.currentTitle,self.addressDetailsTextField.text];
        _prizeBill.bill = consignee;
        //兑换礼品
        NSMutableArray *array = [NSMutableArray array];
        for (PrizeListModel *model in self.selectArray) {
            TOPrizeBillDetailsEntity *entity = [[TOPrizeBillDetailsEntity alloc]init];
188 189 190
            TOPrizeEntity *prize = [[TOPrizeEntity alloc]init];
            prize.fid = model.fid;
            entity.prize = prize;
191 192 193 194 195 196 197
            [array addObject:entity];
        }
        _prizeBill.details = (NSArray<TOPrizeBillDetailsEntity> *)array;
    }
    return _prizeBill;
}

198
@end