//
//  SOSViewController.m
//  watch
//
//  Created by xTT on 2017/7/17.
//  Copyright © 2017年 xTT. All rights reserved.
//

#import "SOSViewController.h"
#import "SOS.h"//

@interface SOSViewController ()<UITextFieldDelegate>

@property (nonatomic ,weak) UITextField *phoneField1;
@property (nonatomic ,weak) UITextField *phoneField2;
@property (nonatomic ,weak) UITextField *phoneField3;

@end

@implementation SOSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.zx_navTitle = @"SOS号码";
    [self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [self rightBarItemClick];
    }];


    
}

- (void)subbgView
{
    UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:10 borderWidth:0 borderColor:KKWhiteColorColor];
    bgView.frame = CGRectMake(16, iPhoneX_NavHeight+15, ScreenWidth-32, self.myDataSource.count*Adapted(55));
    [self.view addSubview:bgView];
    
    for (int i = 0; i < self.myDataSource.count; i++)
    {
        SOS *sos = self.myDataSource[i];
        UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:Font_(16) textColor:KKBlack20 text:[NSString stringWithFormat:@"SOS号码%d",i+1] Radius:0];
        [bgView addSubview:titleLabel];
        CGRect ww = [UICommon GetTextWidth:titleLabel.text ViewHeight:Adapted(55) fontSize:Font_(16) type:@"w"];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView).offset(15);
            make.top.equalTo(bgView).offset(i*Adapted(55));
            make.size.mas_equalTo(CGSizeMake(ww.size.width+15, Adapted(55)));
        }];
        
        UITextField *fieldView = [UICommon ui_textField:CGRectZero textColor:KKBlack20 backColor:KKClearColor font:FontBold_(14) maxTextNum:11 placeholderColor:KKGrey121 placeholder:@"请输入SOS号码" toMaxNum:nil change:nil];
        fieldView.keyboardType = UIKeyboardTypePhonePad;
        fieldView.textAlignment = NSTextAlignmentRight;
        fieldView.delegate = self;
        fieldView.text = sos.phone;
        if (i == 0) self.phoneField1 = fieldView;
        else if (i == 1) self.phoneField2 = fieldView;
        else if (i == 2) self.phoneField3 = fieldView;
        [bgView addSubview:fieldView];
        [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_right).offset(15);
            make.right.equalTo(bgView.mas_right).inset(15);
            make.centerY.equalTo(titleLabel);
            make.height.mas_equalTo(Adapted(40));
        }];
        
        if (i < self.myDataSource.count-1)
        {
            UIImageView *line = [UIImageView new];
            line.backgroundColor = RGB(240, 240, 240);
            [bgView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(bgView).inset(15);
                make.bottom.equalTo(titleLabel.mas_bottom);
                make.height.mas_equalTo(0.5);
            }];
        }
    }
    
    UILabel *tiLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:Font_(12) textColor:KKGrey163 text:@"1、请为设备设置紧急号码，设备按下SOS按键，即可拨打上方设置的号码；\n\n2、如果您想删除SOS号码，请将号码删除，然后点击”保存“；\n\n3、设备使用SOS功能时，会依次拨打下方号码，轮打3遍结束" Radius:0];
    [self.view addSubview:tiLabel];
    [tiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(15);
        make.top.equalTo(bgView.mas_bottom).offset(15);
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    WEAKSELF
    [SOS getSOSSuccess:^(NSMutableArray *arr) {
        weakSelf.myDataSource = arr;
        [self subbgView];
    } failure:^{}];
}

- (void)rightBarItemClick
{
    for (int i = 0; i < self.myDataSource.count; i++)
    {
        SOS *sos = self.myDataSource[i];
        if (i == 0) sos.phone = self.phoneField1.text;
        else if (i == 1) sos.phone = self.phoneField2.text;
        else if (i == 2) sos.phone = self.phoneField3.text;
    }
    __block BOOL hasError = NO;
    [self.myDataSource enumerateObjectsUsingBlock:^(SOS * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.phone.length > 0) {
            if (obj.phone.length < 3 || obj.phone.length > 15) {
                hasError = YES;
                *stop = YES;
            }
        }
    }];
    if (hasError)
    {
        [SVProgressHUD showErrorWithStatus:@"号码为3~15位"];
    }
    else
    {
        [SOS saveSOS:self.myDataSource
             success:^{
                 //[self goBack:sender];
            [SVProgressHUD showWithStatus:@"保存成功"];
             } failure:^{}];
    }
}




- (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
