WatchListViewController.m 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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 "iSalesNetwork.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. #define ALERT_DEL 1025
  23. //#import "PDFViewController.h"
  24. @interface WatchListViewController ()
  25. @property (nonatomic,strong) SortItemViewController *sortItemController;
  26. @property (nonatomic,assign) int sortIndex;
  27. @property (nonatomic,strong) SortButton *sortButton;
  28. @property (nonatomic,strong) UILabel *qtyLabel;
  29. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  30. @end
  31. @implementation WatchListViewController
  32. - (NSOperationQueue *)dataOperationQueue {
  33. if (!_dataOperationQueue) {
  34. _dataOperationQueue = [[NSOperationQueue alloc] init];
  35. _dataOperationQueue.maxConcurrentOperationCount = 1;
  36. }
  37. return _dataOperationQueue;
  38. }
  39. -(void) logout
  40. {
  41. self.content_data = nil;
  42. }
  43. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  44. {
  45. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  46. if (self) {
  47. // Custom initialization
  48. }
  49. return self;
  50. }
  51. -(void) reload_container_getdata:(bool) update_data
  52. {
  53. [super reload_container_getdata:update_data];
  54. if(update_data)
  55. [self reload_data];
  56. else
  57. [self.itemListTable reloadData];
  58. }
  59. - (IBAction)onAddtoCart:(id)sender {
  60. [RAUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  61. return;
  62. UIApplication * app = [UIApplication sharedApplication];
  63. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  64. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  65. if(appDelegate.bLogin==false)
  66. {
  67. LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  68. // loginvc.delegate = self;
  69. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  70. loginvc.returnValue = ^(bool blogin){
  71. // UIApplication * app = [UIApplication sharedApplication];
  72. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  73. if(blogin)
  74. {
  75. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  76. {
  77. [main_vc checklogin:false];
  78. if(appDelegate.can_create_order)
  79. { NSString* msg =@"";
  80. if(appDelegate.contact_id.length>0)
  81. {
  82. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  83. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  84. }
  85. 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];
  86. // alert.
  87. [alert show];
  88. }
  89. else
  90. {
  91. 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];
  92. // alert.
  93. [alert show];
  94. }
  95. }
  96. else
  97. {
  98. [main_vc checklogin:true];
  99. }
  100. }
  101. // self.offset = 0;
  102. // [self.content_data removeAllObjects];
  103. // [self loadpage];
  104. //
  105. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  106. };
  107. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  108. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  109. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  110. [self presentViewController:navi animated:YES completion:^{
  111. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  112. NSLog(@"LoginViewController present.........");
  113. // self.btop = false;
  114. // <#code#>
  115. }];
  116. }
  117. else
  118. {
  119. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  120. {
  121. [main_vc checklogin:false];
  122. if(appDelegate.can_create_order)
  123. {
  124. NSString* msg =@"";
  125. if(appDelegate.contact_id.length>0)
  126. {
  127. msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  128. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  129. }
  130. 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];
  131. // alert.
  132. [alert show];
  133. }
  134. else
  135. {
  136. 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];
  137. // alert.
  138. [alert show];
  139. }
  140. }
  141. else
  142. {
  143. if(appDelegate.order_code==nil)
  144. [ self neworder];
  145. else
  146. [self addtocart];
  147. }
  148. }
  149. }
  150. -(void) addtocart
  151. {
  152. NSMutableArray* checked = [[NSMutableArray alloc] init];
  153. NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  154. int count = [[self.content_data valueForKey:@"count"] intValue];
  155. for(int i=0;i<count;i++)
  156. {
  157. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  158. bool check = [[item_json valueForKey:@"check"] boolValue];
  159. if(check)
  160. {
  161. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  162. [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  163. }
  164. }
  165. if(checked.count==0)
  166. {
  167. [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Add to cart." controller:self] ;
  168. return;
  169. }
  170. NSString * ids = [checked_iid componentsJoinedByString:@","];
  171. self.btnAddtoCart.enabled = false;
  172. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  173. NSDictionary* return_json = [iSalesNetwork move_wish2cart:ids];
  174. dispatch_async(dispatch_get_main_queue(), ^{
  175. self.btnAddtoCart.enabled = true;
  176. if([[return_json valueForKey:@"result"] intValue]==2)
  177. {
  178. // int result=[[return_json valueForKey:@"result"] intValue];re
  179. // if(result==2)
  180. // {
  181. //successed.
  182. // NSString* order_code = [return_json valueForKey:@"order_code"];
  183. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  184. // appDelegate.order_code = order_code;
  185. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  186. // appDelegate.order_code = order_code;
  187. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  188. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:true];
  189. [RAUtils message_alert:nil title:@" Multiple items moved to Cart" controller:self] ;
  190. // [self end_select];
  191. // }
  192. }
  193. else
  194. {
  195. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Move To Cart" controller:self] ;
  196. }
  197. });
  198. });
  199. }
  200. -(void) neworder
  201. {
  202. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  203. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  204. NSDictionary* return_json = [iSalesNetwork new_Order];
  205. dispatch_async(dispatch_get_main_queue(), ^{
  206. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  207. if([[return_json valueForKey:@"result"] intValue]==2)
  208. {
  209. int result=[[return_json valueForKey:@"result"] intValue];
  210. if(result==2)
  211. {
  212. //successed.
  213. NSString* order_code = [return_json valueForKey:@"orderCode"];
  214. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  215. appDelegate.order_code = order_code;
  216. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  217. [self addtocart];
  218. // [self ReloadData];
  219. // if(self.shopCartBlock!=nil)
  220. // {
  221. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  222. //
  223. //
  224. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  225. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  226. // iv.image = img;
  227. // // [self.contentView addSubview:iv];
  228. // self.shopCartBlock(iv);
  229. //
  230. // }
  231. }
  232. }
  233. else
  234. {
  235. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  236. }
  237. });
  238. });
  239. }
  240. //- (IBAction)onCreateTearSheet:(id)sender {
  241. //
  242. //
  243. // self.pdf_url= nil;
  244. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  245. //
  246. // NSMutableArray* checked_iid = [[NSMutableArray alloc] init];
  247. //
  248. // int count = [[self.content_data valueForKey:@"count"] intValue];
  249. // for(int i=0;i<count;i++)
  250. // {
  251. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  252. // bool check = [[item_json valueForKey:@"check"] boolValue];
  253. // if(check)
  254. // {
  255. // [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"product_id"] intValue]]];
  256. // [checked_iid addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  257. // }
  258. // }
  259. // // disable for debug
  260. // if(checked.count==0)
  261. // {
  262. // [RAUtils message_alert:@"You must select at least one model." title:@"Cannot Create Portfolio." controller:self] ;
  263. // return;
  264. // }
  265. //
  266. //
  267. //
  268. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  269. //
  270. // NSString * string = [checked componentsJoinedByString:@","];
  271. //
  272. // NSString * string_iid = [checked_iid componentsJoinedByString:@","];
  273. // [params setValue:string forKey:@"product_ids"];
  274. // [params setValue:string_iid forKey:@"item_ids"];
  275. // TearSheetParamViewController * VC =[ self.storyboard instantiateViewControllerWithIdentifier:@"TearSheetParamViewController"];
  276. //
  277. //
  278. // VC.url_type = URL_REMOTE;
  279. // VC.request_url=URL_DM_PARAMS;
  280. //
  281. // VC.params = params;
  282. //
  283. //// VC.delegate=self;
  284. //
  285. // VC.return_url=^(NSString* url,NSString* path, bool remove_item)
  286. // {
  287. // self.pdf_url = url;
  288. // self.path = path;
  289. // self.remove_item = remove_item;
  290. // };
  291. ////AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  292. // VC.onDismiss=^(NSString* catelog_name)
  293. // {
  294. //
  295. // };
  296. //
  297. // //
  298. // // if(checked.count==count)
  299. // // {
  300. // // orderinfoVC.have_tail = true
  301. // // }
  302. //
  303. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:VC] ;
  304. //
  305. //
  306. //
  307. //
  308. //
  309. // navi.modalPresentationStyle = UIModalPresentationPageSheet;//有三种状态,自己看看是哪种
  310. // [self presentViewController:navi animated:YES completion:^{
  311. //
  312. // NSLog(@"TearSheetParamViewController present.........");
  313. //
  314. // // self.btop = false;
  315. // // <#code#>
  316. // }];
  317. //
  318. // [self.btnselect setTitle:@"Select all"];
  319. //
  320. //
  321. //}
  322. - (void)viewDidLoad
  323. {
  324. [super viewDidLoad];
  325. NSLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  326. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  327. ref.tag = 200 ;
  328. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  329. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  330. // ref.hidden = true;
  331. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  332. [self.itemListTable addSubview:ref];
  333. NSLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  334. // [[UIToolbar appearance] setBackgroundColor: [UIColor whiteColor]];
  335. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  336. self.label_net_err.layer.borderWidth = 2.0;
  337. self.label_net_err.layer.cornerRadius=15;
  338. self.label_net_err.layer.masksToBounds=true;
  339. CGPoint center = self.mum.center;
  340. self.mum.frame = CGRectMake(0, 0, 100, 100);
  341. self.mum.center = center;
  342. self.mum.backgroundColor = [UIColor colorWithHEX:0xA5A5A5 alpha:0.5];
  343. self.mum.layer.cornerRadius = 5.0f;
  344. self.mum.layer.masksToBounds = YES;
  345. self.sortIndex = 0;
  346. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  347. // tap.minimumPressDuration = 0.8; //定义按的时间
  348. [self.label_net_err addGestureRecognizer:tap];
  349. // [self reload_data];
  350. }
  351. -(void)manually_refresh
  352. {
  353. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  354. if(self.isrefreshing)
  355. {
  356. [reF endRefreshing];
  357. return;
  358. }
  359. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  360. [self performSelector:@selector(reload_data) withObject:nil afterDelay:1];
  361. // DebugLog(@"refresh!!!!!!!!");
  362. }
  363. - (IBAction)onSelectClick:(id)sender {
  364. // self.total=0;
  365. if([self.btnselect.title isEqualToString:@"Select all"])
  366. {
  367. int count =[[self.content_data valueForKey:@"count"] intValue];
  368. for(int i=0;i<count;i++)
  369. {
  370. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  371. [item_json setValue:@"1" forKey:@"check"];
  372. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  373. cell.img_checkmark.hidden=false;
  374. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  375. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  376. }
  377. [self.btnselect setTitle:@"Deselect all"];
  378. }
  379. else
  380. {
  381. int count =[[self.content_data valueForKey:@"count"] intValue];
  382. for(int i=0;i<count;i++)
  383. {
  384. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)i]] mutableCopy];
  385. [item_json setValue:@"0" forKey:@"check"];
  386. ModelItemCell* cell=(ModelItemCell*)[self.itemListTable cellForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  387. cell.img_checkmark.hidden=true;
  388. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i]];
  389. }
  390. [self.btnselect setTitle:@"Select all"];
  391. // self.btnselect.titleLabel.text=@"Select all";
  392. }
  393. }
  394. - (IBAction)onDeleteClick:(id)sender {
  395. NSMutableArray* checked = [[NSMutableArray alloc] init];
  396. int count = [[self.content_data valueForKey:@"count"] intValue];
  397. for(int i=0;i<count;i++)
  398. {
  399. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  400. int check = [[item_json valueForKey:@"check"] intValue];
  401. if(check==1)
  402. {
  403. [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"item_id"] intValue]]];
  404. }
  405. }
  406. // disable for debug
  407. if(checked.count==0)
  408. return;
  409. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  410. NSString * string = [checked componentsJoinedByString:@","];
  411. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  412. NSDictionary* cart_json = [iSalesNetwork watchlist_remove:string];
  413. dispatch_async(dispatch_get_main_queue(), ^{
  414. if([[cart_json valueForKey:@"result"] intValue]==2)
  415. {
  416. [self reload_data];
  417. [self.btnselect setTitle:@"Select all"];
  418. }
  419. else
  420. {
  421. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Remove Model From Wish List" controller:self] ;
  422. }
  423. });
  424. });
  425. }
  426. -(void) viewWillAppear:(BOOL)animated
  427. {
  428. [super viewWillAppear:animated];
  429. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  430. }
  431. -(void) operation_reload_data
  432. {
  433. if (self.dataOperationQueue.operationCount > 1) {
  434. return;
  435. }
  436. dispatch_async(dispatch_get_main_queue(), ^{
  437. if(self.isrefreshing)
  438. return;
  439. // [self.content_data removeAllObjects];
  440. // [self.itemListTable reloadData];
  441. self.isrefreshing=true;
  442. self.label_net_err.hidden=true;
  443. self.itemListTable.hidden=true;
  444. self.mum.center = self.view.center;
  445. self.mum.hidden = false;
  446. [self.mum startAnimating];
  447. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  448. [reF endRefreshing];
  449. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  450. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  451. NSDictionary* cart_json = [iSalesNetwork request_WatchList:self.sortIndex];
  452. dispatch_async(dispatch_get_main_queue(), ^{
  453. [self.mum stopAnimating];
  454. self.isrefreshing=false;
  455. if (self.dataOperationQueue.operationCount > 1) {
  456. return ;
  457. }
  458. int result=[[cart_json valueForKey:@"result"] intValue];
  459. self.content_data = [cart_json mutableCopy];
  460. [self.itemListTable reloadData];
  461. if(result==2||result==1||result==0)
  462. {
  463. self.itemListTable.hidden=false;
  464. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  465. appDelegate.wish_count =[[self.content_data valueForKey:@"count"] intValue];
  466. }
  467. else
  468. if(result==RESULT_NET_ERROR)
  469. {
  470. self.label_net_err.hidden=false;
  471. self.itemListTable.hidden=true;
  472. }
  473. else
  474. {
  475. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Loading Wish list" controller:self] ;
  476. }
  477. self.isrefreshing=false;
  478. });
  479. });
  480. });
  481. }
  482. -(void) reload_data {
  483. __weak typeof(self) weakself = self;
  484. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  485. if (weakself) {
  486. __strong typeof(weakself) strongself = weakself;
  487. [strongself operation_reload_data];
  488. }
  489. }];
  490. [self.dataOperationQueue addOperation:operation];
  491. }
  492. - (void)didReceiveMemoryWarning
  493. {
  494. [super didReceiveMemoryWarning];
  495. // Dispose of any resources that can be recreated.
  496. }
  497. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  498. //{
  499. //
  500. // [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  501. //
  502. //
  503. //
  504. //// [self.itemListTable reloadData];
  505. // NSLog(@"%@",NSStringFromCGRect(self.itemListTable.frame));
  506. //
  507. //
  508. //}
  509. #pragma mark - Table view data source
  510. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  511. //{
  512. // return 0;
  513. //}
  514. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  515. //{
  516. // return 0;
  517. //}
  518. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  519. //{
  520. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  521. // return myView;
  522. //
  523. //}
  524. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  525. //{
  526. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  527. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  528. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  529. //// titleLabel.textColor=[UIColor whiteColor];
  530. //// titleLabel.backgroundColor = [UIColor clearColor];
  531. //// if(section==0)
  532. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  533. //// else
  534. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  535. //// [titleLabel sizeToFit];
  536. //// [myView addSubview:titleLabel];
  537. ////
  538. // return myView;
  539. //}
  540. //
  541. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  542. // if(section==0)
  543. // return nil;
  544. // else
  545. // return @"detail section";
  546. //}
  547. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  548. {
  549. return 33;
  550. }
  551. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  552. {
  553. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  554. if(value==nil)
  555. value=@"";
  556. unsigned long color = strtoul([value UTF8String],0,16);
  557. NSString *sectionTitle = @"Wish List";
  558. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 33)];
  559. // UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
  560. // sortButton.frame = CGRectMake(20, 5.5, (33 - 5.5 * 2), (33 - 5.5 * 2));
  561. // [sortButton setImage:[UIImage imageNamed:@"check_1_24"] forState:UIControlStateNormal];
  562. // [sortButton addTarget:self action:@selector(wishlistSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  563. // [myView addSubview:sortButton];
  564. [myView addSubview:self.sortButton];
  565. UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5.5, tableView.bounds.size.width, 22)];
  566. titlelabel.textColor=UIColorFromRGB(color);
  567. titlelabel.textAlignment=NSTextAlignmentCenter;
  568. titlelabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
  569. titlelabel.text=sectionTitle;
  570. titlelabel.center = myView.center;
  571. myView.autoresizesSubviews=true;
  572. [myView addSubview:titlelabel];
  573. [myView addSubview:self.qtyLabel];
  574. return myView;
  575. }
  576. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  577. {
  578. return 1;
  579. }
  580. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  581. {
  582. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  583. int count =[[self.content_data valueForKey:@"count"] intValue];
  584. return count;
  585. }
  586. - (IBAction)imgbtnClicked:(UIButton *)sender {
  587. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  588. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  589. // NSLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  590. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  591. NSString* product_id = [item_json valueForKey:@"product_id"];
  592. DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  593. // dvc
  594. dvc.product_id=product_id;
  595. dvc.category_id=nil;
  596. dvc.ispush=true;
  597. [dvc reload];
  598. [self.navigationController pushViewController:dvc animated:true];
  599. }
  600. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  601. {
  602. // if(tableView==self.itemListTable)
  603. // {
  604. NSString *CellIdentifier = @"WatchListItemCell";
  605. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  606. NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  607. NSString* img_url = [item_json valueForKey:@"img"];
  608. NSString* description = [item_json valueForKey:@"description"];
  609. // NSString* identifier = [item_json valueForKey:@"identifier"];
  610. // NSString* attribute = [item_json valueForKey:@"attribute"];
  611. // NSString* currency = [item_json valueForKey:@"currency"];
  612. // int count =[[item_json valueForKey:@"count"] intValue];
  613. NSString* price =[item_json valueForKey:@"tear_sheet_price"];
  614. // cell.labelAttribute.text = attribute;
  615. // cell.labelCurrency.text = currency;
  616. cell.labelDescription.text = description;
  617. // cell.labelIdentifier.text = identifier;
  618. cell.labelPrice.text = price;
  619. // cell.editCount.text =[NSString stringWithFormat:@"%d",count];
  620. // NSDictionary* val_json =[cadedate_json objectForKey:[NSString stringWithFormat:@"val_%d",i]];
  621. int check = [[item_json valueForKey:@"check"] intValue];
  622. if(check==1)
  623. cell.img_checkmark.hidden=false;
  624. else
  625. cell.img_checkmark.hidden=true;
  626. // UIImageView* iv =cell.btnImage.imageView;
  627. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  628. NSString* file_name=[img_url lastPathComponent];
  629. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  630. if(img_data!=nil)
  631. {
  632. UIImage * img =[UIImage imageWithData:img_data];
  633. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  634. // cell.btnImage.imageView.image = img;
  635. }
  636. else
  637. {
  638. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  639. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  640. dispatch_async(dispatch_get_main_queue(), ^{
  641. if(downloadimg_data!=nil)
  642. {
  643. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  644. UIImage * img =[UIImage imageWithData:downloadimg_data];
  645. [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  646. }
  647. else
  648. [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  649. });
  650. });
  651. }
  652. cell.backgroundColor = [UIColor whiteColor];// [UIColor whiteColor];;
  653. int qty = [item_json[@"cart_count"] integerValue];
  654. cell.qty_Label.text = [NSString stringWithFormat:@"QTY: %d",qty];
  655. return cell;
  656. }
  657. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  658. {
  659. ModelItemCell* cell = (ModelItemCell*)[tableView cellForRowAtIndexPath:indexPath];
  660. cell.selected = false;
  661. // self.dirty = true;
  662. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  663. int check = [[item_json valueForKey:@"check"] intValue];
  664. if(check==1)
  665. {
  666. [item_json setValue:@"0" forKey:@"check"];
  667. cell.img_checkmark.hidden=true;
  668. [self.btnselect setTitle: @"Select all"];
  669. }
  670. else
  671. {
  672. [item_json setValue:@"1" forKey:@"check"];
  673. cell.img_checkmark.hidden=false;
  674. }
  675. [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  676. }
  677. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  678. return UITableViewCellEditingStyleDelete;
  679. }
  680. //#pragma mark - UIAlertViewDelegate
  681. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  682. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  683. //{
  684. //
  685. //
  686. //
  687. //}
  688. - (BOOL)isPureFloat:(NSString*)string{
  689. NSScanner* scan = [NSScanner scannerWithString:string];
  690. float val;
  691. return[scan scanFloat:&val] && [scan isAtEnd];
  692. }
  693. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  694. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  695. {
  696. // 添加一个删除按钮
  697. self.indexPath=indexPath;
  698. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  699. NSLog(@"delete click");
  700. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Delete confirm", nil) message:NSLocalizedString(@"Are you sure remove model from wishlist?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  701. alert.tag = ALERT_DEL;
  702. // alert.
  703. [alert show];
  704. }];
  705. deleteRowAction.backgroundColor = UIColorFromRGB(0x336699);
  706. // 添加一个编辑按钮
  707. // UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" Edit "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  708. //
  709. // NSLog(@"edit click");
  710. //
  711. //
  712. //
  713. // UIViewController* parentvc=self;
  714. //
  715. //
  716. //
  717. //
  718. // EditModelPriceViewController * vc =[ parentvc.storyboard instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  719. //
  720. //
  721. // NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  722. //
  723. //
  724. //
  725. // vc.set_watch_list=true;
  726. // vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  727. // vc.price = [[item_json valueForKey:@"tear_sheet_price"] floatValue];
  728. // vc.cart_id = [NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  729. // // vc.delegate = parentvc.self;
  730. // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  731. // vc.onSetValue = ^(){
  732. // [self reload_data];//[main_vc checklogin:true];
  733. // };
  734. //
  735. //
  736. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  737. //
  738. //
  739. //
  740. //
  741. //
  742. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  743. //
  744. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  745. // [parentvc presentViewController:navi animated:YES completion:^{
  746. //
  747. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  748. //
  749. // NSLog(@"EditModelPriceViewController present.........");
  750. //
  751. // // self.btop = false;
  752. // // <#code#>
  753. // }];
  754. //
  755. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  756. //
  757. //
  758. // }];
  759. // editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  760. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  761. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  762. return @[deleteRowAction];
  763. else
  764. return @[deleteRowAction];
  765. }
  766. - (void)tableView:(UITableView *)tableView
  767. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  768. }
  769. #pragma mark - UIAlertViewDelegate
  770. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  771. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  772. {
  773. UIApplication * app = [UIApplication sharedApplication];
  774. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  775. // if(alertView.tag==1234)
  776. // {
  777. //
  778. // // availability warring;
  779. //
  780. //
  781. // if(buttonIndex!=alertView.cancelButtonIndex)
  782. // {
  783. // if(appDelegate.order_code==nil)
  784. // {
  785. // [self neworder];
  786. // }
  787. // else
  788. // {
  789. // [self addtocart];
  790. // }
  791. // }
  792. // return;
  793. // }
  794. // int count=[self.quantity_text.text intValue];
  795. if(alertView.tag==ALERT_DEL)
  796. {
  797. if(buttonIndex!=alertView.cancelButtonIndex)
  798. {
  799. [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  800. NSMutableDictionary * item_json = [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)self.indexPath.row]] mutableCopy];
  801. NSString *ids=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"item_id"] intValue]];
  802. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  803. NSDictionary* cart_json = [iSalesNetwork watchlist_remove:ids];
  804. dispatch_async(dispatch_get_main_queue(), ^{
  805. if([[cart_json valueForKey:@"result"] intValue]==2)
  806. {
  807. [self reload_data];
  808. }
  809. else
  810. {
  811. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  812. }
  813. });
  814. });
  815. }
  816. return;
  817. }
  818. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  819. if(buttonIndex==alertView.cancelButtonIndex)
  820. {
  821. }
  822. else if(buttonIndex==1)
  823. {
  824. //open exist
  825. OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  826. ovc.showNavibar = true;
  827. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  828. if(appDelegate.order_code==nil)
  829. {
  830. [self neworder];
  831. }
  832. else
  833. {
  834. [self addtocart];
  835. [main_vc checklogin:true];
  836. }
  837. };
  838. ovc.init_style = OL_OPEN;
  839. ovc.onCancel = ^(){
  840. [main_vc checklogin:true];
  841. };
  842. [self.navigationController pushViewController:ovc animated:true];
  843. }
  844. else
  845. {
  846. //create new;
  847. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  848. {
  849. ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  850. cvc.showNavibar = true;
  851. cvc.contact_type = @"Sales_Order_Customer";
  852. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  853. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  854. appDelegate.customerInfo = value;
  855. if(appDelegate.order_code==nil)
  856. [self neworder];
  857. // [main_vc checklogin:true];
  858. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  859. //
  860. // if(self.returnValue)
  861. // self.returnValue(value);
  862. };
  863. cvc.onCancel = ^(){
  864. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"" controller:self];
  865. };
  866. cvc.onReset = ^(){
  867. [main_vc checklogin:true];
  868. };
  869. [self.navigationController pushViewController:cvc animated:true];
  870. }
  871. else
  872. {
  873. [self neworder];
  874. }
  875. }
  876. }
  877. #pragma mark - sort button
  878. - (void)wishlistSortButtonClicked:(UIButton *)sender {
  879. DebugLog(@"wish list sort button clicked");
  880. [self.view addSubview:self.sortItemController.view];
  881. }
  882. - (SortItemViewController *)sortItemController {
  883. if (!_sortItemController) {
  884. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(40, 40)];
  885. _sortItemController.sortIndex = self.sortIndex;
  886. __weak typeof(self) weakSelf = self;
  887. _sortItemController.sortBlock = ^(int sort){
  888. weakSelf.sortIndex = sort;
  889. [weakSelf reload_data];
  890. };
  891. }
  892. // _sortItemController.sortIndex = self.sortIndex;
  893. return _sortItemController;
  894. }
  895. - (SortButton *)sortButton {
  896. if (!_sortButton) {
  897. _sortButton = [SortButton sortButtonWithHeight:33];
  898. [_sortButton addTarget:self action:@selector(wishlistSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  899. }
  900. return _sortButton;
  901. }
  902. - (void)setSortIndex:(int)sortIndex {
  903. _sortIndex = sortIndex;
  904. NSString *selectedImageName = @"";
  905. switch (sortIndex) {
  906. case 0:{
  907. selectedImageName = @"TX_22";
  908. }
  909. break;
  910. case 1:{
  911. selectedImageName = @"TS_22";
  912. }
  913. break;
  914. case 2:{
  915. selectedImageName = @"IX_22";
  916. }
  917. break;
  918. case 3:{
  919. selectedImageName = @"IS_22";
  920. }
  921. break;
  922. case 4:{
  923. selectedImageName = @"DX_22";
  924. }
  925. break;
  926. default:
  927. break;
  928. }
  929. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  930. }
  931. - (UILabel *)qtyLabel {
  932. if (!_qtyLabel) {
  933. _qtyLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22)];
  934. _qtyLabel.text = @"QTY";
  935. _qtyLabel.textAlignment = NSTextAlignmentCenter;
  936. _qtyLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
  937. }
  938. return _qtyLabel;
  939. }
  940. #pragma mark - rotation
  941. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  942. self.sortItemController.view.frame = self.view.bounds;
  943. }
  944. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  945. return YES;
  946. }
  947. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  948. self.sortItemController.view.frame = self.view.bounds;
  949. self.qtyLabel.frame = CGRectMake(self.itemListTable.bounds.size.width - 200, 5.5, 40, 22);
  950. }
  951. @end