//
//  SetSearchDaysViewController.m
//  Macalline
//
//  Created by qianjun_work on 15-7-30.
//  Copyright (c) 2015年 JianjiYuan. All rights reserved.
//

#import "SetSearchDaysViewController.h"

@interface SetSearchDaysViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIPickerView *pickerView;
    NSArray *pickerData;
}
@end

@implementation SetSearchDaysViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置查询天数";
    // Do any additional setup after loading the view.
    
    UILabel* lbl = [[UILabel alloc] init];
    lbl.textColor = [UIColor darkGrayColor];
    lbl.font = [UIFont systemFontOfSize:16];
    lbl.text = @"请选择需要设置的查询天数";
    lbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lbl];
    [lbl autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [lbl autoSetDimension:ALDimensionHeight toSize:30];
    
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 210)];
    //    指定Delegate
    pickerView.delegate=self;
    pickerView.dataSource = self;
    //    显示选中框
    pickerView.showsSelectionIndicator=NO;
    [self.view addSubview:pickerView];
    
    NSArray *dataArray = [[NSArray alloc]initWithObjects:@"三个月内",@"半年内",@"一年内", nil];
    
    pickerData=dataArray;
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.translatesAutoresizingMaskIntoConstraints = NO;
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:@"确  定" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"Login/btn_login.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
//    UIButton* btn = [[UIButton alloc] init];
//    [btn setTitle:@"确定" forState:UIControlStateNormal];
//    [self.view addSubview:btn];
    [btn autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [btn autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    [btn autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:pickerView withOffset:20];
    [btn autoSetDimension:ALDimensionHeight toSize:39];
    
    NSString* str = [UIApplication getObjectType:String key:@"setDays"];
    int i = 0;
    for (; i < dataArray.count; i++) {
        if ([[dataArray objectAtIndex:i] isEqualToString:str]) {
            break;
        }
    }
    [pickerView selectRow:i  inComponent:0  animated:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return pickerData.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString* str = [pickerData objectAtIndex:row];
    return [NSString stringWithFormat:@"%@ 天",str];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30;
}
-(void)btnClick
{
    NSInteger row =[pickerView selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];

    [UIApplication showLoadingView:kTextShowInProgress];

    NSString *interface=@"updateTimeRangeEX";
    NSDictionary *dicParam = @{@"days":selected};

    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"updateTimeRangeEX"])
    {
        NSInteger row =[pickerView selectedRowInComponent:0];
        NSString *selectedday = @"";
        if (row == 0) {
            selectedday = @"90";
        }
        else if (row == 0) {
            selectedday = @"180";
        }
        else if (row == 0) {
            selectedday = @"360";
        }
        else
        {
            selectedday = @"30";
        }
        [UIApplication saveObject:selectedday type:String key:@"setDays"];
        //[DataMgr getInstance].serverTime = selectedday;
        [self .navigationController popViewControllerAnimated:YES];
    }
    
}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
