//
//  GXFProcessViewController.m
//  XFFruit
//
//  Created by freecui on 15/9/1.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//



#import "GXFNewProcessViewController.h"
#import "GXFBottomView.h"
#import "GXFTableViewCell.h"
#import "GXFButtonAndLineView.h"
#import "GXFSearchVendorViewController.h"
#import "Vendor.h"
#import "Warehouse.h"
#import "GXFProcessBill.h"
#import "GXFProcessMaterial.h"
#import "GXFProcessProduct.h"
#import "GXFPopNewMaterialViewController.h"
#import "GXFPopNewProductViewController.h"

#import "GXFProcessMaterialTableViewCell.h"
#import "GXFProcessProductTableViewCell.h"
#import "ProductBillCell.h"
typedef NS_ENUM(NSUInteger, processType){
    kMaterial = 0,
    kProduct
};
typedef NS_ENUM(NSInteger, cellRow) {
    kTitle = 0,
    kVendor,
    kContact,
    kContactPhone,
    kWarehouse,
    kTotal,
    kRemark,
};

@interface GXFNewProcessViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,GXFBottomViewDelegate>
{
    NSMutableArray *_selectRowArr;//记录当前选中的cell
}
@property (nonatomic, strong) UIScrollView *c_scrollView;
@property (nonatomic, strong) UITableView *c_tableView;
@property (nonatomic, strong) IBTSegmentContainer *c_segContainer;
@property (nonatomic, strong) IBTSegmentedControl *c_segmentControl;
@property (nonatomic, strong) GXFPopNewMaterialViewController *c_materialVC;
@property (nonatomic, strong) GXFButtonAndLineView *c_materialView;
@property (nonatomic, strong) GXFButtonAndLineView *c_productView;

@property (nonatomic, strong) NSArray *d_arrRightTableV;
@property (nonatomic, strong) NSArray *d_arrLeftTableV;
@property (nonatomic, strong) NSMutableArray *d_materialMuArr;
@property (nonatomic, strong) NSMutableArray *d_productMuArr;
@property (nonatomic, strong) GXFProcessBill *d_processBill;
@property (nonatomic, assign) BOOL d_isProduct;
@property (nonatomic, strong) NSArray *d_meteilHeaderArr;
@property (nonatomic, strong) NSArray *d_productHeaderArr;
@property (nonatomic, strong) NSMutableArray *d_meteilArr;
@property (nonatomic, strong) NSMutableArray *d_productArr;

@property (nonatomic, assign) NSInteger d_editOrDelete;


@property (nonatomic,assign)BOOL isHiddenEdit;

@end

@implementation GXFNewProcessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"添加加工单";
    [self initData];
    [self initSubViews];
}

