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

#import "SalesInputTableViewCell.h"

@interface SalesInputTableViewCell ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFieldAmount;

@end

@implementation SalesInputTableViewCell

- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.textFieldAmount.delegate = self;
//    self.txtfieldSum.delegate = self;
    
        // Initialization code
}

- (void)cellWithArray:(NSArray *)array {
    
    self.textFieldAmount.text = array[0];
//    self.txtfieldSum.text = [array[1] isEqualToString:@"0"] ? @"" : array[0];
}

//结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"textField end editing");
    
    self.updateCellData(@[self.textFieldAmount.text]);
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    NSArray *valideInput = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"-",@""];
    if ([valideInput containsObject:string]) {
        if (textField.text.length > 0 && [string isEqualToString:@"-"]) {
            return NO;
        }
        NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
        [futureString  insertString:string atIndex:range.location];
        NSInteger flag = 0;
        NSInteger pointNum = 0;//用于标记-的数量，只能有一个-
        const NSInteger limited = 20;//限制小数点后面的位数
        
        for (int i = (int)futureString.length - 1; i>=0; i--) {
            if ([futureString characterAtIndex:i] == '-') {
                pointNum++;
                if (pointNum == 2) {
                    
                    return NO;
                    
                }
            }
        }
        
        for (int i = (int)futureString.length - 1; i>=0; i--) {
            
            if ([futureString characterAtIndex:i] == '-') {
                
                if (flag > limited) {
                    
                    return NO;
                    
                }
                
                break;
                
            }
            
            flag++;
            
        }
        
        
        return YES;
        
    }else{
        return NO;
    }
    
    
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField selectAll:self];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
