WatchListViewController.m 36 KB

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