WatchListViewController.m 40 KB

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