//刷新数据
- (void)p_refreshData {
    NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
    [self.c_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)addNewMaterail: (NSNotification *)aNotification {
    GXFProcessMaterial *material = aNotification.object;
    if (_d_meteilArr) {
        [_d_meteilArr addObject:material];
        [self p_refreshData];
    }
}
- (void)addNewProduct: (NSNotification *)aNotification {
    GXFProcessProduct *product = aNotification.object;
    if (_d_productArr) {
        [_d_productArr addObject:product];
        [self p_refreshData];
    }
}
- (void)editProduct: (NSNotification *)aNotification {
    GXFProcessProduct *product = aNotification.object;
    if (_d_productArr) {
        [_d_productArr replaceObjectAtIndex:_d_editOrDelete withObject:product];//待做：优化为刷新这一条
        [self p_refreshData];
    }
}
- (void)deleteProduct: (NSNotification *)aNotification {
    GXFProcessProduct *product = aNotification.object;
    if (_d_productArr) {
        [_d_productArr removeObject:product];////待做：优化为刷新这一条
        [self p_refreshData];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNewMaterail:) name:KNOTIFICATION_saveMaterial object:nil];

    //货物明细
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNewProduct:) name:KNOTIFICATION_saveProduct object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editProduct:) name:KNOTIFICATION_editProduct object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteProduct:) name:KNOTIFICATION_deleteProduct object:nil];
//
//    //保存
//    void(^succ)(id) = ^(id data) {
//        [IBTLoadingView hideHUDWithText:nil];
////        if (data) {
////            NSInteger success = [data[@"success"] integerValue];
////            NSString *message = data[@"message"];
////            if (success == 1) {
////                //成功
////                if ([state isEqualToString:@"initial"]) {//保存成功
////                    [IBTLoadingView showTips:@"操作成功"];
////                }else{//提交成功
////                    SurveyViewController *svc = [SurveyViewController new];
////                    [self PushViewController:svc animated:YES];
////                }
////            }else{
////                [IBTLoadingView showTips:message];
////            }
////        }
//    };
//    void(^fail)(id) = ^(id data) {
//        [IBTLoadingView hideHUDWithText:nil];
//        [IBTLoadingView showTips:data];
//    };
//    
////    if(self.survey.uuid.length > 0 && uuidObject == [NSNull null]){
////        uuidObject = self.survey.uuid;
////    }
////    if (self.survey.billNumber > 0 && billNumberObject == [NSNull null]) {
////        billNumberObject = self.survey.billNumber;
////    }
////    if (self.survey && [version integerValue] == 0) {
////        version = self.survey.version;
////    }
//    
//    NSDictionary *material = @{@"uuid":@"",@"productUuid":@"",@"productCode":@"",@"productName":@"",@"instockDate":@"",@"place":@"",@"quality":@"",@"baseQty":@"",@"basePrice":@"",@"total":@"",@"remark":@"",};
//    NSDictionary *product = @{@"uuid":@"",@"productUuid":@"",@"productCode":@"",@"productName":@"",@"instockDate":@"",@"place":@"",@"quality":@"",@"baseQty":@"",@"basePrice":@"",@"total":@"",@"remark":@"",};
//    NSDictionary *dict = @{@"uuid":@"",
//                           @"version":@(0),
//                           @"billnumber":@"",
//                           @"title":@"title",//[IBTCommon checkString:self.titleStr],
//                           @"vendor_uuid":_d_processBill.vendor_uuid,
//                           @"vendor_code":_d_processBill.vendor_code,
//                           @"vendor_name":_d_processBill.vendor_name,
//                           @"warehouse_uuid":_d_processBill.warehouse_uuid,
//                           @"warehouse_code":_d_processBill.warehouse_code,
//                           @"warehouse_name":_d_processBill.warehouse_name,
//                           @"contact":[IBTCommon checkString: _d_processBill.contact],
//                           @"contactPhone":[IBTCommon checkString:_d_processBill.contact],
//                           @"total":_d_processBill.total,
//                           @"remark":[IBTCommon checkString:_d_processBill.remark],
//                           @"materials":@[material],
//                           @"products":@[product]};
//    //[IBTLoadingView showProgressLabel:msg];
//    [[ICRHTTPController sharedController] saveSurveyWithAuthenticode:dict success:succ failure:fail];
}

