WatchListViewController.m 43 KB

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