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

#import "SalesInputTableViewCell.h"

@interface SalesInputTableViewCell () <UITextFieldDelegate>

@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] ? array[0] : @"0";
    //    self.txtfieldSum.text = [array[1] isEqualToString:@"0"] ? @"" : array[0];
}

//结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"textField end editing");
    if ([textField.text isEqualToString:@"."] || [textField.text isEqualToString:@"-"]) {
        textField.text = @"";
    }
    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];

        if ([futureString hasPrefix:@"."] || [futureString hasPrefix:@"00"] || [futureString hasPrefix:@"-."] || futureString.length > 11) {
            return NO;
        }
        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++;
        }

        NSInteger flag1 = 0;
        NSInteger pointNum1 = 0; //用于标记.的数量，只能有一个.
        const NSInteger limited1 = 2; //限制小数点后面的位数

        for (int i = (int) futureString.length - 1; i >= 0; i--) {
            if ([futureString characterAtIndex:i] == '.') {
                pointNum1++;
                if (pointNum1 == 2) {

                    return NO;
                }
            }
        }

        for (int i = (int) futureString.length - 1; i >= 0; i--) {

            if ([futureString characterAtIndex:i] == '.') {

                if (flag1 > limited1) {

                    return NO;
                }

                break;
            }

            flag1++;
        }

        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
