//
//  SeePurchaseNoticeViewController.m
//  XFFruit
//
//  Created by mac on 15/8/16.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "SeePurchaseNoticeViewController.h"
#import "ProductBillViewController.h"
#import "PurchaseBillProduct.h"
#define BottomHeight 50
#define LeftMargin 15
#define LeftWidth 100
#define LeftHeight 30
#define TopMargin 15

typedef enum : NSUInteger {
    EndTag = 3500,//结束
    NewTag,//新建
    AcceptTag,//接受
} BtnTag;


@interface SeePurchaseNoticeViewController ()

{
    UIScrollView *_scrollView;
    UIView *_purchaseView;
    UIView *_bottomView;
    ProductBillViewController *_pvc;
    NSArray *_leftArr;
    UIButton *_endBtn;
    UIButton *_rejectBtn;
}
@property (nonatomic,strong)UILabel *billNumberLabel;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *createOperNameLabel;
@property (nonatomic,strong)UILabel *userLabel;
@property (nonatomic,strong)UILabel *stateLabel;
@property (nonatomic,strong)UILabel *countLabel;
@property (nonatomic,strong)UILabel *noteLabel;
@end

@implementation SeePurchaseNoticeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self bulidLayout];
    [self getDataFromServer];
}
- (void)getDataFromServer{
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (data) {
            NSInteger success = [data[@"success"] integerValue];
            NSString *message  = data[@"message"] ;
            if (success == 1) {
                NSDictionary *dictData = data[@"data"];
                PurchaseNotice *purchaseNotice = [[PurchaseNotice alloc]init];
                [purchaseNotice setValuesForKeysWithDictionary:dictData];
                strongSelf.purchaseNotice = purchaseNotice;
                [strongSelf fetchtNoticeDetail];
            }else{
                [IBTLoadingView showTips:message];
            }
        }else{
            [IBTLoadingView showTips:@"     无记录     "];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] getPurchaseNoticeDetailedWithPurchaseUuid:self.purchaseNotice.uuid success:succ failure:fail];
}
- (void)fetchtNoticeDetail{
    self.billNumberLabel.text = [IBTCommon checkString:self.purchaseNotice.billnumber];
    if ([self.purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_NOTACCEPTED]) {
        self.stateLabel.textColor = [UIColor redColor];
        self.stateLabel.text = @"未接受";
    }else if ([self.purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_PURCHASEING]) {
        self.stateLabel.textColor = [UIColor greenColor];
        self.stateLabel.text = @"采购中";
    }else if ([self.purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_FINISHED]) {
        self.stateLabel.textColor = [UIColor grayColor];
        self.stateLabel.text = @"已完成";
    }
    self.createOperNameLabel.text = [NSString stringWithFormat:@"%@%@",[IBTCommon checkString:self.purchaseNotice.create_operName],self.purchaseNotice.create_time.length > 10 ? [self.purchaseNotice.create_time substringToIndex:10] : self.purchaseNotice.create_time];

    self.titleLabel.text = [IBTCommon checkString:self.purchaseNotice.title];
    self.userLabel.text = [IBTCommon checkString:self.purchaseNotice.purchaserName];
    self.countLabel.text = @"";
    self.noteLabel.text = [IBTCommon checkString:self.purchaseNotice.remark];
    
//    [_transView refreshTranProduct:self.transport.pdtDetails];
//    [_transView refreshCost:self.transport.accountDetails];
    [self setNoteHeight];
}


