//
//  SortItemCell.m
//  iSales-NPD
//
//  Created by Jack on 16/9/5.
//  Copyright © 2016年 United Software Applications, Inc. All rights reserved.
//

#import "SortItemCell.h"


@interface SortItemCell ()

@property (nonatomic,strong) UIButton *selectedView;

@end

@implementation SortItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self addSubview:self.selectedView];
        [self addSubview:self.sortTitleLabel];
        self.sortIndex = YES;
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    CGFloat w = CGRectGetWidth(frame);
    CGFloat h = CGRectGetHeight(frame);
    
    self.sortTitleLabel.frame = CGRectMake(40, 0, w - 15, h);
    self.selectedView.frame = CGRectMake(10, (h - 18) / 2, 18, 18);
}

- (UILabel *)sortTitleLabel {
    if (!_sortTitleLabel) {
        CGFloat w = CGRectGetWidth(self.bounds);
        CGFloat h = CGRectGetHeight(self.bounds);
        
        _sortTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, w - 15, h)];
        _sortTitleLabel.backgroundColor = [UIColor clearColor];
        _sortTitleLabel.font = [UIFont systemFontOfSize:17];
        _sortTitleLabel.textColor = [UIColor blackColor];
        _sortTitleLabel.textAlignment = NSTextAlignmentLeft;
        
    }
    return _sortTitleLabel;
}

- (UIButton *)selectedView {
    if (!_selectedView) {

        CGFloat h = CGRectGetHeight(self.bounds);
        _selectedView = [[UIButton alloc] initWithFrame:CGRectMake(10, (h - 18) / 2, 18, 18)];
        
        _selectedView.userInteractionEnabled = NO;
    }
    return _selectedView;
}

- (void)setSortTitle:(NSString *)sortTitle {
    _sortTitle = sortTitle;
    
    self.sortTitleLabel.text = sortTitle;
   
}

- (void)setSelectedSort:(BOOL)selectedSort {
    _selectedSort = selectedSort;

    self.selectedView.selected = selectedSort;
}

- (void)setSortIndex:(NSInteger)sortIndex {
    _sortIndex = sortIndex;
    
    NSString *selectedImageName = @"";
    switch (sortIndex) {
        case 0:{
            selectedImageName = @"TX_18";
        }
            break;
        case 1:{
            selectedImageName = @"TS_18";
        }
            break;
        case 2:{
            selectedImageName = @"IX_18";
        }
            break;
        case 3:{
            selectedImageName = @"IS_18";
        }
            break;
        case 4:{
            selectedImageName = @"DX_18";
        }
            break;
            
        default:
            break;
    }
    [self.selectedView setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
}


@end
