//
//  TopTransportView.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/9/6.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "TopTransportView.h"
#import "SurveyCell.h"
#import "Warehouse.h"
#import "ChooseWarehouseViewController.h"
#import "ChooseTypeViewController.h"

#define LeftMargin 15
#define TopMargin 20
#define LeftWidth 80
#define TableHeight 44
#define SpaceHeight 10

@interface TopTransportView ()<UITableViewDataSource,UITableViewDelegate,HPGrowingTextViewDelegate,UITextFieldDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
}
@end


@implementation TopTransportView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //界面
        [self bulidLayout];
    }
    return self;
}

#pragma mark - 布局
- (void)bulidLayout
{
    self.backgroundColor  = XXFBgColor;
    _dataArr = [NSMutableArray array];
    NSArray *arr = @[@"发货仓库:",@"收货仓库:",@"运输类型:",@"车辆:",@"司机电话:",@"备注:"];
    [_dataArr addObjectsFromArray:arr];
    
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, TopMargin,self.frame.size.width, self.frame.size.height - TopMargin)) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self addSubview:_tableView];
}
#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    SurveyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[SurveyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row ==0 || indexPath.row == 1 || indexPath.row == 2) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        [self createViewInCell:cell indexPath:indexPath];
    }
    [cell setTitleStr:_dataArr[indexPath.row]];
    return cell;
}
-  (void)createViewInCell:(SurveyCell *)cell indexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3 || indexPath.row == 4) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-10, TableHeight)];
        textField.textAlignment = NSTextAlignmentRight;
        textField.textColor = GXF_CONTENT_COLOR;
        textField.font = GXF_FIFTEENTEN_SIZE;
        textField.returnKeyType = UIReturnKeyDone;
        textField.delegate = self;
        [cell.contentView addSubview:textField];
        if (indexPath.row == 3) {
            self.carTextFiled = textField;
            self.carTextFiled.placeholder = @"请输入车辆";
        }else if(indexPath.row == 4){
            textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            textField.returnKeyType = UIReturnKeyDone;
            self.phoneTextFiled = textField;
            self.phoneTextFiled.placeholder = @"请输入司机电话";
        }
        
    }else if (indexPath.row == _dataArr.count -1){
        self.remarkTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-10, TableHeight)];
        self.remarkTextView.contentInset = UIEdgeInsetsMake(5, 5, 5, 0);
        self.remarkTextView.minNumberOfLines = 1;
        self.remarkTextView.maxNumberOfLines = 1;
        self.remarkTextView.isScrollable = YES;
        self.remarkTextView.font = GXF_FIFTEENTEN_SIZE;
        self.remarkTextView.textAlignment = NSTextAlignmentRight;
        self.remarkTextView.delegate = self;
        self.remarkTextView.returnKeyType = UIReturnKeyDone;
        self.remarkTextView.placeholder = @"输入备注内容";
        [cell.contentView addSubview:self.remarkTextView];
    }else{
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:(CGRectMake(100+LeftMargin, 0, ScreenSize.width - 100 - LeftMargin*2-10, TableHeight))];
        contentLabel.textAlignment= NSTextAlignmentRight;
        contentLabel.textColor = GXF_PLACEHOLDER_COLOR;
        contentLabel.font = GXF_FIFTEENTEN_SIZE;
        [cell.contentView addSubview:contentLabel];
        if (indexPath.row == 0) {
            contentLabel.text = @"选择发货仓库";
            self.warehouseLabel = contentLabel;
            
        }else if(indexPath.row == 1){
            contentLabel.text = @"选择收货仓库";
            self.rwarehouseLabel = contentLabel;
            
        }else if(indexPath.row == 2){
            contentLabel.text = @"选择类型";
            self.typeLabel = contentLabel;
            
        }
    }
}
- (void)setTransport:(Transport *)transport{
    if (transport) {
        if (transport.warehouseName.length > 0) {
            self.warehouseLabel.text = transport.warehouseName;
            self.warehouseLabel.textColor = GXF_CONTENT_COLOR;
            self.warehouseCode = transport.warehouseCode;
            self.warehouseName = transport.warehouseName;
            self.warehouseUuid = transport.warehouseUuid;
        }
        if (transport.type.length > 0) {
            NSString *type = [transport.type isEqualToString:GXF_Critical] ? @"紧急" : @"普通";
            self.typeLabel.text = type;
            self.typeLabel.textColor = GXF_CONTENT_COLOR;
            self.type = transport.type;
        }
        if (transport.rwarehouseName.length > 0) {
            self.rwarehouseLabel.text = transport.rwarehouseName;
            self.rwarehouseLabel.textColor = GXF_CONTENT_COLOR;
            self.rwarehouseCode = transport.rwarehouseCode;
            self.rwarehouseName = transport.rwarehouseName;
            self.rwarehouseUuid = transport.rwarehouseUuid;
        }
        if (transport.carnumber.length > 0) {
            self.carTextFiled.text = transport.carnumber;
            self.carnumber = transport.carnumber;
        }
        if (transport.carphone) {
            self.phoneTextFiled.text = transport.carphone;
            self.carphone = transport.carphone;
        }
        if (transport.note) {
            self.remarkTextView.text = transport.note;
            self.note = transport.note;
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        ChooseWarehouseViewController *cvc = [ChooseWarehouseViewController new];
        cvc.choseBaseInfo = ^(NSArray *warehouses){
            if (warehouses.count > 0) {
                Warehouse *warehouse = warehouses[0];
                self.warehouseLabel.text = [NSString stringWithFormat:@"%@[%@]",warehouse.name,warehouse.code];
                self.warehouseLabel.textColor = GXF_CONTENT_COLOR;
                self.warehouseUuid = warehouse.uuid;
                self.warehouseCode = warehouse.code;
                self.warehouseName = warehouse.name;
            }
            
        };
        cvc.isMoreChose = NO;
        [self.delegate pushNextViewController:cvc];
    }else if(indexPath.row == 1){
        ChooseWarehouseViewController *tvc = [[ChooseWarehouseViewController alloc]init];
        tvc.choseBaseInfo = ^(NSArray *warehouses){
            if (warehouses.count > 0) {
                Warehouse *warehouse = warehouses[0];
                self.rwarehouseLabel.text = [NSString stringWithFormat:@"%@[%@]",warehouse.name,warehouse.code];
                self.rwarehouseLabel.textColor = GXF_CONTENT_COLOR;
                self.rwarehouseUuid = warehouse.uuid;
                self.rwarehouseCode = warehouse.code;
                self.rwarehouseName = warehouse.name;
            }
            
        };
        [self.delegate pushNextViewController:tvc];
    }else if(indexPath.row == 2){
        ChooseTypeViewController *tvc = [[ChooseTypeViewController alloc]init];
        tvc.choseBaseInfo = ^(NSArray *types){
            if (types.count > 0) {
                NSString *type = types[0];
                self.typeLabel.text = type;
                self.typeLabel.textColor = GXF_CONTENT_COLOR;
                if ([type isEqualToString:@"紧急"]) {
                    self.type = GXF_Critical;
                }else{
                    self.type = GXF_Normal;
                }
            }
            
        };
        [self.delegate pushNextViewController:tvc];
    }

}

- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    [self.remarkTextView resignFirstResponder];
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.delegate hiddenKeyBoard];
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