- (void)initData {
    self.d_arrLeftTableV =@[@"标题：",@"供应商：",@"联系人：",@"联系电话：",@"加工工厂：",@"总费用：",@"备注："];
    self.d_arrRightTableV = @[@"请输入标题",@"选择供应商",@"请输入联系人姓名",@"请输入联系电话",@"选择加工工厂",@"",@"请输入备注"];
    self.d_meteilHeaderArr = @[@"原料",@"基础数量",@"入库时间"];
    self.d_productHeaderArr = @[@"原料",@"基础数量",@"出库时间"];
     _selectRowArr = [[NSMutableArray alloc]init];
    self.d_processBill = [[GXFProcessBill alloc]init];
    
    if (!self.d_meteilArr) {
        self.d_meteilArr = [NSMutableArray array];
    }
    GXFProcessMaterial *processM = [[GXFProcessMaterial alloc]init];
    processM.uuid = @"00";
    processM.productName = @"Material";
    processM.productUuid = @"01";
    processM.baseQty = @(10);
    processM.instockDate = [NSDate date];
    [self.d_meteilArr addObject:processM];
    [self.d_meteilArr addObject:processM];
    
    
    if (!self.d_productArr) {
        self.d_productArr = [NSMutableArray array];
    }
    GXFProcessProduct *processP = [[GXFProcessProduct alloc]init];
    processP.uuid = @"11";
    processP.productName = @"product";
    processP.productUuid = @"12";
    processP.baseQty = @(10);
    processP.outstockDate = [NSDate date];
    [self.d_productArr addObject:processP];

    NSDictionary *dict = [processP dictForCommit];
}
- (void)initSubViews {
    self.view.backgroundColor = XXFBgColor;
    self.c_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - BottomViewHeight - NavigationBarHeight) style:UITableViewStyleGrouped];
    self.c_tableView.dataSource = self;
    self.c_tableView.delegate = self;
    [self.view addSubview:_c_tableView];
    [_c_tableView reloadData];
    
    GXFBottomView *bottomV = [[GXFBottomView alloc]initWithFrame:CGRectMake(0, _c_tableView.bottom, self.view.width, BottomViewHeight) LeftBtnTitle:@"结束" rightBtnTitle:@"保存"];
    bottomV.delegate = self;
    [self.view addSubview:bottomV];

}

#pragma UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return _d_arrLeftTableV.count;
    } else {
        if (_d_isProduct) {
            return 2 + _d_productArr.count;
        }
        return 2 + _d_meteilArr.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (indexPath.row > 1) {
            if ([self isHaveIndexPath:indexPath]) {
                return 200;
            } else {
                return GXF_TABLEVIEW_NOMALCELL_HEIGHT;
            }
        } else {
            return GXF_TABLEVIEW_NOMALCELL_HEIGHT;
        }
    } else {
        return GXF_TABLEVIEW_NOMALCELL_HEIGHT;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        UIView * headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
        headV.backgroundColor = [UIColor whiteColor];

        GXFButtonAndLineView *materialView = [[GXFButtonAndLineView alloc]initViewLineAndButtonWithFrame:CGRectMake(0, 0, self.view.width * 0.5, headV.height )  isSeleted:!_d_isProduct buttonTitle: @"原料明细" selectColor:GXF_GREEN_COLOR];        [materialView.f_btn addTarget:self action:@selector(selecNewMaterial) forControlEvents:UIControlEventTouchUpInside];
        self.c_materialView = materialView;
       GXFButtonAndLineView *productView = [[GXFButtonAndLineView alloc]initViewLineAndButtonWithFrame:CGRectMake(self.view.width * 0.5, 0, materialView.width, materialView.height)   isSeleted:_d_isProduct buttonTitle: @"成品明细" selectColor:GXF_GREEN_COLOR];
        [productView.f_btn addTarget:self action:@selector(selecNewMaterial) forControlEvents:UIControlEventTouchUpInside];
        self.c_productView= productView;

        [headV addSubview:_c_materialView];
        [headV addSubview:_c_productView];
        [headV addSubview:productView];
        return headV;

    }
        
}


