//
//  LocationViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/3/21.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "LocationViewController.h"
#import "YXPickerManager.h"

@interface LocationViewController ()



@end

@implementation LocationViewController

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

#pragma mark -选择地址
- (IBAction)chooseLocationButtonClickAction:(UIButton *)sender {
    ToStoreAddress *storeAddress = [Shoppersmanager manager].storeAddress;
    NSString *address = [NSString stringWithFormat:@"%@-%@-%@",storeAddress.province,storeAddress.city,storeAddress.country];
    [[YXPickerManager shareManager] showAddressPickerView:kMainBlueColor defaultAddress:address commitBlock:^(NSString *address, NSString *zipcode) {
        [sender setTitle:address forState:UIControlStateNormal];
    } cancelBlock:^{
        
    }];
}

#pragma mark -确认
- (IBAction)affirmButtonClickAction:(UIButton *)sender {
    
    NSArray *addressArray = [[self.chooseLocationButton currentTitle] componentsSeparatedByString:@"-"];
    if (addressArray.count < 3) {
        [XBLoadingView showHUDViewWithText:@"请选择所在区域"];return;
    }
    WS(weakSelf);
    ShowAlertView(@"提示", @"确认后不可更改,请检查是否正确", @[@"确认",@"取消"], UIAlertControllerStyleAlert, ^(NSInteger index) {
        if (index == ONE) {
            return;
        }
        [XBLoadingView showHUDViewWithDefault];
        ToStoreAddress *address = [[ToStoreAddress alloc] init];
        address.resellerid = [Shoppersmanager manager].shoppers.employee.currentDepart.fid;
        address.storecode = [Shoppersmanager manager].shoppers.storeCode;
        address.createDate = [[NSDate date] httpParameterString];
        address.province = addressArray[0];
        address.city = addressArray[1];
        address.country = addressArray[2];
        [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDRESS) withRequestType:POST withParameter:address withReturnValueBlock:^(id returnValue) {
            
            [XBLoadingView hideHUDViewWithDefault];
            if (RESULT(returnValue)) {
                [XBLoadingView showHUDViewWithSuccessText:@"保存成功" completeBlock:^{
                    [weakSelf dismissViewControllerAnimated:YES completion:nil];
                }];
                [weakSelf getStoreAddress:nil];
                
            }else {
                [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
            }
            
        } withFailureBlock:^(NSError *error) {
            [XBLoadingView showHUDViewWithText:error.localizedDescription];
        }];
    });
}
















@end
