//
//  SaleInputTopView.m
//  RealEstateManagement
//
//  Created by Javen on 2016/10/13.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "SaleInputTopView.h"
#import "CalculateHelper.h"
@interface SaleInputTopView ()<UITextFieldDelegate>
@end
@implementation SaleInputTopView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.textFieldDate.type = DateTextFieldYearMonthDay;
    self.textFieldDate.rangeType = DateTextFieldRangeBefore;
    WS(weakSelf);
    self.textFieldDate.blockEndEdit = ^{
        kCanNotBeNil(weakSelf.contract.uuid, @"请先选择合同!");
        [weakSelf httpToDayData];
    };
    
    if (!AppGlobal.isMall) {
        self.btnStore.userInteractionEnabled = NO;
        self.right1.hidden = YES;
        UserInfo_contracts *contract = AppGlobal.user.contracts[0];
        self.contract = [HMSaleInputDetail_contract modelWithDic:contract.toDictionary];
        [self.btnStore setTitle:contract.name forState:UIControlStateNormal];
        [self actionContract:nil];
    }
    
    //如果是每天早晨11点之前进入销售录入页面，则显示日期为昨天
    NSDate *dateNow = [NSDate date];
    //时间点字符串
    NSString *point = [dateNow.yearMonthDayString stringByAppendingString:@" 11:00:00"];
    NSDateFormatter *f = [NSDateFormatter new];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *datePoint = [f dateFromString:point];
    NSComparisonResult result = [dateNow compare:datePoint];
    
    if (result == NSOrderedAscending) {//小于
        NSDate *yesterDay = [NSDate dateWithTimeInterval:- 24 * 60 * 60 sinceDate:dateNow];
        self.textFieldDate.text = yesterDay.yearMonthDayString;
    }else{
        self.textFieldDate.text = [dateNow yearMonthDayString];
    }
}

- (IBAction)actionContract:(id)sender {
    if (!AppGlobal.isMall) {
        UserInfo_contracts *contract = [AppGlobal getTenantContract];
        self.contract = [HMSaleInputDetail_contract modelWithDic:contract.toDictionary];
        [self.btnStore setTitle:contract.name forState:UIControlStateNormal];
        [self httpToDayData];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择合同" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
        
        for (UserInfo_contracts *contract in AppGlobal.user.contracts) {
            NSString *title = [NSString stringWithFormat:@"%@【%@】", contract.name, contract.code];
            UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.contract = [HMSaleInputDetail_contract modelWithDic:contract.toDictionary];
                [self.btnStore setTitle:contract.name forState:UIControlStateNormal];
                [self httpToDayData];
            }];
            [alert addAction:action];
        }
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        
        [self.viewController.navigationController presentViewController:alert animated:YES completion:nil];
    }
}

/**
 *  获取今日的销售录入数据
 */
- (void)httpToDayData {
    if (self.contract == nil) {
        return;
    }
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.contract.uuid, @"contract", [NSDictionary dictionaryWithObjectsAndKeys:[self.textFieldDate.text stringByAppendingString:@" 00:00:00"], @"beginDate", [self.textFieldDate.text stringByAppendingString:@" 23:59:59"], @"endDate", nil], @"dateRange", nil];
    [MBProgressHUD j_loading];
    WS(weakSelf);
    [ZJHttpManager POST:@"salesinput/getSalesTotal" parameters:params complete:^(id responseObject, NSError *error) {
        [MBProgressHUD j_hideLoadingView];
        if (kIsResponse) {
            NSArray *data = responseObject[@"data"];
            
            if (IsNotNullObject(data) && data.count > 0) {
                NSDictionary *record = data[0];
                weakSelf.labelTodayTotal.text = [CalculateHelper getMoneyStringFrom:record[@"total"]];
            } else {
                //没有查到数据，默认显示0
                weakSelf.labelTodayTotal.text = @"0";
            }
        } else {
            kFalseHttpTips;
        }
    }];
}


@end
