//
//  BoltMaskView.m
//  XFFurit
//
//  Created by 陈俊俊 on 15/8/1.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "BoltMaskView.h"
#import "MaskCell.h"
#define TableHeight 45
#define LeftMargin 80
#define TotalHeight 224
#define LeftHeight 44
@interface BoltMaskView ()<UITextFieldDelegate>
{
    UIView *_leftView;
    UIButton *_currentBtn;
    NSIndexPath *_currentIndexPath;
    UIView *_titleView;
    UIView *_billNumberView;
    UITextField * _titleFiled;
    UITextField *_billFiled;
    NSString *_state;
    NSString *_billNumber;
    NSString *_title;
}
@property (nonatomic,strong)NSString *state;
@property (nonatomic,strong)NSString *title;
@property (nonatomic,strong)NSString *billNumber;


@end

@implementation BoltMaskView
- (instancetype)initWithFrame:(CGRect)frame state:(NSString *)state title:(NSString *)title bill:(NSString *)bill{
    self = [super initWithFrame:frame];
    if (self) {
        self.title = title;
        self.billNumber = bill;
        self.state = state;
        [self createView];
        [self createRightView];
        [self preferData];
    }
    return self;
}
- (void)preferData{
    //initial(未提交)，submitted(已提交)，insurvey(调研中)
    if ([self.state isEqualToString:@"none"]) {
        _currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    }else if ([self.state isEqualToString:@"initial"]) {
        _currentIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    }else if ([self.state isEqualToString:@"submitted"]) {
        _currentIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
    }else if ([self.state isEqualToString:@"insurvey"]) {
        _currentIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    }
    _titleView.hidden = YES;
    if(self.title.length > 0){
        _titleFiled.text = self.title;
    }
    _billNumberView.hidden = YES;
    if (self.billNumber.length > 0) {
        _billFiled.text = self.billNumber;
    }
}

#pragma mark - 创建视图
- (void)createView
{
    _leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LeftMargin,self.frame.size.height)];
    [self addSubview:_leftView];
    NSArray *arr = @[@"按状态",@"条件一",@"条件二"];
    for (NSInteger i = 0; i<arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, LeftHeight * i ,  LeftMargin ,LeftHeight);
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:HexColor(@"888888") forState:UIControlStateNormal];
        if (i == 0) {
            button.enabled = NO;
            _currentBtn = button;
        }
        [button setBackgroundImage:[UIImage imageNamed:@"maskEnable"] forState:UIControlStateDisabled];
        [button setBackgroundImage:[UIImage imageNamed:@"mask"] forState:UIControlStateNormal];
        button.tag = 1001+i;
        [button addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_leftView addSubview:button];
    }
}
- (void)createRightView{
    
    _titleView = [[UIView alloc]initWithFrame:CGRectMake(LeftMargin, 0, ScreenSize.width - LeftMargin, self.frame.size.height)];
    _titleView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_titleView];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, (ScreenSize.width - LeftMargin - 10*2), 20)];
    titleLabel.font = FontSize(15);
    titleLabel.text = @"标题类似于：";
    [_titleView addSubview:titleLabel];
    _titleFiled = [[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame) + 10, (ScreenSize.width - LeftMargin - 10*2), 30)];
    _titleFiled.textAlignment = NSTextAlignmentLeft;
    _titleFiled.textColor = HexColor(@"444444");
    _titleFiled.placeholder = @"输入标题";
    _titleFiled.borderStyle = UITextBorderStyleRoundedRect;
    _titleFiled.font = FontSize(15);
    _titleFiled.returnKeyType = UIReturnKeyDone;
    _titleFiled.delegate = self;
    [_titleView addSubview:_titleFiled];
    
    
    _billNumberView = [[UIView alloc]initWithFrame:CGRectMake(LeftMargin, 0, ScreenSize.width - LeftMargin, self.frame.size.height)];
    _billNumberView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_billNumberView];
    UILabel *billLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, (ScreenSize.width - LeftMargin - 10*2), 20)];
    billLabel.font = FontSize(15);
    billLabel.text = @"单号：";
    [_billNumberView addSubview:billLabel];
    
    _billFiled = [[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(billLabel.frame) + 10, (ScreenSize.width - LeftMargin - 10*2), 30)];
    _billFiled.textAlignment = NSTextAlignmentLeft;
    _billFiled.textColor = HexColor(@"444444");
    _billFiled.placeholder = @"输入单号";
    _billFiled.borderStyle = UITextBorderStyleRoundedRect;
    _billFiled.font = FontSize(15);
    _billFiled.returnKeyType = UIReturnKeyDone;
    _billFiled.delegate = self;
    [_billNumberView addSubview:_billFiled];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(LeftMargin, 0, ScreenSize.width - LeftMargin, self.frame.size.height) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];

}


- (void)leftBtnClick:(UIButton *)btn{
    _currentBtn.enabled = YES;
    btn.enabled = NO;
    _currentBtn = btn;
    [self keyBoardHidden];
    switch (btn.tag) {
        case 1001://状态
        {
            self.tableView.hidden = NO;
            _titleView.hidden = YES;
            _billNumberView.hidden = YES;
        }
            break;
        case 1002://条件二
        {
            self.tableView.hidden = YES;
            _titleView.hidden = YES;
            _billNumberView.hidden = NO;
        }
            break;
        case 1003://条件三
        {
            self.tableView.hidden = YES;
            _titleView.hidden = NO;
            _billNumberView.hidden = YES;
        }
            break;
        default:
            break;
    }
}
#pragma mark - 协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"MaskID";
    MaskCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MaskCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID totalWidth: ScreenSize.width - 80 totalHeight:TableHeight];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataArr.count > 0) {
        cell.Commitbtn.hidden = YES;
        [cell setTitleStr:self.dataArr[indexPath.row]];
        
    }
    if (_currentIndexPath) {
        if (indexPath.row == _currentIndexPath.row) {
            cell.Commitbtn.hidden = NO;
        }else{
            cell.Commitbtn.hidden = YES;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取选中的cell
    MaskCell *currentCell = (MaskCell *)[tableView cellForRowAtIndexPath:_currentIndexPath];
    currentCell.Commitbtn.hidden = YES;
    MaskCell *cell = (MaskCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.Commitbtn.hidden = NO;
    _currentIndexPath = indexPath;
    
    //initial(未提交)，submitted(已提交)，insurvey(调研中)

    NSString *stateStr = @"";
    if (indexPath.row == 0) {
        stateStr = @"none";
    }else if(indexPath.row == 1){
        stateStr = @"initial";
    }else if(indexPath.row == 2){
        stateStr = @"submitted";
    }else if(indexPath.row == 3){
        stateStr = @"insurvey";
    }
    [self.delegate getBoltValueSelectRow:stateStr];
}

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self keyBoardHidden];
    return YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self keyBoardHidden];
}
- (void)keyBoardHidden{
    [_titleFiled resignFirstResponder];
    [_billFiled resignFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == _titleFiled) {
        [self.delegate getTitleLike:_titleFiled.text];
    }else if(textField == _billFiled){
        [self.delegate getBillNumber:_billFiled.text];
    }
}

@end
