RAOrderEditorViewController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // RAOrderEditorViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 2017/11/7.
  6. // Copyright © 2017年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAOrderEditorViewController.h"
  9. #import "AppDelegate.h"
  10. #import "MainViewController.h"
  11. #define CLOSE_ACTION 567
  12. @interface RAOrderEditorViewController ()
  13. @property (nonatomic,assign) BOOL isResume;///<恢复
  14. @property (nonatomic,assign) NSInteger userType;///<恢复使用
  15. @property (nonatomic,assign) NSInteger pageIndex;///<当前页码
  16. /**
  17. * 保存不同页码上行参数,最新的在最后。
  18. * 点击下一页时将当前页面上行参数写入数组尾
  19. * 退回到前一页后删除最后一个元素
  20. */
  21. @property (nonatomic,strong) NSMutableArray *pageUpParams;
  22. @property (nonatomic,strong) NSMutableArray *pageContentDownloadArray;///<保存不同页码content_data_download
  23. @property (nonatomic,strong) NSMutableArray *pageContentControlArray;///<保存不同页码content_data_control
  24. @property (nonatomic,strong) NSMutableArray *pageChangeDataArray;///<保存不同页码change_data
  25. @property (strong, nonatomic) IBOutlet UIToolbar *pageControlToolBar;
  26. @property (strong, nonatomic) IBOutlet UIBarButtonItem *frontBarItem;
  27. @property (strong, nonatomic) IBOutlet UIBarButtonItem *nextBarItem;
  28. @end
  29. @implementation RAOrderEditorViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. self.editable = YES;
  34. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  35. style:UIBarButtonItemStylePlain
  36. target:self
  37. action:@selector(onCloseClick:)];
  38. self.navigationItem.leftBarButtonItem = closeButton;
  39. [self setupPageControlBar];
  40. }
  41. - (void)didReceiveMemoryWarning {
  42. [super didReceiveMemoryWarning];
  43. // Dispose of any resources that can be recreated.
  44. }
  45. #pragma mark - Lazy Load
  46. - (NSMutableArray *)pageUpParams {
  47. if (!_pageUpParams) {
  48. _pageUpParams = [NSMutableArray array];
  49. }
  50. return _pageUpParams;
  51. }
  52. - (NSMutableArray *)pageContentDownloadArray {
  53. if (!_pageContentDownloadArray) {
  54. _pageContentDownloadArray = [NSMutableArray array];
  55. }
  56. return _pageContentDownloadArray;
  57. }
  58. - (NSMutableArray *)pageContentControlArray {
  59. if (!_pageContentControlArray) {
  60. _pageContentControlArray = [NSMutableArray array];
  61. }
  62. return _pageContentControlArray;
  63. }
  64. - (NSMutableArray *)pageChangeDataArray {
  65. if (!_pageChangeDataArray) {
  66. _pageChangeDataArray = [NSMutableArray array];
  67. }
  68. return _pageChangeDataArray;
  69. }
  70. #pragma mark - Private
  71. - (void)onCloseClick:(UIButton *)sender {
  72. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  73. if (self.resumeOrder) {
  74. [appDelegate.main_vc.navigationController popViewControllerAnimated:YES];
  75. return;
  76. }
  77. UIAlertView * alert = nil;
  78. if(appDelegate.order_status==0)
  79. {
  80. alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Choose your action", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Switch to cart", nil),@"Switch to order history",NSLocalizedString(@"Close and cancel order", nil), nil];
  81. }
  82. else
  83. {
  84. alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Choose your action", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Switch to cart", nil),@"Switch to order history",NSLocalizedString(@"Close and release order", nil), nil];
  85. }
  86. alert.tag = CLOSE_ACTION;
  87. [alert show];
  88. }
  89. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  90. if(alertView.tag == CLOSE_ACTION)
  91. {
  92. if(buttonIndex==1)
  93. {
  94. [self.navigationController popViewControllerAnimated:true];
  95. }
  96. if(buttonIndex==2)
  97. {
  98. [self.navigationController popViewControllerAnimated:false];
  99. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  100. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  101. [((MainViewController*)appDelegate.main_vc) switchToOrder];
  102. }
  103. else if(buttonIndex==3)
  104. {
  105. // close order;
  106. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  107. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Release Order"];
  108. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  109. NSDictionary* order_json = [RANetwork release_Order:appDelegate.order_code withScreen:nil];
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  112. if([[order_json valueForKey:@"result"] intValue]==2)
  113. {
  114. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  115. [appDelegate closeOrder];
  116. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  117. [((MainViewController*)appDelegate.main_vc) switchToHome];
  118. if([appDelegate.mode isEqualToString:@"TM"]&& appDelegate.submit_order_logout)
  119. {
  120. [((MainViewController*)appDelegate.main_vc) Loginout:false];
  121. }
  122. [self prepareReturn:nil];
  123. [self.navigationController popViewControllerAnimated:true];
  124. }
  125. else
  126. {
  127. [RAUtils message_alert:[order_json valueForKey:@"err_msg"] title:@"Open Order"controller:self] ;
  128. }
  129. });
  130. });
  131. }
  132. }
  133. }
  134. - (void)setupPageControlBar {
  135. int totalPage = 1;
  136. // 1. 控制ToolBar Item显隐
  137. NSMutableArray *items = [self.pageControlToolBar.items mutableCopy];
  138. if (self.pageIndex == 0) {
  139. // 首页不需要前一页
  140. if ([items containsObject:self.frontBarItem]) {
  141. [items removeObject:self.frontBarItem];
  142. }
  143. } else if (self.pageIndex == totalPage - 1) {
  144. // 尾页不需要后一页
  145. if ([items containsObject:self.nextBarItem]) {
  146. [items removeObject:self.nextBarItem];
  147. }
  148. } else {
  149. // 中间显示前一页、后一页按钮
  150. if (![items containsObject:self.frontBarItem]) {
  151. [items insertObject:self.frontBarItem atIndex:0];
  152. }
  153. if (![items containsObject:self.nextBarItem]) {
  154. [items addObject:self.nextBarItem];
  155. }
  156. }
  157. self.pageControlToolBar.items = items;
  158. }
  159. #pragma mark - BarItem Action
  160. - (IBAction)frontBarItemClick:(UIBarButtonItem *)sender {
  161. self.pageIndex--;
  162. // 1.恢复当前页面数据
  163. NSMutableDictionary* upparams = [self.pageUpParams lastObject]; // 当前页上行参数
  164. __weak typeof(self) weakSelf = self;
  165. [upparams enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  166. [weakSelf.params removeObjectForKey:key];
  167. }];
  168. [self.pageUpParams removeLastObject];
  169. self.content_data_control = [self.pageContentControlArray lastObject];
  170. [self.pageContentControlArray removeLastObject];
  171. self.content_data_download = [self.pageContentDownloadArray lastObject];
  172. [self.pageContentDownloadArray removeLastObject];
  173. self.changed_data = [self.pageChangeDataArray lastObject];
  174. [self.pageChangeDataArray removeLastObject];
  175. // 2.刷新页面
  176. [self.editorTable reloadData];
  177. self.navigationItem.title = [self.content_data_download valueForKey:@"title"] ;
  178. [self setupPageControlBar];
  179. }
  180. - (IBAction)nextBarItemClick:(UIBarButtonItem *)sender {
  181. self.pageIndex++;
  182. NSMutableDictionary* upparams = [self check_cancommit:false]; // 当前页上行参数
  183. // 1.保存当前页面数据
  184. if (upparams == nil) {
  185. upparams = [NSMutableDictionary dictionary];
  186. }
  187. [self.pageUpParams addObject:upparams];
  188. if (self.content_data_control == nil) {
  189. self.content_data_control = [NSMutableArray array];
  190. }
  191. [self.pageContentControlArray addObject:self.content_data_control];
  192. if (self.content_data_download == nil) {
  193. self.content_data_download = [NSMutableDictionary dictionary];
  194. }
  195. [self.pageContentDownloadArray addObject:self.content_data_download];
  196. if (self.changed_data == nil) {
  197. self.changed_data = [NSMutableDictionary dictionary];
  198. }
  199. [self.pageChangeDataArray addObject:self.changed_data];
  200. // 2.请求下一页数据
  201. __weak typeof(self) weakSelf = self;
  202. [upparams enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  203. [weakSelf.params setObject:obj forKey:key];
  204. }];
  205. [self setupPageControlBar];
  206. [self refresh:nil];
  207. }
  208. #pragma mark - Override
  209. - (void)download_success {
  210. [self setupPageControlBar];
  211. }
  212. @end