//
//  TopSheetView.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/8/12.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "TopSheetView.h"
#define LeftMargin 15
#define TopMargin 10
#define LeftWidth 80
#define ContentHeight 25
#define SpaceHeight 10

@interface TopSheetView ()<UITextFieldDelegate,HPGrowingTextViewDelegate>

@end


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

#pragma mark - 布局
- (void)bulidLayout
{
    
    NSArray *arr = @[@"标题:",@"供应商:",@"联系人:",@"联系电话:",@"加工工厂:",@"备注:"];
    for (NSInteger i = 0; i < arr.count; i++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, LeftWidth, ContentHeight)];
        label.text = arr[i];
        label.font = FontSize(17);
        [self addSubview:label];
        if (i == 0) {
            self.titleTextFiled = [self getTextFileWithFrame:CGRectMake(LeftWidth+LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, self.frame.size.width - LeftMargin*2-LeftWidth, ContentHeight) title:@"请输入标题"];
        }else if (i == 1){
            self.supplierBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.supplierBtn.frame = CGRectMake(LeftWidth+LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, self.frame.size.width - LeftMargin*2-LeftWidth, ContentHeight);
            [self addSubview:self.supplierBtn];
            self.supplierLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width - LeftMargin*4-LeftWidth, ContentHeight)];
            self.supplierLabel.font = FontSize(15);
            self.supplierLabel.backgroundColor = [UIColor redColor];
            [self.supplierBtn addSubview:self.supplierLabel];
        }else if (i == 2){
            self.peopleTextFiled = [self getTextFileWithFrame:CGRectMake(LeftWidth+LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, self.frame.size.width - LeftMargin*2-LeftWidth, ContentHeight) title:@"请输入联系人"];
            
        }else if (i == 3){
         self.phoneTextFiled = [self getTextFileWithFrame:CGRectMake(LeftWidth+LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, self.frame.size.width - LeftMargin*2-LeftWidth, ContentHeight) title:@"请输入联系电话"];
        }else if (i == 4){
            self.factoryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            self.factoryBtn.frame = CGRectMake(LeftWidth+LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, self.frame.size.width - LeftMargin*2-LeftWidth, ContentHeight);
            [self addSubview:self.factoryBtn];
            self.factoryLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width - LeftMargin*4-LeftWidth, ContentHeight)];
            self.factoryLabel.font = FontSize(15);
            self.factoryLabel.backgroundColor = [UIColor redColor];
            [self.factoryBtn addSubview:self.factoryLabel];
        }else if (i == 5){
           self.remarkTextView =  [[HPGrowingTextView alloc] initWithFrame:CGRectMake(LeftWidth+LeftMargin, TopMargin + (ContentHeight + SpaceHeight)*i, self.frame.size.width - LeftMargin*2-LeftWidth, ContentHeight)];
            self.remarkTextView.contentInset = UIEdgeInsetsMake(5, 5, 5, 0);
            self.remarkTextView.minNumberOfLines = 1;
            self.remarkTextView.maxNumberOfLines = 2;
            self.remarkTextView.font = FontSize(15);
            self.remarkTextView.textAlignment = NSTextAlignmentLeft;
            self.remarkTextView.delegate = self;
            self.remarkTextView.returnKeyType = UIReturnKeyDone;
            self.remarkTextView.placeholder = @"输入备注内容";
            [self addSubview:self.remarkTextView];
        }
    }
}

- (UITextField *)getTextFileWithFrame:(CGRect)frame title:(NSString *)title{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.textColor = HexColor(@"444444");
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = title;
    textField.font = FontSize(15);
    textField.delegate = self;
    textField.returnKeyType = UIReturnKeyDone;
    [self addSubview:textField];
    return textField;
}

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

@end
