//
//  ListPartView.m
//  Macalline
//
//  Created by qianjun_work on 15-8-1.
//  Copyright (c) 2015年 JianjiYuan. All rights reserved.
//

#import "ListPartView.h"

@implementation ListPartView
{
    NSString*arrType;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self initCommonView];
    }
    return self;
}

-(id) initWithFrame:(CGRect)frame andArray:(NSArray*)arr andType:(NSString *)type;
{
    if (self = [super initWithFrame:frame]) {
        self.arrContent = arr;
        arrType = type;
        [self initCommonView];
    }
    return self;
}
- (void)initCommonView
{
    //int num = self.arrContent.count+1;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    UITableView* tableView = [[UITableView alloc] init];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self addSubview:tableView];
    [tableView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [tableView autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [tableView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [tableView autoSetDimension:ALDimensionHeight toSize:280];
    
    
    UIView* viewHead = [[UIView alloc] init];
    viewHead.backgroundColor  = [UIColor whiteColor];
    [self addSubview:viewHead];
    [viewHead autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [viewHead autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [viewHead autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:tableView];
    [viewHead autoSetDimension:ALDimensionHeight toSize:40];
    
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, 350, 0.5)];
    viewLine.backgroundColor = [UIColor colorWithRed:0.7333 green:0.7333 blue:0.7333 alpha:1];
    [viewHead addSubview:viewLine];
    
    UILabel* lblTitle = [[UILabel alloc] init];
    lblTitle.textColor = [UIColor darkGrayColor];
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.text = @"请选择过滤条件";
    lblTitle.font = [UIFont systemFontOfSize:15];
    [viewHead addSubview:lblTitle];
    [lblTitle autoAlignAxis:ALAxisVertical toSameAxisOfView:viewHead];
    [lblTitle autoAlignAxis:ALAxisHorizontal toSameAxisOfView:viewHead];
    
    UIButton* btnClose = [[UIButton alloc] init];
    [btnClose addTarget:self action:@selector(btnClose) forControlEvents:UIControlEventTouchUpInside];
    [btnClose setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btnClose.titleLabel.font = [UIFont systemFontOfSize:13];
    [btnClose setTitle:@"关闭" forState:UIControlStateNormal];
    [viewHead addSubview:btnClose];
    [btnClose autoAlignAxis:ALAxisHorizontal toSameAxisOfView:viewHead];
    [btnClose autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];

}
-(void)btnClose
{
    [self removeFromSuperview];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentity = @"TaskCheckCell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"全部";
    }
    else
    {
        if ([arrType isEqualToString:@"area"]) {
            cell.textLabel.text = self.arrContent[indexPath.row-1][@"areaName"];
        }
        if ([arrType isEqualToString:@"shen"]) {
            cell.textLabel.text = self.arrContent[indexPath.row-1][@"name"];
        }
        if ([arrType isEqualToString:@"shi"]) {
            cell.textLabel.text = self.arrContent[indexPath.row-1][@"name"];
        }
        if ([arrType isEqualToString:@""]) {
            cell.textLabel.text = self.arrContent[indexPath.row-1];
        }
    }

    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.textColor = [UIColor colorWithRed:0.2431 green:0.7137 blue:0.9373 alpha:1];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5, 350, 0.5)];
    viewLine.backgroundColor = [UIColor colorWithRed:0.7333 green:0.7333 blue:0.7333 alpha:1];
    [cell.contentView addSubview:viewLine];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrContent.count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.BtnClickBlock) {//回调
        self.BtnClickBlock(indexPath.row);
    }
    
}
@end