- (UIView *)viewLineAndButtonWithFrame: (CGRect)frame
                             isSeleted: (BOOL)isSelected
                           buttonTitle: (NSString *)title
                           selectColor: (UIColor *)color
                               // target: (id)target
                                action:(SEL)action
                      forControlEvents:(UIControlEvents)controlEvents
{
    UIView *view = [[UIView alloc]initWithFrame:frame];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, view.width, view.height - 1);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:controlEvents];
    
    CGSize labelSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: btn.titleLabel.font}];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake((view.width - labelSize.width ) * 0.5, btn.bottom, labelSize.width, 1)];
    if (isSelected) {
        [btn setTitleColor:GXF_GREEN_COLOR forState:UIControlStateNormal];
         line.backgroundColor = GXF_GREEN_COLOR;
        
    } else {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        line.backgroundColor = [UIColor whiteColor];
    }

    [view addSubview:line];
    [view addSubview:btn];
    
    return view;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    } else {
        return 44;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GXFTableViewCell *xfCell ;
    if (indexPath.section == 0) {
        if (indexPath.row == kTitle || indexPath.row == kContact ||
            indexPath.row == kContactPhone ||indexPath.row == kRemark) {
            xfCell = [tableView dequeueReusableCellWithIdentifier:noLblCell];
            if (!xfCell ) {
                xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:noLblCell textFieldPlaceholder:_d_arrRightTableV[indexPath.row] isEdit:YES];
            }
        } else if (indexPath.row == kVendor || indexPath.row ==kWarehouse ) {
            xfCell = [tableView dequeueReusableCellWithIdentifier:arrowCell];
                    if ( !xfCell) {
                                    xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:arrowCell textFieldPlaceholder:_d_arrRightTableV[indexPath.row] isEdit:NO];
                        //xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableVie<#(NSString *)#>wCellStyleDefault reuseIdentifier:arrowCell];
                    }
            xfCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else if (indexPath.row == kTotal ){
            xfCell = [tableView dequeueReusableCellWithIdentifier:lblCell];
            if (!xfCell ) {
                xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lblCell textFieldPlaceholder:_d_arrRightTableV[indexPath.row] hasLable:YES backLableText:@"元"];
            }
        } else {
            xfCell = [tableView dequeueReusableCellWithIdentifier:noLblCell];
            if (!xfCell ) {
                xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:noLblCell textFieldPlaceholder:_d_arrRightTableV[indexPath.row] isEdit:YES];
            }
        }

        if (xfCell.f_textField) {
            xfCell.f_textField.delegate = self;
        }
        xfCell.f_textField.tag = indexPath.row;
        xfCell.textLabel.text = _d_arrLeftTableV[indexPath.row];
        xfCell.textLabel.textColor = [UIColor blackColor];
        xfCell.selectionStyle = UITableViewCellAccessoryNone;
        return xfCell;
    } else {
        if (indexPath.row == 0) {
            xfCell = [tableView dequeueReusableCellWithIdentifier:addDetailCell];
            
            if (!xfCell) {
                xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addDetailCell];
                UIButton *addDetailedButton=[UIButton  buttonWithType: UIButtonTypeCustom];
                [addDetailedButton  setFrame:CGRectMake(xfCell.width * 0.5, 0, xfCell.width * 0.5, xfCell.height)];
                [addDetailedButton setTitle:@"+ 点击添加商品明细" forState:UIControlStateNormal];
                [addDetailedButton addTarget:self action:@selector(addNewInfoDetailed) forControlEvents:UIControlEventTouchUpInside];
                [addDetailedButton setTitleColor:HexColor(@"444444") forState:UIControlStateNormal];
                [xfCell.contentView addSubview:addDetailedButton];
                }
            
            xfCell.selectionStyle = UITableViewCellAccessoryNone;
            return xfCell;
            } else if ( indexPath.row == 1){
                 xfCell = [tableView dequeueReusableCellWithIdentifier:addHeaderCell];
                if (!xfCell) {
                    NSArray *arr = [NSArray arrayWithArray: _d_isProduct ? _d_productHeaderArr :_d_meteilHeaderArr] ;
                    if (arr) {
                        xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addHeaderCell contentArray:arr];
                    } else {
                        xfCell = [[GXFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addHeaderCell];
                    }
                    
   
                }
            
                xfCell.selectionStyle = UITableViewCellAccessoryNone;
                return xfCell;

            }else  {
                static NSString *cellID = @"ProductCell";
                
                    GXFProcessMaterialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
                
                
                if (cell == nil) {
                    if (_d_isProduct) {
                        GXFProcessProduct *processP = (GXFProcessProduct *)_d_productArr[indexPath.row - 2];
                        cell = [[GXFProcessProductTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID processProduct:processP];
                    } else {
                    GXFProcessMaterial *pm = (GXFProcessMaterial *)_d_meteilArr[indexPath.row - 2];
                    cell = [[GXFProcessMaterialTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID processMaterial:pm];
                    }
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    if (self.isHiddenEdit) {
                        cell.editBtn.hidden = YES;
                    }
                }
                if ([self isHaveIndexPath:indexPath]) {
                    cell.smallImageView.image = [UIImage imageNamed:@"arrowdown"];
                    CGRect Linefrmame = cell.lineLabel.frame;
                    Linefrmame.origin.y = 200-1;
                    cell.lineLabel.frame = Linefrmame;
                    CGRect showfrmame = cell.showView.frame;
                    showfrmame.size.height = 150;
                    cell.showView.frame = showfrmame;
                    cell.backgroundColor = XXFBgColor;
                }else{
                    cell.smallImageView.image = [UIImage imageNamed:@"arrowright"];
                    CGRect Linefrmame = cell.lineLabel.frame;
                    Linefrmame.origin.y = GXF_TABLEVIEW_NOMALCELL_HEIGHT-1;
                    cell.lineLabel.frame = Linefrmame;
                    CGRect showfrmame = cell.showView.frame;
                    showfrmame.size.height = 0;
                    cell.showView.frame = showfrmame;
                    cell.backgroundColor = [UIColor whiteColor];
                }
                cell.editBtn.tag = indexPath.row;
                [cell.editBtn addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
                return cell;

            }
           
    }
}

#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        GXFTableViewCell *xfCell = (GXFTableViewCell *)[_c_tableView cellForRowAtIndexPath:indexPath];
        switch (indexPath.row) {
            case kVendor:
            {
                GXFSearchVendorViewController *vc = [[GXFSearchVendorViewController alloc]initWithTitle:[IBTCommon localizableString:@"VendorTitle"]];
                vc.choseVendor = ^(NSArray *vendors){
                    if (vendors.count > 0) {
                        Vendor *vendor = vendors[0];
                        xfCell.f_textField.text = [NSString stringWithFormat:@"%@[%@]",vendor.name,vendor.code];
                        _d_processBill.vendor_uuid = vendor.uuid;
                        _d_processBill.vendor_code = vendor.code;
                        _d_processBill.vendor_name = vendor.name;
                    }
                };
                [self PushViewController:vc animated:YES];
            }
                break;
            case kWarehouse:
            {
                GXFSearchVendorViewController *vc = [[GXFSearchVendorViewController alloc]initWithTitle:[IBTCommon localizableString:@"WarehouseTitle"]];
                vc.choseVendor = ^(NSArray *vendors){
                    if (vendors.count > 0) {
                        Warehouse *warehouse = vendors[0];
                        xfCell.f_textField.text = [NSString stringWithFormat:@"%@[%@]",warehouse.name,warehouse.code];
                        _d_processBill.warehouse_uuid = warehouse.uuid;
                        _d_processBill.warehouse_code = warehouse.code;
                        _d_processBill.warehouse_name = warehouse.name;
                    }
                };
                [self PushViewController:vc animated:YES];
            }
                
                break;
                
            default:
                break;
        }

    } else {
    
    ProductBillCell *cell = (ProductBillCell *)[tableView cellForRowAtIndexPath:indexPath];
       // ProductCell *cell = (ProductCell *)[tableView cellForRowAtIndexPath:indexPath];
        CGRect Linefrmame = cell.lineLabel.frame;
        CGRect showfrmame = cell.showView.frame;
        if (Linefrmame.origin.y == GXF_TABLEVIEW_NOMALCELL_HEIGHT - 1) {
            cell.smallImageView.image = [UIImage imageNamed:@"arrowdown"];
            
            Linefrmame.origin.y = 200-1;
            showfrmame.size.height = 150;
            cell.backgroundColor = [UIColor whiteColor];
            [_selectRowArr addObject:indexPath];
        }else{
            cell.smallImageView.image = [UIImage imageNamed:@"arrowright"];
            Linefrmame.origin.y = GXF_TABLEVIEW_NOMALCELL_HEIGHT -1;
            showfrmame.size.height = 0;
            cell.backgroundColor = [UIColor whiteColor];
            [_selectRowArr removeObject:indexPath];
        }
        cell.lineLabel.frame = Linefrmame;
        cell.showView.frame = showfrmame;
        [self p_refreshData];
//        NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
//        [self.c_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

    
    }
}

