// // AddRailViewController.m // LekangGuard // // Created by ecell on 2022/12/12. // #import "AddRailViewController.h" #import "RailNameTagViewController.h" #import "RailAddressViewController.h" #import "MoreRailAddressViewController.h" @interface AddRailViewController () /// 地址标签 @property (nonatomic ,weak) UIButton *nameBtn; /// 详细地址 @property (nonatomic ,weak) UIButton *addressBtn; /// 开始时间 @property (nonatomic ,weak) UIButton *startTime; /// 结束时间 @property (nonatomic ,weak) UIButton *endTime; @property (nonatomic ,strong) NSMutableArray *btnArr; @property (nonatomic ,weak) UIButton *duoBtn; @property (nonatomic ,weak) UIButton *yuanBtn; @property (nonatomic ,weak) UIButton *anBtn; @property (nonatomic ,weak) UIButton *weiBtn; /// 围栏类型 @property (nonatomic ,assign) NSInteger railType; /// 安全区域 @property (nonatomic ,assign) NSInteger property; @end @implementation AddRailViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = self.isRedact == 1 ? GJText(@"添加电子围栏") : GJText(@"编辑电子围栏"); if (self.isRedact == 1) { self.railType = 1; self.property = 1; self.railModel = [[SafetyRailModel alloc] init]; } else { self.railType = self.railModel.type; self.property = self.railModel.property; } [self zx_setRightBtnWithText:GJText(@"保存") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self AddRails]; }]; kWeakSelf(self) NSArray *arr = @[GJText(@"围栏类型"),GJText(@"区域类型"),GJText(@"围栏名称"),GJText(@"地址"),GJText(@"有效时间"),GJText(@"星期")]; NSArray *timeArr = @[GJText(@"一"),GJText(@"二"),GJText(@"三"),GJText(@"四"),GJText(@"五"),GJText(@"六"),GJText(@"日")]; UIView *bgView = [UICommon ui_view:CGRectMake(0, iPhoneX_NavHeight, SCREEN_WIDTH, Adapted(55)*arr.count) backgroundColor:KKWhiteColorColor cornerRadius:0 borderWidth:0 borderColor:KKWhiteColorColor]; [self.view addSubview:bgView]; for (int i = 0; i < arr.count; i++) { UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(14) textColor:KKTextBlackColor text:arr[i] Radius:0]; [bgView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(15); make.top.equalTo(bgView).offset(i*Adapted(55)); make.height.mas_equalTo(Adapted(55)); }]; if (i == 0) { if (self.isRedact == 1) { UIButton *duoBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(14) normalColor:KKTextBlackColor normalText:@" 多边形" click:^(UIButton *btn) { self.railType = 2; [self selectbtnImg:btn :self.yuanBtn]; if (self.railModel.boundaryCoordinates.count > 0) { [self.addressBtn setTitle:self.railModel.address forState:0]; [self.addressBtn setTitleColor:KKTextBlackColor forState:0]; } else { [self.addressBtn setTitle:@"请设置详细地址" forState:0]; [self.addressBtn setTitleColor:KKGrey121 forState:0]; } }]; self.duoBtn = duoBtn; [bgView addSubview:duoBtn]; [duoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(80, 40)); }]; UIButton *yuanBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(14) normalColor:KKTextBlackColor normalText:@" 圆形" click:^(UIButton *btn) { self.railType = 1; [self selectbtnImg:btn :self.duoBtn]; if (self.railModel.lat.length > 0 && self.railModel.lon.length) { [self.addressBtn setTitle:self.railModel.address forState:0]; [self.addressBtn setTitleColor:KKTextBlackColor forState:0]; } else { [self.addressBtn setTitle:@"请设置详细地址" forState:0]; [self.addressBtn setTitleColor:KKGrey121 forState:0]; } }]; self.yuanBtn = yuanBtn; [bgView addSubview:yuanBtn]; [yuanBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(duoBtn.mas_left).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(80, 40)); }]; if (self.railType == 1) [self selectbtnImg:self.yuanBtn :self.duoBtn]; else [self selectbtnImg:self.duoBtn :self.yuanBtn]; } else { UIButton *duoBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(14) normalColor:KKTextBlackColor normalText:self.railModel.type == 1 ? @" 圆形" : @" 多边形" click:^(UIButton *btn) { [self selectbtnImg:btn :nil]; }]; [duoBtn setImage:ImageName_(@"comm_selectBtn_Select") forState:UIControlStateNormal]; [bgView addSubview:duoBtn]; [duoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(80, 40)); }]; } } else if (i == 1) { UIButton *weiBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(14) normalColor:KKTextBlackColor normalText:@" 危险区域" click:^(UIButton *btn) { self.property = 2; [self selectbtnImg:btn :self.anBtn]; }]; self.weiBtn = weiBtn; [bgView addSubview:weiBtn]; [weiBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(100, 40)); }]; UIButton *anBtn = [UICommon ui_buttonSimple:CGRectZero font:Font_(14) normalColor:KKTextBlackColor normalText:@" 安全区域" click:^(UIButton *btn) { self.property = 1; [self selectbtnImg:btn :self.weiBtn]; }]; self.anBtn = anBtn; [bgView addSubview:anBtn]; [anBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weiBtn.mas_left).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(100, 40)); }]; if (self.property == 1) [self selectbtnImg:self.anBtn :self.weiBtn]; else [self selectbtnImg:self.weiBtn :self.anBtn]; } else if (i > 1 &&i < 4) { UIImageView *rightImg = [UICommon ui_imageView:CGRectZero fileName:@"icon_enter_gray"]; [bgView addSubview:rightImg]; [rightImg mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(Adapted(rightImg.image.size.width), Adapted(rightImg.image.size.height))); }]; NSString *text; if (i == 2) text = self.isRedact == 1 ? GJText(@"请设置地址名称") : self.railModel.name; if (i == 3) text = self.isRedact == 1 ? GJText(@"请设置详细地址") : self.railModel.address; UIButton *rightBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:self.isRedact == 1 ? KKGrey121 : KKTextBlackColor normalText:text click:^(UIButton *btn) { if (btn.tag == 2) { RailNameTagViewController *vc = [RailNameTagViewController new]; vc.addressName = weakself.nameBtn.currentTitle; vc.isAddressName = ^(NSString * _Nonnull name) { [weakself.nameBtn setTitle:name forState:0]; self.railModel.name = name; [weakself.nameBtn setTitleColor:KKTextBlackColor forState:0]; }; [weakself.navigationController pushViewController:vc animated:YES]; } else { if (self.railType == 1) { RailAddressViewController *vc = [RailAddressViewController new]; vc.railModel = self.railModel; vc.isRedact = self.railModel.lat.length > 0 && self.railModel.lon.length > 0 ? 2 : self.isRedact; vc.isAddressModel = ^(SafetyRailModel * _Nonnull model) { [weakself.addressBtn setTitle:model.address forState:0]; self.railModel.address = model.address; self.railModel.lat = model.lat; self.railModel.lon = model.lon; self.railModel.radius = model.radius; [weakself.addressBtn setTitleColor:KKTextBlackColor forState:0]; }; [self.navigationController pushViewController:vc animated:YES]; } else { MoreRailAddressViewController *vc = [[MoreRailAddressViewController alloc] init]; vc.railModel = self.railModel; vc.isRedact = self.railModel.boundaryCoordinates.count > 0 ? 2 : self.isRedact; vc.isAddressModel = ^(SafetyRailModel * _Nonnull model) { [weakself.addressBtn setTitle:model.address forState:0]; self.railModel.address = model.address; // self.railModel.lat = model.lat; // self.railModel.lon = model.lon; [weakself.addressBtn setTitleColor:KKTextBlackColor forState:0]; }; [self.navigationController pushViewController:vc animated:YES]; } } }]; rightBtn.tag = i; rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; if (i == 2) self.nameBtn = rightBtn; if (i == 3) self.addressBtn = rightBtn; [bgView addSubview:rightBtn]; [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(rightImg.mas_left).inset(5); make.centerY.equalTo(titleLabel); make.left.equalTo(bgView).offset(100); make.height.mas_equalTo(Adapted(48)); }]; } else if (i == 4) { UIButton *endTime = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKTextBlackColor normalText:self.isRedact == 2 ? self.railModel.endTime : @"17:00" click:^(id x) { [BRDatePickerView showDatePickerWithMode:BRDatePickerModeHM title:GJText(@"选择结束时间") selectValue:self.endTime.currentTitle minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) { [weakself.endTime setTitle:selectValue forState:0]; }]; }]; endTime.layer.borderWidth = 0.5; endTime.layer.borderColor = KKGrey219.CGColor; endTime.layer.cornerRadius = Adapted(16); self.endTime = endTime; [bgView addSubview:endTime]; [endTime mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(Adapted(60), Adapted(32))); }]; UIImageView *line1 = [UIImageView new]; line1.backgroundColor = KKGrey219; [bgView addSubview:line1]; [line1 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(endTime.mas_left).inset(5); make.centerY.equalTo(endTime); make.size.mas_equalTo(CGSizeMake(Adapted(15), 0.5)); }]; UIButton *startTime = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKTextBlackColor normalText:self.isRedact == 2 ? self.railModel.startTime : @"08:00" click:^(id x) { [BRDatePickerView showDatePickerWithMode:BRDatePickerModeHM title:GJText(@"选择开始时间") selectValue:self.startTime.currentTitle minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) { [weakself.startTime setTitle:selectValue forState:0]; }]; }]; startTime.layer.borderWidth = 0.5; startTime.layer.borderColor = KKGrey219.CGColor; startTime.layer.cornerRadius = Adapted(16); self.startTime = startTime; [bgView addSubview:startTime]; [startTime mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(line1.mas_left).inset(5); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(Adapted(60), Adapted(32))); }]; } else { if (self.isRedact == 2) self.btnArr = [self getWeekArr:self.railModel.week]; else self.btnArr = [[NSMutableArray alloc] initWithArray:@[@"1",@"1",@"1",@"1",@"1",@"0",@"0"]]; for (int j = 0; j < timeArr.count; j++) { CGRect rect = [timeArr[j] boundingRectWithSize:CGSizeMake(MAXFLOAT, Adapted(32)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FontADA_(13)} context:nil]; CGFloat ww = 32; if (rect.size.width > 32) ww = rect.size.width+10; UIButton *button = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKTextBlackColor normalText:timeArr[j] click:^(UIButton *btn) { btn.selected = !btn.selected; btn.backgroundColor = btn.selected ? KKMainColor : KKClearColor; btn.layer.borderColor = btn.selected ? KKMainColor.CGColor : KKGrey219.CGColor; NSString *str = btn.selected ? @"1" : @"0"; [self.btnArr replaceObjectAtIndex:btn.tag withObject:str]; NSLog(@"%@",self.btnArr); }]; if (self.isRedact == 2) button.selected = [self.btnArr[j] isEqualToString:@"1"] ? YES : NO; else button.selected = j < 5 ? YES : NO; button.tag = j; button.layer.borderWidth = 0.5; button.layer.borderColor = button.selected ? KKMainColor.CGColor : KKGrey219.CGColor; button.layer.cornerRadius = Adapted(16); button.backgroundColor = button.selected ? KKMainColor : KKClearColor; [button setTitleColor:KKTextBlackColor forState:UIControlStateNormal]; [button setTitleColor:KKWhiteColorColor forState:UIControlStateSelected]; [bgView addSubview:button]; [button mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(Adapted(ww), Adapted(32))); make.left.equalTo(titleLabel.mas_right).offset(Adapted(20)+Adapted(10)*j+Adapted(ww)*j); }]; } } if (i < arr.count-1) { UIImageView *line = [UIImageView new]; line.backgroundColor = KKLineColor; [bgView addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(15); make.right.equalTo(bgView.mas_right); make.top.equalTo(titleLabel.mas_bottom); make.height.mas_equalTo(0.5); }]; } } } #pragma mark 新增电子围栏 /// 新增电子围栏 - (void)AddRails { if ([self.nameBtn.currentTitle isEqualToString:@"请设置地址名称"]) { [UICommon MessageErrorText:@"请设置地址名称"]; return; } if ([self.addressBtn.currentTitle isEqualToString:@"请设置详细地址"]) { [UICommon MessageErrorText:@"请设置详细地址"]; return; } [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:APIManager.sharedManager.deviceModel.imei forKey:@"imei"]; [parameters setValue:self.endTime.currentTitle forKey:@"endTime"]; [parameters setValue:self.startTime.currentTitle forKey:@"startTime"]; [parameters setValue:self.railModel.address forKey:@"address"]; [parameters setValue:self.nameBtn.currentTitle forKey:@"name"]; [parameters setValue:[self.btnArr componentsJoinedByString:@""] forKey:@"week"]; [parameters setValue:@(self.property) forKey:@"property"]; NSString *urlStr; if (self.railType == 1) { /// 圆形电子围栏 [parameters setValue:self.railModel.lat forKey:@"lat"]; [parameters setValue:self.railModel.lon forKey:@"lon"]; [parameters setValue:@(self.railModel.radius) forKey:@"radius"]; urlStr = self.isRedact == 1 ? AddRails_URL : UpdateRails_URL; } if (self.railType == 2) { /// 多边形电子围栏 [parameters setValue:self.railModel.boundaryCoordinates forKey:@"coordinateDTOS"]; urlStr = self.isRedact == 1 ? AddRails2Polygon_URL : UpdateRails2Polygon_URL; } if (self.isRedact == 2) { /// 编辑电子围栏 [parameters setValue:self.railModel.Id forKey:@"id"]; [parameters setValue:self.railModel.status forKey:@"status"]; } [[[APIManager sharedManager] APPOST:urlStr parameters:parameters isJson:YES resultClass:nil] subscribeNext:^(id _Nullable x) { [UICommon HidenLoading]; self.isUpdataModel(); [self.navigationController popViewControllerAnimated:YES]; } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } - (NSMutableArray *)getWeekArr:(NSString *)string { NSMutableArray *weekArr = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@""]]; [string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) { [weekArr replaceObjectAtIndex:substringRange.location withObject:substring]; }]; return weekArr; } - (void)selectbtnImg:(UIButton *)btn :(UIButton *)btn1 { [btn setImage:ImageName_(@"comm_selectBtn_Select") forState:UIControlStateNormal]; [btn1 setImage:ImageName_(@"comm_selectBtn_Normal") forState:UIControlStateNormal]; } /* #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