//
//  SaleInputAmountCollectionViewCell.m
//  RealEstateManagement
//
//  Created by Javen on 2016/10/11.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "CalculateHelper.h"
#import "SaleInputAmountCollectionViewCell.h"
#import "NSString+Additions.h"
@interface SaleInputAmountCollectionViewCell () <UITextFieldDelegate>

@end
@implementation SaleInputAmountCollectionViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.textFieldInput.delegate = self;
    [self.textFieldInput addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];

}

/**
 *  设置可编辑状态和不可编辑状态
 */
- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    if (isEdit) {
        self.textFieldInput.userInteractionEnabled = YES;
        self.textFieldInput.textColor = [UIColor blackColor];
    }else{
        self.textFieldInput.userInteractionEnabled = NO;
        self.textFieldInput.textColor = [UIColor lightGrayColor];
    }
}

//实时获取变化之后的数据
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //获取改变之后的字符串
    NSMutableString *futureString = [NSMutableString stringWithString:textField.text];
    [futureString insertString:string atIndex:range.location];
    return [futureString isValidMoneyInput];
}

- (void)textFieldDidChanged:(UITextField *)textField {
    self.model.total = [CalculateHelper decimalNumber:textField.text];
    self.blockNumberChanged();
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.textColor = [UIColor blackColor];
    if ([textField.text isEqualToString:@"0.00"]) {
        textField.text = @"";
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //输入框有文字则显示，避免没输入自动出现0.00的情况
    if (textField.text.length > 0) {
        if ([textField.text isEqualToString:@"0"]) {
            textField.textColor = [UIColor lightGrayColor];
            textField.text = @"0.00";
        }else{
            self.textFieldInput.text = [CalculateHelper getMoneyStringFrom:textField.text];
        }
    }
}

- (void)configCellWithArray:(NSMutableArray *)array indexPath:(NSIndexPath *)indexPath {
    self.model = array[indexPath.row];
    self.textFieldInput.text = self.model.total ? [CalculateHelper getMoneyStringFrom:self.model.total] : nil;
    if ([self.textFieldInput.text isEqualToString:@"0.00"]) {
        self.textFieldInput.textColor = [UIColor lightGrayColor];
    }else{
        self.textFieldInput.textColor = [UIColor blackColor];
    }
    self.labelTitle.text = self.model.payment.name;
}


@end
