PriceSettingViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // PriceSetting.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 2016/10/11.
  6. // Copyright © 2016年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "PriceSettingViewController.h"
  9. #import "const.h"
  10. #import "EnumSelectViewController.h"
  11. #import "CategoryPriceViewController.h"
  12. #import "RANetwork.h"
  13. #import "RASingleton.h"
  14. #import <objc/runtime.h>
  15. #import "MainViewController.h"
  16. #import "NotificationNameCenter.h"
  17. @interface PriceSettingViewController ()
  18. @property (strong, nonatomic) IBOutlet UIButton *priceTypeButton;
  19. @property (strong, nonatomic) IBOutlet UIButton *showButton;
  20. @property (strong, nonatomic) IBOutlet UIButton *setPriceButton;
  21. @end
  22. @implementation PriceSettingViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. self.priceType = [RASingleton sharedInstance].npd_shop_price_type;
  27. [self configAppearance];
  28. }
  29. - (void)didReceiveMemoryWarning {
  30. [super didReceiveMemoryWarning];
  31. // Dispose of any resources that can be recreated.
  32. }
  33. #pragma mark - Customer Inint
  34. - (void)configAppearance {
  35. self.setPriceButton.hidden = ![RASingleton sharedInstance].permissions_price_setting;
  36. UIApplication * app = [UIApplication sharedApplication];
  37. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  38. self.showPrice = !appDelegate.price_hidden;
  39. [self changePriceType:self.priceType];
  40. self.showButton.selected = !self.showPrice;
  41. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  42. style:UIBarButtonItemStylePlain
  43. target:self
  44. action:@selector(onCloseClick:)];
  45. self.navigationItem.rightBarButtonItem = closeButton;
  46. }
  47. - (void)changePriceType:(NSInteger)type {
  48. NSString *title = [RASingleton sharedInstance].deliveryString;
  49. switch (type) {
  50. case 0: {
  51. title = [RASingleton sharedInstance].deliveryString;
  52. }
  53. break;
  54. case 1: {
  55. title = flat_price;
  56. }
  57. break;
  58. case 2: {
  59. title = given_price;
  60. }
  61. break;
  62. default:
  63. break;
  64. }
  65. self.priceType = type;
  66. [self.priceTypeButton setTitle:title forState:UIControlStateNormal];
  67. }
  68. #pragma mark - action
  69. - (void)onCloseClick:(id)sender {
  70. [self dismissViewControllerAnimated:YES completion:^{
  71. }];
  72. }
  73. - (IBAction)showOrHidePriceButtonClick:(UIButton *)sender {
  74. sender.selected = !sender.selected;
  75. self.showPrice = !sender.selected;
  76. UIApplication * app = [UIApplication sharedApplication];
  77. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  78. [appDelegate set_priceHidden:sender.selected];
  79. // objc_msgSend(appDelegate.main_vc,@selector(initMenuItems));// initMenuItems 不是公开的方法
  80. [(MainViewController *)appDelegate.main_vc initMenuItems];
  81. }
  82. - (IBAction)calculatePriceButtonClick:(UIButton *)sender {
  83. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Load Data" completion:^{
  84. PopWaitAlert* pop = [RAUtils waiting_pop:@"Load Data" completion:nil];
  85. [RANetwork request_npd_shop_givenprice:^(NSMutableDictionary *result) {
  86. NSDictionary *dic = result;
  87. // [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  88. // [waitalert dismissViewControllerAnimated:YES completion:^{
  89. [pop hide];
  90. if ([[dic objectForKey:@"result"] integerValue] == 2) {
  91. CategoryPriceViewController *categoryPriceVC =[ [UIStoryboard storyboardWithName:@"CUL" bundle:nil] instantiateViewControllerWithIdentifier:@"CategoryPriceViewController"];
  92. categoryPriceVC.categoryGivenPrice = dic.mutableCopy;
  93. [self.navigationController pushViewController:categoryPriceVC animated:YES];
  94. } else {
  95. NSString *msg = [dic objectForKey:@"msg"];
  96. if (!msg) {
  97. msg = @"Some Error Occured,Please Try Again";
  98. }
  99. [RAUtils message_box:@"Set Store Price" message:msg completion:nil];
  100. };
  101. // }];
  102. }];
  103. // }];
  104. }
  105. - (NSString *)checkPriceType:(NSInteger)priceType {
  106. return self.priceType == priceType ? @"1" : @"0";
  107. }
  108. - (NSInteger)checkedCadedate:(NSDictionary *)cadedate {
  109. int priceType = 0;
  110. int count = [[cadedate objectForKey:@"count"] intValue];
  111. for (int i = 0; i < count; i++) {
  112. NSDictionary *dic = [cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  113. int check = [[dic objectForKey:@"check"] intValue];
  114. if (check) {
  115. priceType = [[dic objectForKey:@"value_code"] intValue];
  116. break;
  117. }
  118. }
  119. return priceType;
  120. }
  121. - (IBAction)priceTypeButtonClick:(UIButton *)sender {
  122. EnumSelectViewController* enumvc =[[UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"];
  123. enumvc.max_select = 1;
  124. // enumvc.cadedate = @{
  125. // @"count" : @"3",
  126. // @"val_0" : @{@"value" : [Singleton sharedInstance].deliveryString,@"value_code" : @"0",@"check" : [self checkPriceType:0]},
  127. // @"val_1" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]},
  128. // @"val_2" : @{@"value" : given_price,@"value_code" : @"2",@"check" : [self checkPriceType:2]}
  129. // }.mutableCopy;
  130. enumvc.cadedate = @{
  131. @"count" : @"2",
  132. @"val_0" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]},
  133. @"val_1" : @{@"value" : given_price,@"value_code" : @"2",@"check" : [self checkPriceType:2]}
  134. }.mutableCopy;
  135. enumvc.title = @"";
  136. enumvc.single_select =true;
  137. __weak typeof(self) weakSelf = self;
  138. enumvc.returnValue = ^(NSMutableDictionary* value){
  139. if (weakSelf) {
  140. __strong typeof(weakSelf) strongSelf = weakSelf;
  141. [strongSelf changePriceType:[strongSelf checkedCadedate:value]];
  142. }
  143. };
  144. [self.navigationController pushViewController:enumvc animated:true];
  145. }
  146. - (IBAction)saveButtonClick:(UIButton *)sender {
  147. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Set PriceType" completion:^{
  148. PopWaitAlert* pop = [RAUtils waiting_pop:@"Set PriceType" completion:nil];
  149. [RANetwork request_update_npd_shop_price_type:self.priceType completionHandler:^(NSMutableDictionary *result) {
  150. NSDictionary *dic=result;
  151. // [waitalert dismissViewControllerAnimated:YES completion:^{
  152. [pop hide];
  153. // 成功
  154. if ([[dic objectForKey:@"result"] integerValue] == 2) {
  155. [RASingleton sharedInstance].npd_shop_price_type = self.priceType;
  156. #ifdef RA_NOTIFICATION
  157. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  158. #else
  159. // 刷新 Cart、Order List
  160. [[NSNotificationCenter defaultCenter] postNotificationName:Change_Price_Type_Notification object:nil];
  161. #endif
  162. } else { // 失败
  163. DebugLog(@"set price type failure");
  164. }
  165. [self dismissViewControllerAnimated:YES completion:nil];;
  166. // }];
  167. }];
  168. // }];
  169. }
  170. @end