//
//  setSearchDays.m
//  Macalline
//
//  Created by bestRun on 15-8-5.
//  Copyright (c) 2015年 JianjiYuan. All rights reserved.
//

#import "setSearchDays.h"

@interface setSearchDays ()
{
   long  int selected;
}
@property (nonatomic, strong) UITableView *tableVContent;
@property (nonatomic,strong)UIButton *cancelButton;
@property(nonatomic,strong)UIButton *doneButton;
@property (nonatomic, strong) BRNetworkMgr *networkMgr;

@end


@implementation setSearchDays

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setViews];
    }
    
    // [UIApplication saveObject:selectedday type:String key:@"setDays"];
    
    
    if((long)[[UIApplication getObjectType:String key:@"setDays"] integerValue]>0)
    {
       // selected=((long)[[UIApplication getObjectType:String key:@"setDays"] integerValue])/90;
        if([[UIApplication getObjectType:String key:@"setDays"] integerValue]==90)
            selected=1;
        else
        {
            
        if([[UIApplication getObjectType:String key:@"setDays"] integerValue]==180)
            {
                selected=2;
            }
        else
            if([[UIApplication getObjectType:String key:@"setDays"] integerValue]==360)
            {
                selected=3;
            }
        }
    }
    else
        selected=1;
    
    return self;
}


-(void)setViews
{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.tableVContent autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
    [self.tableVContent autoSetDimension:ALDimensionHeight toSize:44*4];
   
    
    NSIndexPath *first=[NSIndexPath indexPathForRow:selected inSection:0];
    [self.tableVContent selectRowAtIndexPath:first animated:YES scrollPosition:(UITableViewScrollPositionTop)];
    
}

-(void)btnCancel
{
    [self removeFromSuperview];
}
-(void)updateDays
{

    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface=@"updateTimeRangeEX";
    NSString *selectedday =[NSString stringWithFormat:@"%ld",selected*30];
    
    
    NSDictionary *dicParam = @{@"days":selectedday};
   
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseEXURL(interface)
                                                     interface:interface
                                                        params:dicParam
                                                      dataType:BRNetMgrDataTypeJSON
                                                        method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];

  
  //  [self removeFromSuperview];
}



#pragma mark =============  UITableView DataSource  =============

- (UITableView *)tableVContent
{
    if (_tableVContent) {
        return _tableVContent;
    }
    self.tableVContent = [UITableView newAutoLayoutView];
    _tableVContent.backgroundColor = [UIColor clearColor];
    
    _tableVContent.delegate = self;
    
    _tableVContent.dataSource = self;
    _tableVContent.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableVContent.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self addSubview:_tableVContent];
    return _tableVContent;
}

-(void)configureCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath
{
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text=@"设置获取任务时间段";
            cell.textLabel.textColor = [UIColor darkGrayColor];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
            [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            _cancelButton.titleLabel.font= [UIFont boldSystemFontOfSize:15];
            [_cancelButton setTitleColor:RGBColor(153, 153, 153, 1) forState:UIControlStateNormal];
            [cell.contentView addSubview:self.cancelButton];
            [_cancelButton autoPinEdgeToSuperviewEdge:ALEdgeLeft];
            [_cancelButton autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:cell.contentView withOffset:5];
            [_cancelButton autoSetDimension:ALDimensionWidth toSize:80];
             [self.cancelButton addTarget:self action:@selector(btnCancel) forControlEvents:UIControlEventTouchUpInside];
            
            self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _doneButton.translatesAutoresizingMaskIntoConstraints = NO;
            
            [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
            _doneButton.titleLabel.font= [UIFont boldSystemFontOfSize:15];
            [_doneButton setTitleColor:RGBColor(153, 153, 153, 1) forState:UIControlStateNormal];
            [cell.contentView addSubview:self.doneButton];
            [_doneButton autoPinEdgeToSuperviewEdge:ALEdgeRight];
            [_doneButton autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:cell.contentView withOffset:5];
            [_doneButton autoSetDimension:ALDimensionWidth toSize:80];
            [self.doneButton addTarget:self action:@selector(updateDays)  forControlEvents:UIControlEventTouchUpInside];
            
            break;
        case 1:
            cell.textLabel.text=@"三个月内";
            cell.textLabel.font=[UIFont systemFontOfSize:15];
            
            cell.textLabel.textColor = [UIColor darkGrayColor];
            break;
        case 2:
            cell.textLabel.text=@"半年内";
             cell.textLabel.font=[UIFont systemFontOfSize:15];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            
            break;
        case 3:
            cell.textLabel.text=@"一年内";
             cell.textLabel.font=[UIFont systemFontOfSize:15];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            break;
        default:
            break;
    }

    
    if(indexPath.row==0)
    {
        UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(10,43, 300, 1.1)];
        viewLine.backgroundColor=RGBColor(153, 153, 153,0.5 );
        [cell.contentView addSubview:viewLine];
        
    }
    else
    {
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(10, 43, 300, 1.1)];
    viewLine.backgroundColor=RGBColor(153, 153, 153,0.5 );
    [cell.contentView addSubview:viewLine];
    }
    
    
    
    if(indexPath.row==selected)
    {
        [self.tableVContent selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        
    }
    
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [self configureCell:cell forIndexPath:indexPath];
    return cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0)
        return 44;
    else
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"You Click At Section: %d Row: %d", (int)indexPath.section, (int)indexPath.row);
    if(indexPath.row == 1)
    {
        selected = 3;
    }
    if(indexPath.row == 2)
    {
        selected = 6;
    }
    if(indexPath.row == 3)
    {
        selected = 12;
    }
    
}



#pragma mark =============  Properties Lazy Load  =============

- (BRNetworkMgr *)networkMgr
{
    if (!_networkMgr) {
        self.networkMgr = [BRNetworkMgr getInstance];
        _networkMgr.delegate = self;
    }
    return _networkMgr;
}

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    if ([operation.userInfo[@"interface"] isEqualToString:@"updateTimeRangeEX"])
    {
        NSString *selectedday =[NSString stringWithFormat:@"%ld",selected*30];
        
        
        [UIApplication saveObject:selectedday type:String key:@"setDays"];
        //[DataMgr getInstance].serverTime = selectedday;
              UIAlertView *notice=[[UIAlertView alloc] initWithTitle:@"修改成功" message:@"修改推送天数成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [notice show];
        
        [self removeFromSuperview];
    }
}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
   // UIAlertView *notice=[[UIAlertView alloc] initWithTitle:@"修改失败" message:@"修改推送天数失败稍后重试 !" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
}

@end
