//
//  postMessageSecondViewController.m
//  Open
//
//  Created by 看看智能 on 2017/3/1.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "postMessageSecondViewController.h"
#import "HXSearchBar.h"
#import "GBTagListView.h"
#import "postMessageThird.h"
@interface postMessageSecondViewController ()<UISearchBarDelegate>
{
    UIView *btView;
    NSArray * _indexPathRow;
    NSMutableArray   * _bottomDataArr;
    GBTagListView*_tempTag;
    
    
    
}
@property(nonatomic,strong)HXSearchBar  *searchHX;
@property(nonatomic,strong)UIButton *superButton;
@property(nonatomic,strong)UIButton *childButton;
@property(nonatomic,strong)UIImageView *imageView_line;
@property(nonatomic,strong)UILabel *labelText;
@property(nonatomic,strong)PostMessageViewModel *postVM;
@property(nonatomic,strong)NSMutableArray *topDataArr;
@end

@implementation postMessageSecondViewController
-(NSMutableArray *)topDataArr
{
    if (!_topDataArr) {
        _topDataArr =[NSMutableArray new];
    }
    return _topDataArr;
}
+ (instancetype)sharedInstance{
    static postMessageSecondViewController *p = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        p = [postMessageSecondViewController new];
    });
    return p;
}
-(PostMessageViewModel *)postVM
{
    if (!_postVM) {
        _postVM =[PostMessageViewModel new];
    }
    return _postVM;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor whiteColor];
  self.navigationController.toolbarHidden=YES;
   _indexPathRow =@[@"工业品",@"农业",@"生活服务",@"消费品",@"热门服务"];
    self.superButton =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.superButton setTitle:_indexPathRow[self.indexRow] forState:UIControlStateNormal];
    self.superButton.backgroundColor =kRGBColor(34, 166, 222);
    self.superButton.layer.cornerRadius=15;
    self.superButton.clipsToBounds=YES;
    self.superButton.titleLabel.font =[UIFont systemFontOfSize:14];
    self.superButton.titleLabel.textAlignment  =NSTextAlignmentCenter;
    [self.view addSubview:self.superButton];
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    [self.superButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(rectStatus.size.height +rectNav.size.height+20);
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];
    self.childButton =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.childButton setTitle:_indexPathRow[0] forState:UIControlStateNormal];
    self.childButton.backgroundColor =kRGBColor(34, 166, 222);
    self.childButton.layer.cornerRadius=15;
    self.childButton.clipsToBounds=YES;
     self.childButton.titleLabel.font =[UIFont systemFontOfSize:14];
       self.childButton.titleLabel.textAlignment  =NSTextAlignmentCenter;
    [self.view addSubview:self.childButton];
    
    [self.childButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.topMargin.mas_equalTo(self.superButton);
        make.left.mas_equalTo(self.superButton.mas_right).mas_equalTo(20);
          make.width.mas_equalTo(80);

    }];
    
    
    
    self.imageView_line =[[UIImageView alloc]init];
     self.imageView_line.backgroundColor=kRGBColor(188, 188, 188);
    [self.view addSubview:self.imageView_line];
    [self.imageView_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.childButton.mas_bottom).mas_equalTo(20);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(1);
    }];
    self.labelText =[[UILabel alloc]init];
    self.labelText.text =@"热门发布";
    self.labelText.font =[UIFont systemFontOfSize:20];
    [self.view addSubview:self.labelText];
    [self.labelText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo( self.imageView_line.mas_bottom).mas_equalTo(20);
        make.centerX.mas_equalTo(0);
    }];
    
    
    
    
    //搜索框
    
    //控件的选择
    [self addControl];
    //添加流式布局
    /**
     1.添加数据
     2.添加顶部视图
     3.添加底部的按钮
     */
    [self preData];
   
    [self bottomView];
}














- (void)addSearchBar {
    //加上 搜索栏
    self.view.backgroundColor=[UIColor lightGrayColor];
    self.searchHX  = [[HXSearchBar alloc] init];
    self.searchHX .backgroundColor = [UIColor clearColor];
    self.searchHX .delegate = self;
    //输入框提示
    self.searchHX .placeholder = @"搜索";
    //光标颜色
    self.searchHX .cursorColor = [UIColor blackColor];
    //TextField
    self.searchHX .searchBarTextField.layer.cornerRadius = 10;
    self.searchHX .searchBarTextField.layer.masksToBounds = YES;
    self.searchHX .searchBarTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.searchHX .searchBarTextField.layer.borderWidth = 1.0;
    
    //清除按钮图标
    self.searchHX .clearButtonImage = [UIImage imageNamed:@"demand_delete"];
    //    searchBar.showsCancelButton=YES;
    //去掉取消按钮灰色背景
    self.searchHX .hideSearchBarBackgroundImage = YES;
    
    [self.view addSubview:self.searchHX ];
}
#pragma mark - UISearchBar Delegate
-(void)addControl
{
    [self.searchHX mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 20, 35));
    }];
    
    
    
    
    
}