#pragma mark - 视图初始化
- (void)bulidLayout{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64 - BottomHeight)];
    _scrollView.showsHorizontalScrollIndicator  = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = XXFBgColor;
    
    [self.view addSubview:_scrollView];
    [self createBtn];
    [self createPurchaseView];
    [self createBottomView];
   

}
- (void)hiddenBottomBtn{
    
        _endBtn.hidden = YES;
        _rejectBtn.hidden = YES;
        CGRect scrollViewFrame = _scrollView.frame;
        scrollViewFrame.size.height = ScreenSize.height - 64;
        _scrollView.frame = scrollViewFrame;
}
- (void)createBtn{
    _rejectBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, ScreenSize.height  - 64 - BottomHeight +5, (ScreenSize.width - LeftMargin*3)/2, 40) target:self sel:@selector(btnClick:) tag:0 image:nil title:@"" titleColor: [UIColor whiteColor] isCorner:YES corner:5 bgColor:GXF_SAVE_COLOR];
    [self.view addSubview:_rejectBtn];
    
    _endBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(CGRectGetMaxX(_rejectBtn.frame)+ LeftMargin, ScreenSize.height  - 64 - BottomHeight +5, (ScreenSize.width - LeftMargin*3)/2, 40) target:self sel:@selector(btnClick:) tag:0 image:nil title:@"" titleColor:[UIColor whiteColor] isCorner:YES corner:5 bgColor:GXF_COMMIT_COLOR];
    [self.view addSubview:_endBtn];
    
    if ([self.purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_NOTACCEPTED]) {
        if ([self checkIsPermission:NOTICE_PERMISSIONS_ACCEPTTASK] && [self checkIsPermission:NOTICE_PERMISSIONS_CREATEBILL]) {
            _rejectBtn.tag = AcceptTag;
            [_rejectBtn setTitle:@"接受任务" forState:UIControlStateNormal];
            _endBtn.tag = NewTag;
            _endBtn.backgroundColor = [UIColor grayColor];
            [_endBtn setTitle:@"新建采购单" forState:UIControlStateNormal];
        }else{
            if ([self checkIsPermission:NOTICE_PERMISSIONS_ACCEPTTASK]) {
                _rejectBtn.tag = AcceptTag;
                [_rejectBtn setTitle:@"接受任务" forState:UIControlStateNormal];
            }
            if ([self checkIsPermission:NOTICE_PERMISSIONS_CREATEBILL]) {
                _endBtn.tag = NewTag;
                [_endBtn setTitle:@"新建采购单" forState:UIControlStateNormal];
            }
        }
    }else if([self.purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_PURCHASEING]){
        if([self checkIsPermission:NOTICE_PERMISSIONS_FINISH]){
            _rejectBtn.hidden = YES;
            _endBtn.frame = CGRectMake(LeftMargin, ScreenSize.height  - 64 - BottomHeight +5, ScreenSize.width - LeftMargin * 2, 40);
            _endBtn.tag = EndTag;
            [_endBtn setTitle:@"结束" forState:UIControlStateNormal];
        }else{
            [self hiddenBottomBtn];
        }
    }else if([self.purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_FINISHED]) {
        [self hiddenBottomBtn];
    }
}
- (BOOL)checkIsPermission:(NSString *)permission{
    for (NSString *per in [ICRUserUtil sharedInstance].permissions) {
        if ([per isEqualToString:permission]) {
            return YES;
        }
    }
    return NO;
}
- (void)btnClick:(UIButton *)btn{
    NSLog(@"%ld",(long)btn.tag);
    switch (btn.tag) {
        
        case EndTag:
        {
            NSLog(@"结束");
        }
            break;
        case NewTag:
        {
            NSLog(@"新建");
        }
            break;
        case AcceptTag:
        {
            NSLog(@"接受");
        }
            break;
        default:
            break;
    }
}

- (void)createBottomView{
    _bottomView= [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_purchaseView.frame) + TopMargin, ScreenSize.width, 200)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_bottomView];
    
    _pvc = [[ProductBillViewController alloc]init];
    [self addChildViewController:_pvc];
    
    _pvc.viewFrame = _bottomView.bounds;
    _pvc.isHiddenEdit = YES;
    [_bottomView addSubview:_pvc.view];
}

- (void)createPurchaseView{
    
    _leftArr = @[@"单号:",@"创建人:",@"标题:",@"状态:",@"状态:",@"采购员:",@"合计基础数量:",@"备注:"];
    _purchaseView= [[UIView alloc]initWithFrame:CGRectMake(0, TopMargin, ScreenSize.width, LeftHeight*_leftArr.count +LeftMargin)];
    _purchaseView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_purchaseView];
    
    for (NSInteger i = 0 ; i < _leftArr.count; i++) {
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin, 10 + LeftHeight *i, LeftWidth, LeftHeight)];
        leftLabel.font = GXF_SEVENTEENTH_SIZE;
        leftLabel.text = _leftArr[i];
        leftLabel.textColor = GXF_DETAIL_COLOR;
        [_purchaseView addSubview:leftLabel];
        
        UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftLabel.frame), 10 + LeftHeight *i, _purchaseView.frame.size.width - LeftMargin - LeftWidth, LeftHeight)];
        rightLabel.font = GXF_SEVENTEENTH_SIZE;
        rightLabel.textColor = GXF_DETAIL_COLOR;
        
        if (i == 0) {
            self.billNumberLabel = rightLabel;
        }else if (i == 1) {
            self.createOperNameLabel = rightLabel;
        }else if(i == 2){
            self.titleLabel = rightLabel;
        }else if(i == 3){
            self.stateLabel = rightLabel;
        }else if(i == 4){
            self.userLabel = rightLabel;
        }else if(i == 4){
            self.countLabel = rightLabel;
        }else if(i == _leftArr.count - 1){
            rightLabel.numberOfLines = 0;
            self.noteLabel = rightLabel;
        }
        [_purchaseView addSubview:rightLabel];
    }
}

