//
//  TaskSeatViewController.m
//  Macalline
//
//  Created by JianjiYuan on 14-12-3.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "TaskSeatViewController.h"

@interface TaskSeatViewController ()

@property (nonatomic, strong) IBOutlet UILabel *lblPercent;
@property (nonatomic, strong) IBOutlet UILabel *lblOptional;
@property (nonatomic, strong) IBOutlet UILabel *lblRequired;
@property (nonatomic, strong) IBOutlet UILabel *lblStartDate;
@property (nonatomic, strong) IBOutlet UILabel *lblEndDate;
@property (nonatomic, strong) IBOutlet UILabel *lblStartWeekday;
@property (nonatomic, strong) IBOutlet UILabel *lblEndWeekday;
@property (nonatomic, strong) IBOutlet UILabel *lblLeftTime;
@property (nonatomic, strong) IBOutlet UILabel *lblLeftTimePre;

@end

@implementation TaskSeatViewController

- (void)dealloc
{
    self.lblPercent = nil;
    self.lblOptional = nil;
    self.lblRequired = nil;
    self.lblStartDate = nil;
    self.lblEndDate = nil;
    self.lblStartWeekday = nil;
    self.lblEndWeekday = nil;
    self.lblLeftTime = nil;
    self.lblLeftTimePre = nil;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)reloadDatas
{
    self.lblOptional.text = [NSString stringWithFormat:@"%d", [self.dictProgress[@"optional"] intValue]];
    self.lblRequired.text = [NSString stringWithFormat:@"%d", [self.dictProgress[@"required"] intValue]];
    
    if ([self.dictProgress[@"sum"] intValue] == 0) {
        self.lblPercent.text = @"0%";
    }else {
        int finished = [self.dictProgress[@"finishedOptional"] intValue]+[self.dictProgress[@"finishedRequired"] intValue];
        float percent = finished/[self.dictProgress[@"sum"] floatValue];
        self.lblPercent.text = [NSString stringWithFormat:@"%02d%@", (int)(percent*100), @"%"];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *startDate = [dateFormatter dateFromString:self.strStartDate];
    NSDate *endDate = [dateFormatter dateFromString:self.strEndDate];
    NSDate *currentDate = [NSDate date];
    
    if (!startDate || !endDate) {
        return;
    }
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *startComponents = [calendar components:NSCalendarUnitWeekday
                                                    fromDate:startDate];
    NSDateComponents *endComponents = [calendar components:NSCalendarUnitWeekday
                                                  fromDate:endDate];
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitDay fromDate:currentDate toDate:endDate options:0];
    NSInteger intervalDay = dateComponents.day;
    
    NSInteger startWeekday = startComponents.weekday;
    NSInteger endWeekday = endComponents.weekday;
    NSArray *arrWeekdays = @[@"星期日", @"星期一", @"星期二", @"星期三", @"星期四",
                             @"星期五", @"星期六"];
    self.lblStartWeekday.text = arrWeekdays[startWeekday-1];
    self.lblEndWeekday.text = arrWeekdays[endWeekday-1];
    
    self.lblStartDate.text = self.strStartDate;
    self.lblEndDate.text = self.strEndDate;
    
    if (intervalDay >= 0) {
        self.lblLeftTime.text = [NSString stringWithFormat:@"%d", (int)intervalDay];
        [DataMgr getInstance].isUploadActionExpired = NO;
    }else {
        self.lblLeftTime.text = [NSString stringWithFormat:@"%d", abs((int)intervalDay)];
        self.lblLeftTimePre.text = @"过期";
        [DataMgr getInstance].isUploadActionExpired = YES;
    }
    
    if (![[DataMgr getInstance].roleId isEqualToString:@"2"]) {//不是商场管理员 就没有过期限制
        [DataMgr getInstance].isUploadActionExpired = NO;
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
