PriceSettingViewController.m 6.8 KB

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