//
//  DurationPopupView.m
//  kneet2
//
//  Created by Jason Lee on 1/25/16.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

#import "CustomLabel.h"
#import "CustomButton.h"

#import "DurationPopupView.h"
#import "DatePickerButton.h"

@interface DurationPopupView () {
    
}

@end

@implementation DurationPopupView

- (id)initFromNib {

    for (UIView *view in [CommonUtil nibViews:@"DurationPopupView"]) {
        if ([view isKindOfClass:[DurationPopupView class]]) {
            self = (DurationPopupView *)view;

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            
            
            _btnFrom.date = [NSDate systemDate];
            _btnTo.date = [NSDate systemDate];
            
            _btnFrom.placeHolder = _btnFrom.dateString; //[CommonUtil formattedDate:[CommonUtil stringFromDate:[NSDate systemDate]]];
            _btnTo.placeHolder = _btnTo.dateString; //[CommonUtil formattedDate:[CommonUtil stringFromDate:[NSDate systemDate]]];
            
            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {
    
}

- (void)setCondition:(ItemModel *)condition {
    _condition = condition;
    
    if (_condition.itemSubTypeCode && [_condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDueDate]) {
        
//        ItemSubModel *subCondition = _condition.subItems && _condition.subItems.count ? _condition.subItems[0] : nil;
//        if (subCondition) {
//            //set values.
//            NSArray *darray = [subCondition.cmdclsValueMsg componentsSeparatedByString:@"~"];
//            
//            //from
//            [_btnFrom setTitle:darray[0] forState:UIControlStateNormal];
//            if (darray.count > 1) {//to
//                [_btnTo setTitle:darray[1] forState:UIControlStateNormal];
//            }
//        }
        
    } else {
        _condition.itemName = @"이 기간동안만";
        _condition.itemSubTypeCode = ksConditionSubTypeCodeDueDate;
    }
}


#pragma mark - UI Events

- (IBAction)btnConfirmTouched:(id)sender {
    //validate
    
    ItemSubModel *subCondition = nil;
    if (_condition.subItems && _condition.subItems.count) {
        subCondition = _condition.subItems[0];
    } else {
        subCondition = [[ItemSubModel alloc] init];
        
        _condition.subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
        [_condition.subItems addObject:subCondition];
    }
    
//    subCondition.sourceId = @"VAR";
    subCondition.conditionTypeCode = @"09";
    subCondition.cmdclsValue = [NSString stringWithFormat:@"%@~%@", _btnFrom.dateString, _btnTo.dateString];;
    subCondition.cmdclsValueMsg = [NSString stringWithFormat:@"%@ ~ %@", _btnFrom.formatString, _btnTo.formatString];;

    
    [super btnConfirmTouched:sender];
}

@end
