//
//  ChooseShopViewController.m
//  vanke
//
//  Created by Z on 16/4/19.
//  Copyright © 2016年 gomore. All rights reserved.
//

#import "ChooseShopViewController.h"
#import "UIImage+Helper.h"
#import "ICRHTTPController.h"

#define kCellId @"searchCell"
@interface ChooseShopViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *searchText;

@end

@implementation ChooseShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择商铺";
    [self setUPSearchBar];
    [self setUpTableView];
    // Do any additional setup after loading the view.
}

- (void)setUPSearchBar {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor colorWithRed:0.953 green:0.439 blue:0.063 alpha:1.000]];
    self.searchBar.tintColor = [UIColor whiteColor];
    self.searchBar.placeholder = @"请输入关键字";
    self.searchBar.delegate = self;
    [self.view addSubview:self.searchBar];
}

#pragma mark =========== searbar delegate ===========
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.view endEditing:YES];
    searchBar.showsCancelButton = NO;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.searchText = searchText;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchShopName:self.searchText];
    [self.view endEditing:YES];

}


- (void)searchShopName:(NSString *)name {
    NSDictionary *param = @{@"codeOrNameLike":name,
                            @"projectIdEquals":@""};
    ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
    
    [httpCtrl getUrl:@"rest/shop/" params:param success:^(id data){
        
        NSLog(@"d");
    } failure:^(id data){
        
    }];
    
}



- (void)setUpTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.searchBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.searchBar.bottom - 64) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor greenColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellId];
    [self.view addSubview:self.tableView];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId forIndexPath:indexPath];
    
    cell.textLabel.text = [NSString stringWithFormat:@"星巴克【A01-0%ld】", (long)indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.chooseBlock(@"选择好了商铺");
    [self.navigationController popViewControllerAnimated:YES];
}

- (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