#pragma mark - 设置流式布局
-(void)preData
{
//    _topDataArr  = @[@"化肥",@"农作物",@"种子",@"生活用品",@"工艺",@"美食",@"教程",@"手工",@"烘焙",@"吃",@"风景",@"家常菜",@"生活方式"];
    //    _bottomDataArr    = [[NSMutableArray alloc]initWithArray:arr];
    NSLog(@"----------%ld",[_indexPathRow[self.indexRow] integerValue]);
  [postMessageMNet getChildCatalog:self.indexRow+1 completionHandle:^(postMessageModel *model, NSError *error) {
      [self.topDataArr removeAllObjects];
      for (postMessageResultModel *models in model.Result) {
          [self.topDataArr addObject:models.categoryname];
      }
      if (self.topDataArr ==Nil) {
          return ;
      }
      if(self.topDataArr.count > 0){
      [self.childButton setTitle:self.topDataArr[0] forState:UIControlStateNormal];
      }
       [self topView];
  }];
    
    
    
    
    
    
}
-(void)topView
{    GBTagListView *tagList=[[GBTagListView alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen]bounds].size.height/3, ScreenWidth, 0)];
    tagList.canTouch=YES;
    tagList.signalTagColor=[UIColor whiteColor];
    [tagList setTagWithTagArray:_topDataArr];
    [self.view addSubview:tagList];

    
    
    __weak __typeof(self)weakSelf = self;
    [tagList setDidselectItemBlock:^(NSArray *arr) {
        NSLog(@"%@",arr);
        [weakSelf.childButton setTitle:arr[0] forState:UIControlStateNormal];
    }];

    
    
    
    //    UILabel*tip=[[UILabel alloc]initWithFrame:CGRectMake(0, tagList.frame.size.height+10, ScreenWidth, 20)];
    //    tip.text=@"选中的标签是：";
    //    tip.textAlignment=NSTextAlignmentCenter;
    //    tip.font=[UIFont boldSystemFontOfSize:18];
    //    [self.view addSubview:tip];
    
}








-(void)bottomView
{
    UIButton *buttomNext =[UIButton buttonWithType:UIButtonTypeCustom];
    [buttomNext setTitle:@"下一步" forState:UIControlStateNormal];
    [buttomNext addTarget:self action:@selector(buttonNextMethod:) forControlEvents:UIControlEventTouchUpInside];
    if (!_tempTag ) {
        [buttomNext setBackgroundColor:[UIColor colorWithRed:34/255.0 green:167/255.0 blue:222/255.0 alpha:1.0]];
        //        buttomNext.userInteractionEnabled =YES;
    }else
    {
        [buttomNext setBackgroundColor:[UIColor grayColor]];
        //           buttomNext.userInteractionEnabled =NO;
    }
    [self.view addSubview:buttomNext];
    [buttomNext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, 40));
    }];
    
    
    
    
}
#pragma mark - 跳转到第三页面
-(void)buttonNextMethod:(UIButton*)button
{
    
    postMessageThird *bvc= [postMessageThird sharedInstance];
    bvc.chilfStr =self.childButton.titleLabel.text;
    bvc.superStr =self.superButton.titleLabel.text;
    
    //以后在改过来
    [self.navigationController pushViewController:bvc animated:YES];
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    HXSearchBar *sear = (HXSearchBar *)searchBar;
    //取消按钮
    sear.showsCancelButton =NO;
    sear.cancleButton.backgroundColor = [UIColor clearColor];
    [sear.cancleButton setTitle:@"搜索" forState:UIControlStateNormal];
    [sear.cancleButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    sear.cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
}
//编辑文字改变的回调
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"searchText:%@",searchText);
}
//搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.view endEditing:YES];
}
//取消按钮点击的回调
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"%@",searchBar.text);
    searchBar.text = nil;
    [self.view endEditing:YES];
}




@end