- (void)fetchtPurchaseDetail{
//    self.billNumberLabel.text = [IBTCommon checkString:self.bill.billNumber];
//    if ([self.bill.state isEqualToString:PURCHASE_STATE_INITIAL]) {
//        self.stateLabel.textColor = [UIColor redColor];
//        self.stateLabel.text = @"未提交";
//    }else if ([self.bill.state isEqualToString:PURCHASE_STATE_SUBMITTED]) {
//        self.stateLabel.textColor = [UIColor greenColor];
//        self.stateLabel.text = @"已提交";
//    }else if ([self.bill.state isEqualToString:PURCHASE_STATE_REJECTED]) {
//        self.stateLabel.textColor = [UIColor grayColor];
//        self.stateLabel.text = @"已拒绝";
//    }else if ([self.bill.state isEqualToString:PURCHASE_STATE_SHIPPING]) {
//        self.stateLabel.textColor = [UIColor grayColor];
//        self.stateLabel.text = @"已审批";
//    }else if ([self.bill.state isEqualToString:PURCHASE_STATE_SHIPPING]) {
//        self.stateLabel.textColor = [UIColor grayColor];
//        self.stateLabel.text = @"发运中";
//    }else if ([self.bill.state isEqualToString:PURCHASE_STATE_FINISHED]) {
//        self.stateLabel.textColor = [UIColor blackColor];
//        self.stateLabel.text = @"已完成";
//    }
//    self.noticeNumberLabel.text = (self.bill.noticeNumber.length == 0) ? @"无":(self.bill.noticeNumber);
//    self.createOperNameLabel.text = [IBTCommon checkString:self.bill.create_operName];
//    self.checkNameLabel.text = [IBTCommon checkString:self.bill.vendor_name];
//    self.vendorNameLabel.text = [IBTCommon checkString:self.bill.vendor_name];
//    self.totalPriceLabel.text = [IBTCommon checkString:[self.bill.total stringValue]];
//    self.noteLabel.text = [IBTCommon checkString:self.bill.remark];
//    self.lastModifyNameLabel.text = [IBTCommon checkString:self.bill.lastModify_operName];
//    NSString *type = [self.bill.type isEqualToString:GXF_Critical] ? @"紧急" : @"普通";
//    self.typeLabel.text = type;
//    self.vendorIsSureLabel.text = self.bill.vendorConfirmTime.length > 0 ? @"是":@"否";
//    self.warehouseLabel.text = [IBTCommon checkString:self.bill.receiveWrh_name];
//    self.otherPriceLabel.text = [self.bill.charge stringValue];
//    NSMutableArray *productArr = [NSMutableArray array];
//    for (NSDictionary *billDict in self.bill.products) {
//        PurchaseBillProduct *billProbuct = [PurchaseBillProduct new];
//        [billProbuct setValuesForKeysWithDictionary:billDict];
//        [productArr addObject:billProbuct];
//    }
//    _pvc.productArr = productArr;
//    [_pvc.tableView reloadData];
    
    [self setNoteHeight];
}

- (void)setNoteHeight
{
    CGFloat height =  [self.noteLabel calculateHeight];
    
    CGRect noteFrame = self.noteLabel.frame;
    noteFrame.size.height = height;
    self.noteLabel.frame = noteFrame;
    
    CGFloat totalHeight = height + LeftHeight*_leftArr.count;
    CGRect purchaseFrame = _purchaseView.frame;
    purchaseFrame.size.height = totalHeight;
    _purchaseView.frame = purchaseFrame;
    
    
    CGRect bottomFrame = _bottomView.frame;
    bottomFrame.origin.y = CGRectGetMaxY(_purchaseView.frame) + TopMargin;
    _bottomView.frame = bottomFrame;
    
    _scrollView.contentSize = CGSizeMake(ScreenSize.width, totalHeight + CGRectGetHeight(_bottomView.frame) + TopMargin*2);
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
