//
//  CompeteCell.m
//  gaotingshan
//
//  Created by MacBook Dq01 on 16/3/30.
//  Copyright © 2016年 Mac Book DQ02. All rights reserved.
//

#import "CompeteCell.h"
#import "ApplyModel.h"
#import "CreateModel.h"
#import "Singleton.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KMargin 10
#define kHeight 20

@interface CompeteCell () {
    Singleton *singleton;
}

@property (nonatomic, strong) UILabel *comNameLabel; //赛事名称
@property (nonatomic, strong) UILabel *organizerLable; //组织者
@property (nonatomic, strong) UILabel *timeLabel; //活动时间
@property (nonatomic, strong) UILabel *statusLable; //活动状态
@property (nonatomic, strong) UILabel *totalMileageLabel; //总里程
@property (nonatomic, strong) UILabel *curAppNumLabel; //当前参加人数

@end

@implementation CompeteCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //cell内部UI布局（6个label）
        _comNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KMargin, KMargin, kScreenWidth - 4*KMargin, kHeight)];
        [self addSubview:_comNameLabel];
        
        _organizerLable = [[UILabel alloc] initWithFrame:CGRectMake(KMargin, CGRectGetMaxY(_comNameLabel.frame), kScreenWidth - 4*KMargin, kHeight)];
        [self addSubview:_organizerLable];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(KMargin, CGRectGetMaxY(_organizerLable.frame), kScreenWidth - 4*KMargin, kHeight)];
        [self addSubview:_timeLabel];
        
        _statusLable = [[UILabel alloc] initWithFrame:CGRectMake(KMargin, CGRectGetMaxY(_timeLabel.frame), kScreenWidth - 4*KMargin, kHeight)];
        [self addSubview:_statusLable];
        
        _totalMileageLabel = [[UILabel alloc] initWithFrame:CGRectMake(KMargin, CGRectGetMaxY(_statusLable.frame), kScreenWidth - 4*KMargin, kHeight)];
        [self addSubview:_totalMileageLabel];
        
        _curAppNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(KMargin, CGRectGetMaxY(_totalMileageLabel.frame), kScreenWidth - 4*KMargin, kHeight)];
        [self addSubview:_curAppNumLabel];
        
    }
    return self;
}


#pragma mark - 封装数据, 实现show方法
- (void)show:(ApplyModel *)apply {
    _comNameLabel.text = [NSString stringWithFormat:@"赛事名称－%@", apply.eventName];
    _organizerLable.text = [NSString stringWithFormat:@"组织者:%@", apply.organizerName];
    
   //以下是对活动时间的处理（后台数据太坑，累死哥了！！！）
    NSString *time = [NSString stringWithFormat:@"%@", apply.eventStartTime];
//    NSLog(@"length:%ld", time.length);
    NSRange range = {0, 10};
    NSString *setTime = [time substringWithRange:range];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [numberFormatter numberFromString:setTime];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:number.floatValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *eventStartTime =[dateFormatter stringFromDate:date];
    _timeLabel.text = [NSString stringWithFormat:@"活动时间:%@", eventStartTime];
    
    //以下是对活动状态的处理
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSString *str = [formatter stringFromNumber:apply.eventStatus];
    if ([str isEqualToString:@"0"]) {
        NSString *status = @"未开始";
        _statusLable.text = [NSString stringWithFormat:@"活动状态:%@", status];

    } else if ([str isEqualToString:@"1"]) {
        NSString *status = @"进行中";
        _statusLable.text = [NSString stringWithFormat:@"活动状态:%@", status];
        
    } else if ([str isEqualToString:@"2"]) {
        NSString *status = @"已结束";
        _statusLable.text = [NSString stringWithFormat:@"活动状态:%@", status];
    }

//    NSLog(@"eventId：%@", [apply.organizerId class]);
    _totalMileageLabel.text = [NSString stringWithFormat:@"总里程:%@", apply.mileage];
    _curAppNumLabel.text = [NSString stringWithFormat:@"当前参加人数:%@", apply.number];
}


- (void)showCreate:(CreateModel *)create {
    //创建单例实例
    singleton = [Singleton defaultSingleton];
    
    _comNameLabel.text = [NSString stringWithFormat:@"赛事名称－%@", create.name];
    _organizerLable.text = [NSString stringWithFormat:@"组织者:%@", singleton.reallyName];
    
    //以下是对活动时间的处理（后台数据太坑，累死哥了！！！）
    NSString *time = [NSString stringWithFormat:@"%@", create.eventStartTime];
//    NSLog(@"length:%ld", time.length);
    NSRange range = {0, 10};
    NSString *setTime = [time substringWithRange:range];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [numberFormatter numberFromString:setTime];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:number.floatValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *eventStartTime =[dateFormatter stringFromDate:date];
    _timeLabel.text = [NSString stringWithFormat:@"活动时间:%@", eventStartTime];
    
    //以下是对活动状态的处理
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSString *str = [formatter stringFromNumber:create.status];
    if ([str isEqualToString:@"0"]) {
        NSString *status = @"未开始";
        _statusLable.text = [NSString stringWithFormat:@"活动状态:%@", status];
        
    } else if ([str isEqualToString:@"1"]) {
        NSString *status = @"进行中";
        _statusLable.text = [NSString stringWithFormat:@"活动状态:%@", status];
        
    } else if ([str isEqualToString:@"2"]) {
        NSString *status = @"已结束";
        _statusLable.text = [NSString stringWithFormat:@"活动状态:%@", status];
    }
    
    _totalMileageLabel.text = [NSString stringWithFormat:@"总里程:%@", create.mileage];
    _curAppNumLabel.text = [NSString stringWithFormat:@"当前参加人数:%@", create.number];
}








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

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

    // Configure the view for the selected state
}

@end
