//
//  SaleInputListViewModel.m
//  HDMall
//
//  Created by Javen on 2017/8/8.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "SaleInputListViewModel.h"
#import "NSDate+Additions.h"
#define KDateRangeTypeWeek @"最近一周"
#define KDateRangeTypeOneMonth @"最近一月"
#define KDateRangeTypeThreeMonth @"最近三月"
@implementation SaleInputListViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.arrDateTitles = @[KDateRangeTypeWeek, KDateRangeTypeOneMonth, KDateRangeTypeThreeMonth];
        self.arrStateTitles = @[@"全部",@"未生效", @"已生效"];
        self.arrDropMenu = @[self.arrDateTitles, self.arrStateTitles];
        
        //第一次请求，默认是最近一周
        NSDate *end = [NSDate date];
        NSDate *begin = [NSDate dateWithTimeInterval:-7 * 24 * 60 * 60 sinceDate:end];
        self.query.contracts = [AppGlobal getUserContractUuids];
        self.query.dateRange = [HMSaleInputQuery_dateRange new];
        self.query.dateRange.beginDate = begin.yearMonthDayString;
        self.query.dateRange.endDate = end.yearMonthDayString;
    }
    return self;
}

- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    return 2;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow {
    return [self.arrDropMenu[column] count];
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column {
    return [self.arrDropMenu[column] firstObject];
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    return [self.arrDropMenu[indexPath.column] objectAtIndex:indexPath.row];
}

- (BOOL)haveRightTableViewInColumn:(NSInteger)column {
    return NO;
}

- (CGFloat)widthRatioOfLeftColumn:(NSInteger)column {
    return 1;
}

- (NSInteger)currentLeftSelectedRow:(NSInteger)column {
    if (column == 0) {
        return self.dateTag;
    } else {
        return self.stateTag;
    }
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    NSString *title = [self.arrDropMenu[indexPath.column] objectAtIndex:indexPath.row];
    if (indexPath.column == 0) {
        self.dateTag = indexPath.row;
        NSDate *end = [NSDate date];
        NSDate *begin = nil;
        if ([title isEqualToString:KDateRangeTypeWeek]) {
            begin = [NSDate dateWithTimeInterval:-7 * 24 * 60 * 60 sinceDate:end];
        }else if ([title isEqualToString:KDateRangeTypeOneMonth]){
            begin = [NSDate dateWithTimeInterval:-30 * 24 * 60 * 60 sinceDate:end];
        }else if ([title isEqualToString:KDateRangeTypeThreeMonth]){
            begin = [NSDate dateWithTimeInterval:-90 * 24 * 60 * 60 sinceDate:end];
        }
        self.query.dateRange.beginDate = begin.yearMonthDayString;
        self.query.dateRange.endDate = end.yearMonthDayString;
    } else {
        self.stateTag = indexPath.row;
        if (indexPath.row == 0) {
            self.query.state = nil;
        }else{
            self.query.state = [SaleInputState stateWithText:title].code;
        }
    }
    self.blockRequest();
}

ZJLazy(HMSaleInputQuery, query);

@end
