WatchListViewController.m 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. //
  2. // WatchListViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-8-1.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "WatchListViewController.h"
  9. #import "RANetwork.h"
  10. #import "ModelItemCell.h"
  11. #import "DetailViewController.h"
  12. #import "LoginViewController.h"
  13. #import "MainViewController.h"
  14. #import "TearSheetParamViewController.h"
  15. #import "EditModelPriceViewController.h"
  16. #import "ContactListViewController.h"
  17. #import "DefaultTableHeaderView.h"
  18. #import "DefaultAppearance.h"
  19. #import "SortItemViewController.h"
  20. #import "SortButton.h"
  21. #import "UIColor+JK_HEX.h"
  22. #import "CartUtils.h"
  23. #import "RAPDFViewController.h"
  24. #import "JKMessageBoxController.h"
  25. #import "FileCache.h"
  26. //#define ALERT_DEL 1025
  27. //#import "PDFViewController.h"
  28. @interface WatchListViewController ()
  29. @property (strong, nonatomic) IBOutlet UIBarButtonItem *emailBtn;
  30. @property (strong, nonatomic) IBOutlet UIBarButtonItem *printBtn;
  31. @property (nonatomic,strong) SortItemViewController *sortItemController;
  32. @property (nonatomic,assign) int sortIndex;
  33. @property (nonatomic,strong) SortButton *sortButton;
  34. @property (nonatomic,strong) UILabel *qtyLabel;
  35. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  36. @property (nonatomic,copy) NSString *print_url;
  37. //仅用于加购物车的临时变量,避免wishlist刷新后check 丢失。
  38. @property (nonatomic,strong) NSMutableArray* checked_cart;
  39. @property (nonatomic,strong) NSMutableArray* checked_iid_cart;
  40. @end
  41. @implementation WatchListViewController
  42. - (NSOperationQueue *)dataOperationQueue {
  43. if (!_dataOperationQueue) {
  44. _dataOperationQueue = [[NSOperationQueue alloc] init];
  45. _dataOperationQueue.maxConcurrentOperationCount = 1;
  46. }
  47. return _dataOperationQueue;
  48. }
  49. -(void) logout
  50. {
  51. self.content_data = nil;
  52. [self refresh_ui];
  53. }
  54. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  55. {
  56. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  57. if (self) {
  58. // Custom initialization
  59. }
  60. return self;
  61. }
  62. #ifndef RA_NOTIFICATION
  63. -(void) reload_container_getdata:(bool) update_data
  64. {
  65. [super reload_container_getdata:update_data];
  66. if(update_data)
  67. [self reload_data];
  68. else
  69. [self.itemListTable reloadData];
  70. }
  71. #endif
  72. - (IBAction)onAddtoCart:(id)sender {
  73. self.checked_cart = [[NSMutableArray alloc] init];
  74. self.checked_iid_cart= [[NSMutableArray alloc] init];
  75. int count = [[self.content_data valueForKey:@"count"] intValue];
  76. for(int i=0;i<count;i++)
  77. {
  78. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  79. bool check = [[item_json valueForKey:@"check"] boolValue];
  80. if(check)
  81. {
  82. [self.checked_cart addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  83. [self.checked_iid_cart addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  84. }
  85. }
  86. [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  87. return;
  88. //
  89. // UIApplication * app = [UIApplication sharedApplication];
  90. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  91. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  92. // if(appDelegate.bLogin==false)
  93. // {
  94. //
  95. // LoginViewController * loginvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  96. // // loginvc.delegate = self;
  97. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  98. //
  99. // loginvc.returnValue = ^(bool blogin){
  100. //
  101. //
  102. // // UIApplication * app = [UIApplication sharedApplication];
  103. // // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  104. //
  105. //
  106. //
  107. //
  108. // if(blogin)
  109. // {
  110. // if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  111. // {
  112. //
  113. // [main_vc checklogin:false];
  114. // if(appDelegate.can_create_order)
  115. // { NSString* msg =@"";
  116. // if(appDelegate.contact_id.length>0)
  117. // {
  118. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  119. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  120. //
  121. // }
  122. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  123. //
  124. //
  125. //
  126. // // alert.
  127. // [alert show];
  128. // }
  129. // else
  130. // {
  131. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  132. //
  133. // // alert.
  134. // [alert show];
  135. // }
  136. // }
  137. //// else
  138. //// {
  139. //// [main_vc checklogin:true];
  140. //// }
  141. // }
  142. //
  143. // // self.offset = 0;
  144. // // [self.content_data removeAllObjects];
  145. // // [self loadpage];
  146. // //
  147. // // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  148. //
  149. // };
  150. //
  151. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  152. //
  153. //
  154. //
  155. //
  156. //
  157. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  158. //
  159. // navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  160. // [self presentViewController:navi animated:YES completion:^{
  161. //
  162. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  163. //
  164. // DebugLog(@"LoginViewController present.........");
  165. //
  166. // // self.btop = false;
  167. // // <#code#>
  168. // }];
  169. // }
  170. // else
  171. // {
  172. // //if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  173. // if(appDelegate.order_code==nil)
  174. // {
  175. // [main_vc checklogin:false];
  176. // if(appDelegate.can_create_order)
  177. // {
  178. // NSString* msg =@"";
  179. // if(appDelegate.contact_id.length>0)
  180. // {
  181. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  182. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  183. //
  184. // }
  185. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  186. //
  187. // // alert.
  188. // [alert show];
  189. // }
  190. // else
  191. // {
  192. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  193. //
  194. // // alert.
  195. // [alert show];
  196. // }
  197. // }
  198. // else
  199. // {
  200. //
  201. // if(appDelegate.order_code==nil)
  202. // [ self neworder];
  203. // else
  204. // [self addtocart];
  205. //
  206. //
  207. //
  208. // }
  209. // }
  210. //
  211. }
  212. -(void) addtocart
  213. {
  214. [self addtocart:self.checked_cart checked_iid:self.checked_iid_cart];
  215. self.checked_cart=nil;
  216. self.checked_iid_cart=nil;
  217. }
  218. -(void) addtocart:(NSMutableArray*) checked checked_iid:(NSMutableArray*)checked_iid
  219. {
  220. if(checked.count==0)
  221. {
  222. [RAUtils message_box:@"Cannot Add to cart." message:@"You must select at least one model." completion:nil];
  223. return;
  224. }
  225. NSString * ids = [checked_iid componentsJoinedByString:@","];
  226. self.btnAddtoCart.enabled = false;
  227. [RANetwork request_move_wish2cart:ids completionHandler:^(NSMutableDictionary *result) {
  228. NSDictionary* return_json = result;
  229. self.btnAddtoCart.enabled = true;
  230. if([[return_json valueForKey:@"result"] intValue]==2)
  231. {
  232. #ifdef RA_NOTIFICATION
  233. [ActiveViewController Notify:@"WatchListViewController,CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  234. #else
  235. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  236. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  237. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  238. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  239. #endif
  240. [RAUtils message_box:@" Multiple items moved to Cart" message:@"" completion:nil];
  241. }
  242. else
  243. {
  244. [self reload_data];
  245. [RAUtils message_box:@"Move To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  246. }
  247. }];
  248. }
  249. - (void)viewDidLoad
  250. {
  251. [super viewDidLoad];
  252. DebugLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  253. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  254. ref.tag = 200 ;
  255. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  256. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  257. // ref.hidden = true;
  258. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  259. [self.itemListTable addSubview:ref];
  260. DebugLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  261. // [[UIToolbar appearance] setBackgroundColor: [UIColor whiteColor]];
  262. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  263. self.label_net_err.layer.borderWidth = 2.0;
  264. self.label_net_err.layer.cornerRadius=15;
  265. self.label_net_err.layer.masksToBounds=true;
  266. CGPoint center = self.mum.center;
  267. self.mum.frame = CGRectMake(0, 0, 100, 100);
  268. self.mum.center = center;
  269. // self.mum.backgroundColor = [UIColor colorWithHEX:0xA5A5A5 alpha:0.5];
  270. self.mum.layer.cornerRadius = 5.0f;
  271. self.mum.layer.masksToBounds = YES;
  272. self.sortIndex = 0;
  273. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  274. // tap.minimumPressDuration = 0.8; //定义按的时间
  275. [self.label_net_err addGestureRecognizer:tap];
  276. // [self reload_data];
  277. }
  278. -(void)manually_refresh
  279. {
  280. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  281. if(self.isrefreshing)
  282. {
  283. [reF endRefreshing];
  284. return;
  285. }
  286. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  287. [self performSelector:@selector(reload_data) withObject:nil afterDelay:1];
  288. // DebugLog(@"refresh!!!!!!!!");
  289. }
  290. - (IBAction)onSelectClick:(id)sender {
  291. // self.total=0;
  292. if([self.btnselect.title isEqualToString:@"Select all"])
  293. {
  294. int count =[[self.content_data valueForKey:@"count"] intValue];
  295. for(int i=0;i<count;i++)
  296. {
  297. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  298. [item_json setValue:@"1" forKey:@"check"];
  299. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  300. cell.img_checkmark.hidden=false;
  301. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  302. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  303. }
  304. [self.btnselect setTitle:@"Deselect all"];
  305. }
  306. else
  307. {
  308. int count =[[self.content_data valueForKey:@"count"] intValue];
  309. for(int i=0;i<count;i++)
  310. {
  311. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)i]] mutableCopy];
  312. [item_json setValue:@"0" forKey:@"check"];
  313. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  314. cell.img_checkmark.hidden=true;
  315. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  316. }
  317. [self.btnselect setTitle:@"Select all"];
  318. // self.btnselect.titleLabel.text=@"Select all";
  319. }
  320. }
  321. - (IBAction)onDeleteClick:(id)sender {
  322. NSMutableArray* checked = [[NSMutableArray alloc] init];
  323. int count = [[self.content_data valueForKey:@"count"] intValue];
  324. for(int i=0;i<count;i++)
  325. {
  326. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  327. int check = [[item_json valueForKey:@"check"] intValue];
  328. if(check==1)
  329. {
  330. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  331. }
  332. }
  333. // disable for debug
  334. if(checked.count==0)
  335. return;
  336. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  337. NSString * string = [checked componentsJoinedByString:@","];
  338. // __block UIAlertController* wait_alert = [RAUtils waiting_alert:self title:@"Remove Models From Wishlist" completion:^{
  339. PopWaitAlert* pop=[RAUtils waiting_pop:@"Remove Models From Wishlist" completion:nil];
  340. [RANetwork request_remove_wishlist:string completionHandler:^(NSMutableDictionary *result) {
  341. NSMutableDictionary* cart_json = result;
  342. // [wait_alert dismissViewControllerAnimated:YES completion:^{
  343. [pop hide];
  344. if([[cart_json valueForKey:@"result"] intValue]==2)
  345. {
  346. [self reload_data];
  347. [self.btnselect setTitle:@"Select all"];
  348. }
  349. else
  350. {
  351. [RAUtils message_box:@"Remove Model from Withlist" message:cart_json[@"err_msg"] completion:nil];
  352. }
  353. // }];
  354. }];
  355. // }];
  356. }
  357. -(void) viewWillAppear:(BOOL)animated
  358. {
  359. [super viewWillAppear:animated];
  360. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  361. #ifdef OFFLINE_MODE
  362. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  363. // 离线隐藏Email
  364. self.emailBtn.enabled = !appDelegate.offline_mode;
  365. // 离线隐藏Print
  366. self.printBtn.enabled = !appDelegate.offline_mode;
  367. #endif
  368. }
  369. -(void) operation_reload_data
  370. {
  371. if (self.dataOperationQueue.operationCount > 1) {
  372. return;
  373. }
  374. dispatch_async(dispatch_get_main_queue(), ^{
  375. if(self.isrefreshing)
  376. return;
  377. // [self.content_data removeAllObjects];
  378. // [self.itemListTable reloadData];
  379. self.isrefreshing=true;
  380. self.label_net_err.hidden=true;
  381. self.itemListTable.hidden=true;
  382. self.mum.center = self.view.center;
  383. self.mum.hidden = false;
  384. [self.mum startAnimating];
  385. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  386. [reF endRefreshing];
  387. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  388. [RANetwork request_wishlist:self.sortIndex completionHandler:^(NSMutableDictionary *result) {
  389. NSMutableDictionary* cart_json =result;
  390. [self.mum stopAnimating];
  391. self.isrefreshing=false;
  392. if (self.dataOperationQueue.operationCount > 1) {
  393. return ;
  394. }
  395. int result_code=[[cart_json valueForKey:@"result"] intValue];
  396. self.content_data = [cart_json mutableCopy];
  397. [self.itemListTable reloadData];
  398. self.print_url = [cart_json objectForKey:@"wishlist_print_url"];
  399. if(result_code==2||result_code==1||result_code==0)
  400. {
  401. self.itemListTable.hidden=false;
  402. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  403. appDelegate.wish_count =[[self.content_data valueForKey:@"count"] intValue];
  404. }
  405. else
  406. if(result_code==RESULT_NET_ERROR)
  407. {
  408. self.label_net_err.hidden=false;
  409. self.itemListTable.hidden=true;
  410. }
  411. else
  412. {
  413. [RAUtils message_box:@"Loading Wish list" message:[cart_json valueForKey:@"err_msg"] completion:nil];
  414. }
  415. self.isrefreshing=false;
  416. }];
  417. return;
  418. });
  419. }
  420. - (void)didReceiveMemoryWarning
  421. {
  422. [super didReceiveMemoryWarning];
  423. // Dispose of any resources that can be recreated.
  424. }
  425. #pragma mark - Table view data source
  426. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  427. {
  428. return 33;
  429. }
  430. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  431. {
  432. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  433. if(value==nil)
  434. value=@"";
  435. unsigned long color = strtoul([value UTF8String],0,16);
  436. NSString *sectionTitle = @"Wish List";
  437. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 33)];
  438. // UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
  439. // sortButton.frame = CGRectMake(20, 5.5, (33 - 5.5 * 2), (33 - 5.5 * 2));
  440. // [sortButton setImage:[UIImage imageNamed:@"check_1_24"] forState:UIControlStateNormal];
  441. // [sortButton addTarget:self action:@selector(wishlistSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  442. // [myView addSubview:sortButton];
  443. [myView addSubview:self.sortButton];
  444. UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5.5, tableView.bounds.size.width, 22)];
  445. titlelabel.textColor=UIColorFromRGB(color);
  446. titlelabel.textAlignment=NSTextAlignmentCenter;
  447. titlelabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
  448. titlelabel.text=sectionTitle;
  449. titlelabel.center = myView.center;
  450. myView.autoresizesSubviews=true;
  451. [myView addSubview:titlelabel];
  452. [myView addSubview:self.qtyLabel];
  453. return myView;
  454. }
  455. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  456. {
  457. return 1;
  458. }
  459. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  460. {
  461. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  462. int count =[[self.content_data valueForKey:@"count"] intValue];
  463. return count;
  464. }
  465. - (IBAction)imgbtnClicked:(UIButton *)sender {
  466. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  467. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  468. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  469. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  470. NSString* product_id = [item_json valueForKey:@"product_id"];
  471. NSString *name = [item_json objectForKey:@"description"];
  472. NSRange subRange = [name rangeOfString:@"\n"];
  473. name = [name substringToIndex:subRange.location];
  474. DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  475. // dvc
  476. dvc.product_id=product_id;
  477. dvc.category_id=nil;
  478. dvc.ispush=true;
  479. dvc.model_name = name;
  480. [dvc reload];
  481. [self.navigationController pushViewController:dvc animated:true];
  482. }
  483. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  484. {
  485. // if(tableView==self.itemListTable)
  486. // {
  487. NSString *CellIdentifier = @"WatchListItemCell";
  488. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  489. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  490. // Button 设置了大小约束,Size就不会随内容变化
  491. [cell.btnImage setContentMode:UIViewContentModeScaleAspectFit];
  492. #if __IPHONE_OS_VERSION_MIN_REQUIRED <150000
  493. [cell.btnImage setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  494. #endif
  495. [cell.btnImage setContentHorizontalAlignment:UIControlContentHorizontalAlignmentFill];
  496. [cell.btnImage setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
  497. cell.btnImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
  498. NSString* img_url = [item_json valueForKey:@"img"];
  499. NSString* description = [item_json valueForKey:@"description"];
  500. // NSString* identifier = [item_json valueForKey:@"identifier"];
  501. // NSString* attribute = [item_json valueForKey:@"attribute"];
  502. // NSString* currency = [item_json valueForKey:@"currency"];
  503. // int count =[[item_json valueForKey:@"count"] intValue];
  504. NSString* price =[item_json valueForKey:@"tear_sheet_price"];
  505. // cell.labelAttribute.text = attribute;
  506. // cell.labelCurrency.text = currency;
  507. cell.labelDescription.text = description;
  508. // cell.labelIdentifier.text = identifier;
  509. cell.labelPrice.text = price;
  510. // cell.editCount.text =[NSString stringWithFormat:@"%d",count];
  511. // NSDictionary* val_json =[cadedate_json objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  512. int check = [[item_json valueForKey:@"check"] intValue];
  513. if(check==1)
  514. cell.img_checkmark.hidden=false;
  515. else
  516. cell.img_checkmark.hidden=true;
  517. // UIImageView* iv =cell.btnImage.imageView;
  518. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  519. [cell.btnImage setImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  520. NSString* file_name=[img_url lastPathComponent];
  521. NSData* img_data=[FileCache load_cached_img:file_name loadFrom:img_url];
  522. if(img_data!=nil)
  523. {
  524. UIImage * img =[UIImage imageWithData:img_data];
  525. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  526. [cell.btnImage setImage:img forState:UIControlStateNormal];
  527. }
  528. else
  529. {
  530. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  531. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  532. dispatch_async(dispatch_get_main_queue(), ^{
  533. if(downloadimg_data!=nil)
  534. {
  535. [FileCache cache_img:downloadimg_data filename:file_name saveTo:img_url];
  536. UIImage * img =[UIImage imageWithData:downloadimg_data];
  537. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  538. [cell.btnImage setImage:img forState:UIControlStateNormal];
  539. }
  540. else
  541. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  542. [cell.btnImage setImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  543. });
  544. });
  545. }
  546. cell.backgroundColor = [UIColor whiteColor];// [UIColor whiteColor];;
  547. int qty = [item_json[@"cart_count"] intValue];
  548. cell.qty_Label.text = [NSString stringWithFormat:@"QTY: %d",qty];
  549. return cell;
  550. }
  551. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  552. {
  553. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  554. cell.selected = false;
  555. // self.dirty = true;
  556. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  557. int check = [[item_json valueForKey:@"check"] intValue];
  558. if(check==1)
  559. {
  560. [item_json setValue:@"0" forKey:@"check"];
  561. cell.img_checkmark.hidden=true;
  562. [self.btnselect setTitle: @"Select all"];
  563. }
  564. else
  565. {
  566. [item_json setValue:@"1" forKey:@"check"];
  567. cell.img_checkmark.hidden=false;
  568. }
  569. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  570. }
  571. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  572. return UITableViewCellEditingStyleDelete;
  573. }
  574. //#pragma mark - UIAlertViewDelegate
  575. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  576. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  577. //{
  578. //
  579. //
  580. //
  581. //}
  582. - (BOOL)isPureFloat:(NSString*)string{
  583. NSScanner* scan = [NSScanner scannerWithString:string];
  584. float val;
  585. return[scan scanFloat:&val] && [scan isAtEnd];
  586. }
  587. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  588. //- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  589. - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
  590. {
  591. // 添加一个删除按钮
  592. self.indexPath=indexPath;
  593. // UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  594. UIContextualAction* deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  595. DebugLog(@"delete click");
  596. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete confirm" message:@"Are you sure remove model from wishlist?" preferredStyle:UIAlertControllerStyleAlert];
  597. UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  598. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  599. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  600. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  601. // __block UIAlertController* wait_alert = [RAUtils waiting_alert:self title:@"Remove Model" completion:^{
  602. PopWaitAlert* pop=[RAUtils waiting_pop:@"Remove Model" completion:nil];
  603. [RANetwork request_remove_wishlist:ids completionHandler:^(NSMutableDictionary *result) {
  604. NSMutableDictionary* cart_json = result;
  605. // [wait_alert dismissViewControllerAnimated:YES completion:^{
  606. [pop hide];
  607. if([[cart_json valueForKey:@"result"] intValue]==2)
  608. {
  609. [self reload_data];
  610. }
  611. else
  612. {
  613. [RAUtils message_box:@"Delete Model" message:cart_json[@"err_msg"] completion:nil];
  614. }
  615. // }];
  616. }];
  617. // }];
  618. }];
  619. UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  620. }];
  621. [alertController addAction:action_2];
  622. [alertController addAction:action_3];
  623. [self presentViewController:alertController animated:YES completion:nil];
  624. }];
  625. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  626. return [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
  627. //
  628. //
  629. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  630. // if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  631. //
  632. // return @[deleteRowAction];
  633. //
  634. // else
  635. // return @[deleteRowAction];
  636. //
  637. }
  638. - (void)tableView:(UITableView *)tableView
  639. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  640. }
  641. #pragma mark - sort button
  642. - (void)wishlistSortButtonClicked:(UIButton *)sender {
  643. DebugLog(@"wish list sort button clicked");
  644. [self.view addSubview:self.sortItemController.view];
  645. }
  646. - (SortItemViewController *)sortItemController {
  647. if (!_sortItemController) {
  648. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(40, 40)];
  649. _sortItemController.sortIndex = self.sortIndex;
  650. _sortItemController.sortData = @[
  651. @{@"title":@"Last",@"icon":@"TX_18"},
  652. @{@"title":@"First",@"icon":@"TS_18"},
  653. @{@"title":@"Item number a-z",@"icon":@"IX_18"},
  654. @{@"title":@"Item number z-a",@"icon":@"IS_18"},
  655. @{@"title":@"Description",@"icon":@"DX_18"},
  656. ];
  657. __weak typeof(self) weakSelf = self;
  658. _sortItemController.sortBlock = ^(int sort){
  659. weakSelf.sortIndex = sort;
  660. [weakSelf reload_data];
  661. };
  662. }
  663. // _sortItemController.sortIndex = self.sortIndex;
  664. return _sortItemController;
  665. }
  666. - (SortButton *)sortButton {
  667. if (!_sortButton) {
  668. _sortButton = [SortButton sortButtonWithHeight:33];
  669. [_sortButton addTarget:self action:@selector(wishlistSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  670. }
  671. return _sortButton;
  672. }
  673. - (void)setSortIndex:(int)sortIndex {
  674. _sortIndex = sortIndex;
  675. NSString *selectedImageName = @"";
  676. switch (sortIndex) {
  677. case 0:{
  678. selectedImageName = @"TX_22";
  679. }
  680. break;
  681. case 1:{
  682. selectedImageName = @"TS_22";
  683. }
  684. break;
  685. case 2:{
  686. selectedImageName = @"IX_22";
  687. }
  688. break;
  689. case 3:{
  690. selectedImageName = @"IS_22";
  691. }
  692. break;
  693. case 4:{
  694. selectedImageName = @"DX_22";
  695. }
  696. break;
  697. default:
  698. break;
  699. }
  700. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  701. }
  702. - (UILabel *)qtyLabel {
  703. if (!_qtyLabel) {
  704. _qtyLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22)];
  705. _qtyLabel.text = @"QTY";
  706. _qtyLabel.textAlignment = NSTextAlignmentCenter;
  707. _qtyLabel.autoresizingMask= UIViewAutoresizingFlexibleLeftMargin;
  708. }
  709. return _qtyLabel;
  710. }
  711. #pragma mark - rotation
  712. //- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  713. //
  714. // self.sortItemController.view.frame = self.view.bounds;
  715. //}
  716. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  717. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  718. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  719. // what ever you want to prepare
  720. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  721. self.sortItemController.view.frame = self.view.bounds;
  722. // self.sortItemController.view.frame = self.view.bounds;
  723. self.qtyLabel.frame = CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22);
  724. }];
  725. }
  726. //- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  727. // return YES;
  728. //}
  729. - (BOOL)shouldAutorotate
  730. {
  731. return YES;
  732. }
  733. //- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  734. //
  735. // self.sortItemController.view.frame = self.view.bounds;
  736. // self.qtyLabel.frame = CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22);
  737. //
  738. //}
  739. #pragma mark - Action
  740. - (IBAction)printWishlistClick:(UIBarButtonItem *)sender {
  741. if ([[self.content_data valueForKey:@"count"] intValue] < 1) {
  742. [RAUtils message_box:@"Warning" message:@"Please add wishlist first" completion:nil];
  743. return;
  744. }
  745. if (self.print_url.length == 0) {
  746. [RAUtils message_box:@"Warning" message:@"There is no resource to print" completion:nil];
  747. }
  748. RAPDFViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"RAPDFViewController"];
  749. ViewController.url = self.print_url;
  750. ViewController.canSave = false;
  751. ViewController.isLocalfile=NO;
  752. NSString* subject;
  753. subject =@"Wishlist Print";
  754. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  755. NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  756. NSMutableArray* send_to = [[NSMutableArray alloc]init];
  757. if(customer_email.length>0)
  758. {
  759. send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy];
  760. }
  761. ViewController.mail_to = send_to;
  762. // ViewController.attachment_name = [NSString stringWithFormat:@"NPD_Wish_List %@.pdf",cur_time];
  763. /*
  764. #ifdef BUILD_HOMER
  765. ViewController.filename = [NSString stringWithFormat:@"HOMER_Wish_List.pdf"];
  766. #endif
  767. #if defined(BUILD_NPD) || defined(BUILD_USAI)
  768. ViewController.filename = [NSString stringWithFormat:@"NPD_Wish_List.pdf"];
  769. #endif
  770. */
  771. ViewController.filename = [NSString stringWithFormat:@"%@_Wish_List.pdf",COMPANY_SHORT_NAME];
  772. ViewController.mail_subject = subject;
  773. ViewController.hidenavi = false;
  774. [self.navigationController pushViewController:ViewController animated:YES];
  775. }
  776. - (IBAction)emailWishlistClick:(UIBarButtonItem *)sender {
  777. if ([[self.content_data valueForKey:@"count"] intValue] < 1) {
  778. [RAUtils message_box:@"Warning" message:@"Please add wishlist first" completion:nil];
  779. return;
  780. }
  781. JKMessageBoxController *emailAddrVC = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter email address"];
  782. emailAddrVC.textFiled.secureTextEntry = NO;
  783. emailAddrVC.yesButtonTitle = @"send";
  784. __weak typeof(emailAddrVC) weakVC = emailAddrVC;
  785. __weak typeof(self) weakself = self;
  786. emailAddrVC.textHandler = ^(NSString *text){
  787. // 验证邮箱格式是否正确
  788. // NSString *match = EMAIL_MATCHES;
  789. // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  790. // BOOL isEmailAddr = [predicate evaluateWithObject:text];
  791. BOOL isEmailAddr = YES;
  792. if (isEmailAddr) {
  793. // 验证是邮件地址,发送邮件
  794. [weakVC dismissViewControllerAnimated:YES completion:^{
  795. // __block UIAlertController *waitting_alert = [RAUtils waiting_alert:self title:@"Sending Email" completion:^{
  796. PopWaitAlert* pop=[RAUtils waiting_pop:@"Sending Email" completion:nil];
  797. [RANetwork request_emailwishlist:text completionHandler:^(NSMutableDictionary *result) {
  798. DebugLog(@"email wishlist result: %@",result);
  799. // [waitting_alert dismissViewControllerAnimated:YES completion:^{
  800. [pop hide];
  801. // [waitting_alert dismissWithClickedButtonIndex:0 animated:YES];
  802. if ([[result objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  803. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"The email send to %@ failed",text] preferredStyle:UIAlertControllerStyleAlert];
  804. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  805. }];
  806. [errorAlertVC addAction:action];
  807. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  808. }else {
  809. [RAUtils message_box:@"Email was sent successfully" message:@"" completion:nil];
  810. }
  811. // }];
  812. }];
  813. // }];
  814. }];
  815. } else {
  816. // 非邮件地址,警告
  817. [weakVC warning:@"Please enter right email address"];
  818. }
  819. };
  820. [self presentViewController:emailAddrVC animated:YES completion:nil];
  821. }
  822. - (void)reRefreshView {
  823. [self.itemListTable reloadData];
  824. }
  825. #pragma mark - RA_NOTIFICAITON
  826. -(void) refresh_ui
  827. {
  828. [self.itemListTable reloadData];
  829. }
  830. -(void) reload_data {
  831. __weak typeof(self) weakself = self;
  832. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  833. if (weakself) {
  834. __strong typeof(weakself) strongself = weakself;
  835. [strongself operation_reload_data];
  836. }
  837. }];
  838. [self.dataOperationQueue addOperation:operation];
  839. }
  840. @end