PriceSettingViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "iSalesNetwork.h"
  13. #import "Singleton.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 = [Singleton 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 = ![Singleton 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 = [Singleton sharedInstance].deliveryString;
  49. switch (type) {
  50. case 0: {
  51. title = [Singleton 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. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Load Data"];
  84. NSDictionary *dic = [iSalesNetwork get_npd_shop_givenPrice];
  85. [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  86. if ([[dic objectForKey:@"result"] integerValue] == 2) {
  87. CategoryPriceViewController *categoryPriceVC =[ [UIStoryboard storyboardWithName:@"CUL" bundle:nil] instantiateViewControllerWithIdentifier:@"CategoryPriceViewController"];
  88. categoryPriceVC.categoryGivenPrice = dic.mutableCopy;
  89. [self.navigationController pushViewController:categoryPriceVC animated:YES];
  90. } else {
  91. NSString *msg = [dic objectForKey:@"msg"];
  92. if (!msg) {
  93. msg = @"Some Error Occured,Please Try Again";
  94. }
  95. [RAUtils message_alert:msg title:@"Set Store Price" controller:self];
  96. }
  97. }
  98. - (NSString *)checkPriceType:(NSInteger)priceType {
  99. return self.priceType == priceType ? @"1" : @"0";
  100. }
  101. - (NSInteger)checkedCadedate:(NSDictionary *)cadedate {
  102. int priceType = 0;
  103. int count = [[cadedate objectForKey:@"count"] intValue];
  104. for (int i = 0; i < count; i++) {
  105. NSDictionary *dic = [cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  106. int check = [[dic objectForKey:@"check"] intValue];
  107. if (check) {
  108. priceType = [[dic objectForKey:@"value_code"] integerValue];
  109. break;
  110. }
  111. }
  112. return priceType;
  113. }
  114. - (IBAction)priceTypeButtonClick:(UIButton *)sender {
  115. EnumSelectViewController* enumvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"];
  116. enumvc.max_select = 1;
  117. // enumvc.cadedate = @{
  118. // @"count" : @"3",
  119. // @"val_0" : @{@"value" : [Singleton sharedInstance].deliveryString,@"value_code" : @"0",@"check" : [self checkPriceType:0]},
  120. // @"val_1" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]},
  121. // @"val_2" : @{@"value" : given_price,@"value_code" : @"2",@"check" : [self checkPriceType:2]}
  122. // }.mutableCopy;
  123. enumvc.cadedate = @{
  124. @"count" : @"2",
  125. @"val_0" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]},
  126. @"val_1" : @{@"value" : given_price,@"value_code" : @"2",@"check" : [self checkPriceType:2]}
  127. }.mutableCopy;
  128. enumvc.title = @"";
  129. enumvc.single_select =true;
  130. __weak typeof(self) weakSelf = self;
  131. enumvc.returnValue = ^(NSMutableDictionary* value){
  132. if (weakSelf) {
  133. __strong typeof(weakSelf) strongSelf = weakSelf;
  134. [strongSelf changePriceType:[strongSelf checkedCadedate:value]];
  135. }
  136. };
  137. [self.navigationController pushViewController:enumvc animated:true];
  138. }
  139. - (IBAction)saveButtonClick:(UIButton *)sender {
  140. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Set PriceType"];
  141. NSDictionary *dic = [iSalesNetwork set_npd_shop_price_type:self.priceType];
  142. [waitalert dismissWithClickedButtonIndex:0 animated:YES];
  143. // 成功
  144. if ([[dic objectForKey:@"result"] integerValue] == 2) {
  145. [Singleton sharedInstance].npd_shop_price_type = self.priceType;
  146. // 刷新 Cart、Order List
  147. [[NSNotificationCenter defaultCenter] postNotificationName:Change_Price_Type_Notification object:nil];
  148. } else { // 失败
  149. DebugLog(@"set price type failure");
  150. }
  151. [self dismissViewControllerAnimated:YES completion:nil];
  152. }
  153. @end