| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- //
- // SetCategoryPriceController.m
- // iSales-NPD
- //
- // Created by Jack on 2016/10/11.
- // Copyright © 2016年 United Software Applications, Inc. All rights reserved.
- //
- #import "SetCategoryPriceController.h"
- #import "const.h"
- #import "EnumSelectViewController.h"
- #import "RANetwork.h"
- #define Percent 100
- #define Float 101
- #define NoChange 102
- #define NUMBERS @"0123456789.\n"
- #define NEGATIVE_NUMBERS @"0123456789.-\n"
- @interface SetCategoryPriceController ()<UITextFieldDelegate>
- @property (strong, nonatomic) IBOutlet UIButton *basePriceButton;
- @property (strong, nonatomic) IBOutlet UIButton *floatCheckButton;
- @property (strong, nonatomic) IBOutlet UIButton *percentageCheckButton;
- @property (strong, nonatomic) IBOutlet UITextField *floatTextField;
- @property (strong, nonatomic) IBOutlet UITextField *percentageTextField;
- @property (nonatomic,assign) NSInteger changePrice;
- @property (nonatomic,assign) NSInteger priceType;
- @end
- @implementation SetCategoryPriceController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- self.changePrice = NoChange;
- self.basePriceButton.enabled = NO;
- [self configAppearance];
-
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)configAppearance {
- UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector( returnBackClick:)];
-
- self.navigationItem.leftBarButtonItem = backButton;
-
- // base price
- // NSString *basePrice = [self.categoryPrice objectForKey:@"base_price"];
- // [self changePriceType:basePrice.integerValue];
- [self changePriceType:1];
-
- // discount
- NSString *discount = [self.categoryPrice objectForKey:@"price_discount"];
- if ([discount hasSuffix:@"%"]) {
- self.percentageTextField.text = [discount substringToIndex:discount.length - 1];
- } else {
- self.floatTextField.text = discount;
- }
- }
- - (void)returnBackClick:(id)sender {
-
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)changePriceType:(NSInteger)type {
-
- NSString *title = [RASingleton sharedInstance].deliveryString;
- switch (type) {
- case 0: {
- title = [RASingleton sharedInstance].deliveryString;
- }
- break;
- case 1: {
- title = flat_price;
- }
- break;
-
- default:
- break;
- }
-
- self.priceType = type;
- [self.basePriceButton setTitle:title forState:UIControlStateNormal];
-
- }
- - (NSString *)checkPriceType:(NSInteger)priceType {
- return self.priceType == priceType ? @"1" : @"0";
- }
- - (NSInteger)checkedCadedate:(NSDictionary *)cadedate {
-
- int priceType = 0;
- int count = [[cadedate objectForKey:@"count"] intValue];
- for (int i = 0; i < count; i++) {
- NSDictionary *dic = [cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]];
- int check = [[dic objectForKey:@"check"] intValue];
- if (check) {
-
- priceType = [[dic objectForKey:@"value_code"] intValue];
-
- break;
- }
- }
-
- return priceType;
- }
- - (IBAction)basePriceButtonClick:(UIButton *)sender {
-
- EnumSelectViewController* enumvc =[[UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"];
-
- enumvc.max_select = 1;
-
-
- enumvc.cadedate = @{
- @"count" : @"2",
- @"val_0" : @{@"value" : [RASingleton sharedInstance].deliveryString,@"value_code" : @"0",@"check" : [self checkPriceType:0]},
- @"val_1" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]}
- }.mutableCopy;
- enumvc.title = @"";
-
-
- enumvc.single_select =true;
-
- __weak typeof(self) weakSelf = self;
-
- enumvc.returnValue = ^(NSMutableDictionary* value){
-
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
- [strongSelf changePriceType:[strongSelf checkedCadedate:value]];
- }
-
- };
-
- [self.navigationController pushViewController:enumvc animated:true];
- }
- - (IBAction)percentageCheckButtonClick:(UIButton *)sender {
-
-
- if (self.changePrice == NoChange || self.changePrice == Percent) {
- sender.selected = !sender.selected;
- if (sender.selected) {
- self.changePrice = Percent;
- } else {
- self.changePrice = NoChange;
- }
- } else if (self.changePrice == Float) {
- sender.selected = YES;
- self.changePrice = Percent;
- self.floatCheckButton.selected = NO;
- self.floatTextField.enabled = NO;
- }
-
- self.percentageTextField.enabled = sender.selected;
-
- }
- - (IBAction)floatCheckButtonClick:(UIButton *)sender {
-
- if (self.changePrice == NoChange || self.changePrice == Float) {
- sender.selected = !sender.selected;
- if (sender.selected) {
- self.changePrice = Float;
- } else {
- self.changePrice = NoChange;
- }
-
- } else if (self.changePrice == Percent) {
- sender.selected = YES;
- self.changePrice = Float;
- self.percentageCheckButton.selected = NO;
- self.percentageTextField.enabled = NO;
- }
- self.floatTextField.enabled = sender.selected;
- }
- - (IBAction)saveButtonClick:(UIButton *)sender {
-
- // 向服务器提交数据
- // http://192.168.0.126:8080/site/isales/setSeeGivenPrice.htm
-
- NSString *discount = @"";
- BOOL rightDiscount = YES;
- switch (self.changePrice) {
- case Float:{
- discount = self.floatTextField.text;
- if (!discount || !discount.length) {
- rightDiscount = NO;
- }
- }
- break;
- case Percent:{
- discount = [self.percentageTextField.text stringByAppendingString:@"%"];
- if (!discount || discount.length <= 1) {
- rightDiscount = NO;
- }
- }
- break;
-
- default:
- break;
- }
-
-
- if (rightDiscount) {
-
- // __block UIAlertController* waitalert = [RAUtils waiting_alert:self title:@"Set Price" completion:^{
- PopWaitAlert * pop = [RAUtils waiting_pop:@"Set Price" completion:nil];
- [RANetwork request_update_npd_shop_givenprice:@{@"base_price" : [NSString stringWithFormat:@"%ld",self.priceType],@"discount" : discount,@"category_id" : @(self.categoryID)}.mutableCopy completionHandler:^(NSMutableDictionary *result) {
- NSDictionary *dic = result;
-
- // [waitalert dismissViewControllerAnimated:YES completion:^{
-
- [pop hide];
- // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
-
- if ([[dic objectForKey:@"result"] integerValue] == 2) {
-
- [self.categoryPrice setObject:[NSString stringWithFormat:@"%ld",self.priceType] forKey:@"base_price"];
- [self.categoryPrice setObject:discount forKey:@"price_discount"];
-
- // 将数据返回
- if (self.returnBlock)
- self.returnBlock(self.categoryPrice);
- }
-
- [self.navigationController popViewControllerAnimated:YES];
- // }];
-
- }];
- // }];
- // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Set Price"];
-
-
-
- // NSDictionary *dic = [RANetwork set_npd_shop_givenPrice:@{@"base_price" : [NSString stringWithFormat:@"%ld",self.priceType],@"discount" : discount,@"category_id" : @(self.categoryID)}.mutableCopy];
- //
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- //// [waitalert dismissWithClickedButtonIndex:0 animated:YES];
- //
- // if ([[dic objectForKey:@"result"] integerValue] == 2) {
- //
- // [self.categoryPrice setObject:[NSString stringWithFormat:@"%ld",self.priceType] forKey:@"base_price"];
- // [self.categoryPrice setObject:discount forKey:@"price_discount"];
- //
- // // 将数据返回
- // if (self.returnBlock)
- // self.returnBlock(self.categoryPrice);
- // }
- //
- // [self.navigationController popViewControllerAnimated:YES];
-
- } else {
-
-
- [RAUtils message_box:@"Warning" message:@"Please write a correct value" completion:nil];
-
- }
-
- }
- #pragma mark - text field delegate
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
-
- NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
-
- if (newText.length == 0)
- return YES;
-
- // 正则表达式
- NSString *match = @"-[1-9]{1,1}[0-9]*(\\.[0-9]*)?|-0\\.[0-9]+|[1-9]{1,1}[0-9]*(\\.[0-9]*)?|0\\.[0-9]{0,}|0{1,1}|-{1,1}|-0{1,1}|-0\\.{1,1}";
-
- if ([textField isEqual:self.percentageTextField])
- match = @"[1-9]{1,1}[0-9]*(\\.[0-9]*)?|0(\\.[0-9]*)?";
-
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
-
- return [predicate evaluateWithObject:newText];
- }
- @end
|