- (BOOL)isHaveIndexPath:(NSIndexPath *)indexPath{
    for (NSIndexPath *path in _selectRowArr) {
        if (path.row == indexPath.row) {
            return YES;
        }
    }
    return NO;
}
#pragma buttonAction 
- (void)addNewInfoDetailed {

    if (_d_isProduct) {
        GXFPopNewProductViewController *productVC = [[GXFPopNewProductViewController alloc]init];
        [self PushViewController:productVC animated:NO];
    } else {
    GXFPopNewMaterialViewController *materialVC = [[GXFPopNewMaterialViewController alloc]init];
    [self PushViewController:materialVC animated:YES];
    }
  
}
- (void)editClick: (UIButton *)btn{
    if (_d_isProduct) {
        self.d_editOrDelete = btn.tag - 2;
        GXFProcessProduct *product = _d_productArr[btn.tag - 2];
        GXFPopNewProductViewController *productVC = [[GXFPopNewProductViewController alloc]initWithProcessProduct:product isEdit: YES];
        [self PushViewController:productVC animated:NO];
    } else {
    
    }
}
- (void)selecNewMaterial {
    self.d_isProduct = !_d_isProduct;
    if (!_d_isProduct) {
        [self.c_materialView.f_btn setTitleColor:GXF_GREEN_COLOR forState:UIControlStateNormal];
        self.c_materialView.f_line.backgroundColor = GXF_GREEN_COLOR;
        
        [self.c_productView.f_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.c_productView.f_line.backgroundColor = [UIColor whiteColor];
        
    } else {
        [self.c_materialView.f_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.c_materialView.f_line.backgroundColor = [UIColor whiteColor];
        
        [self.c_productView.f_btn setTitleColor:GXF_GREEN_COLOR forState:UIControlStateNormal];
        self.c_productView.f_line.backgroundColor = GXF_GREEN_COLOR;
    }

    [self p_refreshData];
//    NSIndexSet *indexSet = [[NSIndexSet alloc]initWithIndex:1];
//     [self.c_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma GXFBottomViewDelegate
- (void)bottomViewleftButtonClicked:(UIButton *)leftBtn {
    CLog(@"ffff");
    //[IBTCommon checkString:_d_processBill.title]
    if ([IBTCommon checkStringIsNilOrSpance:_d_processBill.title]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"标题不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if ([IBTCommon checkStringIsNilOrSpance:_d_processBill.vendor_name]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请选择供应商" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }

    if ([IBTCommon checkStringIsNilOrSpance:_d_processBill.warehouse_name]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请选择加工工厂" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    
    
    
        //保存
        void(^succ)(id) = ^(id data) {
            [IBTLoadingView hideHUDWithText:nil];
    //        if (data) {
    //            NSInteger success = [data[@"success"] integerValue];
    //            NSString *message = data[@"message"];
    //            if (success == 1) {
    //                //成功
    //                if ([state isEqualToString:@"initial"]) {//保存成功
    //                    [IBTLoadingView showTips:@"操作成功"];
    //                }else{//提交成功
    //                    SurveyViewController *svc = [SurveyViewController new];
    //                    [self PushViewController:svc animated:YES];
    //                }
    //            }else{
    //                [IBTLoadingView showTips:message];
    //            }
    //        }
        };
        void(^fail)(id) = ^(id data) {
            [IBTLoadingView hideHUDWithText:nil];
            [IBTLoadingView showTips:data];
        };
    
    //    if(self.survey.uuid.length > 0 && uuidObject == [NSNull null]){
    //        uuidObject = self.survey.uuid;
    //    }
    //    if (self.survey.billNumber > 0 && billNumberObject == [NSNull null]) {
    //        billNumberObject = self.survey.billNumber;
    //    }
    //    if (self.survey && [version integerValue] == 0) {
    //        version = self.survey.version;
    //    }
    
//        NSDictionary *material = @{@"uuid":@"",@"productUuid":@"",@"productCode":@"",@"productName":@"",@"instockDate":@"",@"place":@"",@"quality":@"",@"baseQty":@"",@"basePrice":@"",@"total":@"",@"remark":@"",};
//        NSDictionary *product = @{@"uuid":@"",@"productUuid":@"",@"productCode":@"",@"productName":@"",@"instockDate":@"",@"place":@"",@"quality":@"",@"baseQty":@"",@"basePrice":@"",@"total":@"",@"remark":@"",};
    NSMutableArray *productMuArr = [NSMutableArray array];
    for (int count = 0; count < _d_productArr.count; count ++) {
        GXFProcessProduct *product = (GXFProcessProduct *)_d_productArr[count];
        [productMuArr addObject:[product dictForCommit]];
    }
    
    NSMutableArray *materialMuArr = [NSMutableArray array];
    for (int count = 0; count < _d_productArr.count; count ++) {
        GXFProcessMaterial *material = (GXFProcessMaterial *)_d_materialMuArr[count];
        [materialMuArr addObject:[material dictForCommit]];
    }

    
        NSDictionary *dict = @{@"uuid":@"",
                               @"version":@(0),
                               @"billnumber":@"",
                               @"title":@"title",//[IBTCommon checkString:self.titleStr],
                               @"vendor_uuid":_d_processBill.vendor_uuid,
                               @"vendor_code":_d_processBill.vendor_code,
                               @"vendor_name":_d_processBill.vendor_name,
                               @"warehouse_uuid":_d_processBill.warehouse_uuid,
                               @"warehouse_code":_d_processBill.warehouse_code,
                               @"warehouse_name":_d_processBill.warehouse_name,
                               @"contact":[IBTCommon checkString: _d_processBill.contact],
                               @"contactPhone":[IBTCommon checkString:_d_processBill.contact],
                               @"total":_d_processBill.total,
                               @"remark":[IBTCommon checkString:_d_processBill.remark],
                               @"materials":materialMuArr,
                               @"products":productMuArr};
        //[IBTLoadingView showProgressLabel:msg];
        [[ICRHTTPController sharedController] saveSurveyWithAuthenticode:dict success:succ failure:fail];

}
- (void)bottomViewRightButtonClicked:(UIButton *)rightBtn {
    
}

/*
 kTitle = 0, 不为空
 kVendor, 不为空
 kContact,
 kContactPhone,
 kWarehouse, 不为空
 kTotal,
 kRemark,
 */

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    switch (textField.tag) {
        case kTitle:
            self.d_processBill.title = textField.text;
            break;
        case kVendor:
           // self.d_processBill.title = textField.text;
            break;
        case kContact:
            self.d_processBill.contact = textField.text;
            break;
        case kContactPhone:
            self.d_processBill.contactPhone = textField.text;
            break;
        case kTotal:
            self.d_processBill.total = @([textField.text floatValue]);
            break;
        case kRemark:
            self.d_processBill.remark = textField.text;
            break;
        default:
            break;
    }
    [textField resignFirstResponder];
    
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self textFieldShouldReturn:textField];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
