HomerModelDetailHeaderCell.m 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. //
  2. // HomerModelDetailHeaderCell.m
  3. // iSales-HOMER
  4. //
  5. // Created by Jack on 2017/10/25.
  6. // Copyright © 2017年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "HomerModelDetailHeaderCell.h"
  9. #import "RANetwork.h"
  10. #import "MainViewController.h"
  11. #import "LoginViewController.h"
  12. #import "ContactListViewController.h"
  13. #import "RAUtils.h"
  14. #import "OrderListViewController.h"
  15. #import "ImageScrollerViewController.h"
  16. #import "CartUtils.h"
  17. #import "CustomIOSAlertView.h"
  18. #import "TextUtils.h"
  19. #import "ContentPreviewController.h"
  20. @implementation HomerModelDetailHeaderCell
  21. @synthesize celldelegate;
  22. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  23. {
  24. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  25. if (self) {
  26. // Initialization code
  27. }
  28. return self;
  29. }
  30. - (void)awakeFromNib
  31. {
  32. [super awakeFromNib];
  33. self.quantity_text.delegate = self;
  34. // MDHTMLLabelVerticalAlignmentCenter = 0,
  35. // MDHTMLLabelVerticalAlignmentTop = 1,
  36. self.informationLabel.numberOfLines = 0;
  37. // self.informationLabel.verticalAlignment=MDHTMLLabelVerticalAlignmentTop;
  38. self.informationLabel.backgroundColor = [UIColor yellowColor];
  39. // self.selector_Button.layer.borderWidth = 0.5;
  40. // self.selector_Button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  41. // self.selector_Button.layer.cornerRadius = 0;
  42. [self.descriptionRTLabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  43. [self.descriptionRTLabel setTextAlignment:RTTextAlignmentCenter];
  44. [self.descriptionRTLabel setFont:[UIFont systemFontOfSize:22]];
  45. [self addObserver:self
  46. forKeyPath:@"stack_contents"
  47. options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  48. context:@"content changed"];
  49. self.inforframe=self.informationLabel.frame.size;
  50. // [self generate_information];
  51. }
  52. -(void) dealloc
  53. {
  54. [self removeObserver:self forKeyPath:@"stack_contents"];
  55. }
  56. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  57. {
  58. if([keyPath isEqualToString:@"stack_contents"])
  59. {
  60. [self.playerView stopVideo];
  61. self.playerView.hidden=true;
  62. }
  63. }
  64. - (void)layoutSubviews
  65. {
  66. [super layoutSubviews];
  67. // [self generate_information];
  68. }
  69. -(void) generate_information
  70. {
  71. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  72. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:16px;font-family:Helvetica' ><b>Availability:</b> %@</font>",self.count_Label.text]];
  73. if(self.incomingcount_Label.text.length>0)
  74. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:16px;font-family:Helvetica' ><b>Incoming stock:</b> %@</font>",self.incomingcount_Label.text]];
  75. if(self.etaval_label.text.length>0)
  76. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:16px;font-family:Helvetica' ><b>Next Arrival Date:</b> %@</font>",self.etaval_label.text]];
  77. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:16px;font-family:Helvetica' ><b>Availability in 7-10 days:</b> %@</font>",self.hmlg_stock_count_lb.text]];
  78. if(self.cqyt_label.text.length>0)
  79. [arr_addr addObject:[NSString stringWithFormat:@"<font style='font-size:16px;color:red';font-family:Helvetica><b>%@</b></font>",self.cqyt_label.text]];
  80. NSString * information_str = [RAConvertor arr2string:arr_addr separator:@"<br>" trim:true];
  81. // UIFont *font=[UIFont systemFontOfSize:14];
  82. //// label.font=font;
  83. NSDictionary *optoins=@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
  84. NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[information_str dataUsingEncoding:NSUnicodeStringEncoding] options:optoins documentAttributes:nil error:nil];
  85. // self.informationLabel.numberOfLines
  86. // self.informationLabel.lineBreakMode
  87. //
  88. // self.informationLabel.adjustsFontSizeToFitWidth = YES;
  89. // self.informationLabel.minimumFontSize = 0.5;
  90. self.informationLabel.attributedText=attrStr;
  91. // self.informationLabel.text = @"文字在语言学中指书面语的视觉形式,古代把独体字叫做“文”,把合体字叫做“字”,如今联合起来叫做“文字”,文字的基本个体叫做“字”。在日常生活中,";
  92. // [self.informationLabel sizeToFit];
  93. // CGSize inforsize=[self.informationLabel sizeThatFits:self.informationLabel.frame.size];
  94. // CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrStr);
  95. // //计算文本绘制size ,这里300是文字宽度,你可以自己更改为247,但是要记得,在height 方法里的这个位置,也改为247
  96. // CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.informationLabel.frame.size.width, MAXFLOAT), NULL);
  97. //
  98. //
  99. //
  100. // CFRelease(framesetter);
  101. // CGSize lblSize = [self.informationLabel.text boundingRectWithSize:self.informationLabel.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
  102. // self.informationLabel.frame = CGRectMake(self.informationLabel.frame.origin.x, self.informationLabel.frame.origin.y, tmpSize.width, tmpSize.height);
  103. // [self.informationLabel sizeToFit];
  104. UILabel* textLabel = [UILabel new];
  105. textLabel.attributedText = attrStr;
  106. textLabel.numberOfLines = 0;
  107. textLabel.lineBreakMode = NSLineBreakByWordWrapping;
  108. // [textLabel sizeToFit];
  109. CGSize newsize = [textLabel sizeThatFits:self.inforframe];
  110. self.informationLabel.frame = CGRectMake(self.informationLabel_arch.frame.origin.x, CGRectGetMaxY(self.informationLabel.frame)-newsize.height, newsize.width, newsize.height);
  111. //
  112. }
  113. - (IBAction)add_toPortfolio:(id)sender {
  114. UIApplication * app = [UIApplication sharedApplication];
  115. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  116. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER) {
  117. [[NSNotificationCenter defaultCenter] postNotificationName:No_Rights_Notification object:nil];
  118. return;
  119. }
  120. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  121. if(appDelegate.bLogin==false)
  122. {
  123. UIViewController *vc= [RAUtils getViewController:self];
  124. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  125. loginvc.returnValue = ^(bool blogin){
  126. if(blogin)
  127. {
  128. if(self.set_update_data)
  129. self.set_update_data(true);
  130. [main_vc checklogin:true];
  131. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  132. NSDictionary* return_json = [RANetwork add_toPortfolio:self.product_id withScreen:ScreenCodeModelInfo];
  133. dispatch_async(dispatch_get_main_queue(), ^{
  134. if([[return_json valueForKey:@"result"] intValue]==2)
  135. {
  136. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  137. if(self.WatchlistBlock!=nil)
  138. {
  139. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  140. CGRect iv_rect = CGRectMake(self.btnaddPortfolio.center.x-50, self.btnaddPortfolio.center.y-50, 100, 100);
  141. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  142. iv.image = img;
  143. self.PortfolioBlock(iv);
  144. }
  145. }
  146. else
  147. {
  148. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add to portfolio" controller:vc] ;
  149. }
  150. });
  151. });
  152. }
  153. };
  154. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  155. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  156. [vc presentViewController:navi animated:YES completion:^{
  157. }];
  158. }
  159. else
  160. {
  161. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  162. NSDictionary* return_json = [RANetwork add_toPortfolio:self.product_id withScreen:ScreenCodeModelInfo];
  163. dispatch_async(dispatch_get_main_queue(), ^{
  164. if([[return_json valueForKey:@"result"] intValue]==2)
  165. {
  166. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  167. if(self.WatchlistBlock!=nil)
  168. {
  169. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  170. CGRect iv_rect = CGRectMake(self.btnaddPortfolio.center.x-50, self.btnaddPortfolio.center.y-50, 100, 100);
  171. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  172. iv.image = img;
  173. self.PortfolioBlock(iv);
  174. }
  175. }
  176. else
  177. {
  178. UIViewController *vc= [RAUtils getViewController:self];
  179. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add to portfolio" controller:vc] ;
  180. }
  181. });
  182. });
  183. }
  184. }
  185. - (IBAction)add_toWatchList:(id)sender {
  186. UIApplication * app = [UIApplication sharedApplication];
  187. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  188. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  189. if(appDelegate.bLogin==false)
  190. {
  191. UIViewController *vc= [RAUtils getViewController:self];
  192. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  193. loginvc.returnValue = ^(bool blogin){
  194. if(blogin)
  195. {
  196. if(self.set_update_data)
  197. self.set_update_data(true);
  198. [main_vc checklogin:true];
  199. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  200. NSDictionary* return_json = [RANetwork add_toWatchList:self.product_id withScreen:ScreenCodeModelInfo];
  201. dispatch_async(dispatch_get_main_queue(), ^{
  202. if([[return_json valueForKey:@"result"] intValue]==2)
  203. {
  204. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  205. if(self.WatchlistBlock!=nil)
  206. {
  207. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  208. CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  209. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  210. iv.image = img;
  211. self.WatchlistBlock(iv);
  212. }
  213. }
  214. else
  215. {
  216. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add Wish List" controller:vc] ;
  217. }
  218. });
  219. });
  220. }
  221. };
  222. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  223. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  224. [vc presentViewController:navi animated:YES completion:^{
  225. }];
  226. }
  227. else
  228. {
  229. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  230. NSDictionary* return_json = [RANetwork add_toWatchList:self.product_id withScreen:ScreenCodeModelInfo];
  231. dispatch_async(dispatch_get_main_queue(), ^{
  232. if([[return_json valueForKey:@"result"] intValue]==2)
  233. {
  234. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  235. if(self.WatchlistBlock!=nil)
  236. {
  237. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  238. CGRect iv_rect = CGRectMake(self.btnaddWish.center.x-50, self.btnaddWish.center.y-50, 100, 100);
  239. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  240. iv.image = img;
  241. self.WatchlistBlock(iv);
  242. }
  243. }
  244. else
  245. {
  246. UIViewController *vc= [RAUtils getViewController:self];
  247. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add Wish List" controller:vc] ;
  248. }
  249. });
  250. });
  251. }
  252. }
  253. - (IBAction)add_toCart:(id)sender {
  254. UIViewController *vc= [RAUtils getViewController:self];
  255. [CartUtils add_to_cart:vc selectorholder:self selector:@selector(addtocart)];
  256. return;
  257. }
  258. -(void) addtocart
  259. {
  260. int count=[self.quantity_text.text intValue];
  261. #ifdef MPACK
  262. if (count % self.step != 0) {
  263. UIViewController *vc= [RAUtils getViewController:self];
  264. [RAUtils message_alert:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] title:@"Add to cart" controller:vc];
  265. return;
  266. }
  267. #endif
  268. int c=[self.count_Label.text intValue];
  269. DebugLog(@"count label : %@ , convert:%d", self.count_Label.text ,c);
  270. NSString* ct=[self.count_Label.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  271. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  272. if(/*c<count*/!appDelegate.can_create_backorder && c == 0)
  273. {
  274. NSString *msg = nil;
  275. BOOL continueAdd = YES;
  276. // #ifdef BUILD_NPD
  277. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  278. if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  279. if ([Singleton sharedInstance].currentOrderIsMerged) { // Purchase Order不允许购买数量大于库存
  280. msg = [NSString stringWithFormat:@"Item is OUT OF STOCK and not available to order from %@.",COMPANY_SHORT_NAME];
  281. continueAdd = NO;
  282. } else { // Shop Order购买数量大于库存需要确认
  283. msg = [NSString stringWithFormat:@"Item is OUT OF STOCK and not available to order from %@.\nAdd to cart anyway?",COMPANY_SHORT_NAME];
  284. continueAdd = YES;
  285. }
  286. }
  287. if (appDelegate.user_type == USER_ROLE_EMPLOYEE) { // Employee不允许购买数量大于库存
  288. msg = @"Item is OUT OF STOCK and not available to order.";
  289. continueAdd = NO;
  290. }
  291. // #endif
  292. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Availability %@",ct ] message:msg preferredStyle:UIAlertControllerStyleAlert];
  293. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  294. if (continueAdd) {
  295. [self addtocart:count];
  296. }
  297. }];
  298. [alertControl addAction:alertthree];
  299. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  300. }];
  301. if (continueAdd) {
  302. [alertControl addAction:alertcancel];
  303. }
  304. UIViewController *vc= [RAUtils getViewController:self];
  305. [vc presentViewController:alertControl animated:YES completion:nil];
  306. }
  307. else
  308. {
  309. [self addtocart:count];
  310. }
  311. }
  312. -(void)Hide_selector:(bool) bhide
  313. {
  314. // self.selector_Button.hidden=bhide;
  315. // self.selector_dorpdown.hidden=bhide;
  316. // self.selector_imageView.hidden=bhide;
  317. //
  318. }
  319. -(void) addtocart:(int) count
  320. {
  321. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  322. if(self.cqty>0)
  323. {
  324. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"QTY: %d of this model already in cart. Continue ?",self.cqty] message:nil preferredStyle:UIAlertControllerStyleAlert];
  325. //block代码块取代了delegate
  326. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  327. int c=self.cqty+count;
  328. int m=c%self.step;
  329. if(m!=0&&appDelegate.alert_sold_in_quantities)
  330. {
  331. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",self.step] message:nil preferredStyle:UIAlertControllerStyleAlert];
  332. //block代码块取代了delegate
  333. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  334. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  335. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  336. dispatch_async(dispatch_get_main_queue(), ^{
  337. if([[return_json valueForKey:@"result"] intValue]==2)
  338. {
  339. // NSString* order_code = [return_json valueForKey:@"order_code"];
  340. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  341. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  342. if(self.shopCartBlock!=nil)
  343. {
  344. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  345. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  346. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  347. iv.image = img;
  348. // [self.contentView addSubview:iv];
  349. self.shopCartBlock(iv);
  350. self.cqty +=count;
  351. if(self.set_cqty)
  352. self.set_cqty(self.cqty);
  353. }
  354. }
  355. else
  356. {
  357. UIViewController *vc= [RAUtils getViewController:self];
  358. NSString *msg = [return_json valueForKey:@"err_msg"];
  359. NSString *title = @"Add To Cart";
  360. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  361. title = @"Add To Cart: Out of Stock";
  362. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  363. self.btnNotifyMe.enabled = YES;
  364. self.btnaddCart.enabled = NO;
  365. }
  366. [RAUtils message_alert:msg title:title controller:vc] ;
  367. }
  368. });
  369. });
  370. }];
  371. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  372. DebugLog(@"No");
  373. }];
  374. [alertControl addAction:actionOne];
  375. [alertControl addAction:alertthree];
  376. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  377. UIViewController *vc= [RAUtils getViewController:self];
  378. [vc presentViewController:alertControl animated:YES completion:nil];
  379. }
  380. else
  381. {
  382. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  383. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  384. dispatch_async(dispatch_get_main_queue(), ^{
  385. if([[return_json valueForKey:@"result"] intValue]==2)
  386. {
  387. // NSString* order_code = [return_json valueForKey:@"order_code"];
  388. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  389. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  390. if(self.shopCartBlock!=nil)
  391. {
  392. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  393. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  394. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  395. iv.image = img;
  396. // [self.contentView addSubview:iv];
  397. self.shopCartBlock(iv);
  398. self.cqty +=count;
  399. if(self.set_cqty)
  400. self.set_cqty(self.cqty);
  401. }
  402. }
  403. else
  404. {
  405. UIViewController *vc= [RAUtils getViewController:self];
  406. NSString *msg = [return_json valueForKey:@"err_msg"];
  407. NSString *title = @"Add To Cart";
  408. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  409. title = @"Add To Cart: Out of Stock";
  410. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  411. self.btnNotifyMe.enabled = YES;
  412. self.btnaddCart.enabled = NO;
  413. }
  414. [RAUtils message_alert:msg title:title controller:vc] ;
  415. }
  416. });
  417. });
  418. }
  419. }];
  420. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  421. DebugLog(@"No");
  422. }];
  423. [alertControl addAction:actionOne];
  424. [alertControl addAction:alertthree];
  425. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  426. UIViewController *vc= [RAUtils getViewController:self];
  427. [vc presentViewController:alertControl animated:YES completion:nil];
  428. }
  429. else
  430. {
  431. int c=self.cqty+count;
  432. int m=c%self.step;
  433. if(m!=0&&appDelegate.alert_sold_in_quantities)
  434. {
  435. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",self.step] message:nil preferredStyle:UIAlertControllerStyleAlert];
  436. //block代码块取代了delegate
  437. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  438. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  439. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  440. dispatch_async(dispatch_get_main_queue(), ^{
  441. if([[return_json valueForKey:@"result"] intValue]==2)
  442. {
  443. // NSString* order_code = [return_json valueForKey:@"order_code"];
  444. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  445. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  446. if(self.shopCartBlock!=nil)
  447. {
  448. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  449. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  450. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  451. iv.image = img;
  452. // [self.contentView addSubview:iv];
  453. self.shopCartBlock(iv);
  454. self.cqty +=count;
  455. if(self.set_cqty)
  456. self.set_cqty(self.cqty);
  457. }
  458. }
  459. else
  460. {
  461. UIViewController *vc= [RAUtils getViewController:self];
  462. NSString *msg = [return_json valueForKey:@"err_msg"];
  463. NSString *title = @"Add To Cart";
  464. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  465. title = @"Add To Cart: Out of Stock";
  466. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  467. self.btnNotifyMe.enabled = YES;
  468. self.btnaddCart.enabled = NO;
  469. }
  470. [RAUtils message_alert:msg title:title controller:vc] ;
  471. }
  472. });
  473. });
  474. }];
  475. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  476. DebugLog(@"No");
  477. }];
  478. [alertControl addAction:actionOne];
  479. [alertControl addAction:alertthree];
  480. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  481. UIViewController *vc= [RAUtils getViewController:self];
  482. [vc presentViewController:alertControl animated:YES completion:nil];
  483. }
  484. else
  485. {
  486. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  487. NSDictionary* return_json = [RANetwork add_toCart:self.product_id count:count name:self.model_label.text];
  488. dispatch_async(dispatch_get_main_queue(), ^{
  489. if([[return_json valueForKey:@"result"] intValue]==2)
  490. {
  491. // NSString* order_code = [return_json valueForKey:@"order_code"];
  492. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  493. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  494. if(self.shopCartBlock!=nil)
  495. {
  496. UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  497. CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  498. UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  499. iv.image = img;
  500. // [self.contentView addSubview:iv];
  501. self.shopCartBlock(iv);
  502. self.cqty +=count;
  503. if(self.set_cqty)
  504. self.set_cqty(self.cqty);
  505. }
  506. }
  507. else
  508. {
  509. UIViewController *vc= [RAUtils getViewController:self];
  510. NSString *msg = [return_json valueForKey:@"err_msg"];
  511. NSString *title = @"Add To Cart";
  512. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  513. title = @"Add To Cart: Out of Stock";
  514. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  515. self.btnNotifyMe.enabled = YES;
  516. self.btnaddCart.enabled = NO;
  517. }
  518. [RAUtils message_alert:msg title:title controller:vc] ;
  519. }
  520. });
  521. });
  522. }
  523. }
  524. }
  525. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  526. {
  527. [super setSelected:selected animated:animated];
  528. // Configure the view for the selected state
  529. }
  530. - (IBAction)selectorOnClick:(id)sender {
  531. DebugLog(@"select click");
  532. [celldelegate SelectorClicked:(UIButton*) sender];
  533. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  534. }
  535. #pragma mark -
  536. #pragma mark Deck DataSource Protocol Methods
  537. -(NSUInteger)numberOfPhotosInPhotoStackView:(PhotoStackView *)photoStack {
  538. return [self.photos count];
  539. }
  540. -(UIImage *)photoStackView:(PhotoStackView *)photoStack photoForIndex:(NSUInteger)index {
  541. if(self.photos.count==0)
  542. return nil;
  543. return [self.photos objectAtIndex:index];
  544. }
  545. #pragma mark -
  546. #pragma mark Deck Delegate Protocol Methods
  547. -(void)photoStackView:(PhotoStackView *)photoStackView willStartMovingPhotoAtIndex:(NSUInteger)index {
  548. // User started moving a photo
  549. }
  550. -(void)photoStackView:(PhotoStackView *)photoStackView willFlickAwayPhotoFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
  551. // User flicked the photo away, revealing the next one in the stack
  552. }
  553. -(void)photoStackView:(PhotoStackView *)photoStackView didRevealPhotoAtIndex:(NSUInteger)index {
  554. self.pageControl.currentPage = index;
  555. }
  556. - (UIView *)createVideoView:(NSString*) videoID
  557. {
  558. UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 360)];
  559. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
  560. // [imageView setImage:[UIImage imageNamed:@"demo"]];
  561. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 620, 10)];
  562. titleLabel.font = [UIFont boldSystemFontOfSize:20];
  563. titleLabel.text=@"a separated player will show here:";
  564. titleLabel.numberOfLines = 0;
  565. titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
  566. // titleLabel.textAlignment = NSTextAlignmentRight;
  567. titleLabel.textAlignment = NSTextAlignmentCenter;
  568. CGSize constraintkey = CGSizeMake(620, 20000.0f);//key label width is 40% cell width;
  569. CGSize sizetitle = [@"a separated player will show here:" sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  570. //[titleLabel sizeToFit];
  571. // titleLabel.center.x = demoView.center.x;
  572. titleLabel.frame = CGRectMake(10, 10, 620, sizetitle.height);
  573. [demoView addSubview:titleLabel];
  574. UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 610, 10)];
  575. lineLabel.font = [UIFont boldSystemFontOfSize:17];
  576. lineLabel.text=videoID;
  577. lineLabel.numberOfLines = 0;
  578. lineLabel.lineBreakMode =NSLineBreakByWordWrapping;
  579. CGSize sizemsg = [videoID sizeWithFont:[UIFont boldSystemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  580. lineLabel.frame = CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 630, sizemsg.height);
  581. //[lineLabel sizeToFit];
  582. [demoView addSubview:lineLabel];
  583. demoView.frame = CGRectMake(0, 0, 640, lineLabel.frame.origin.y+lineLabel.frame.size.height+25);
  584. return demoView;
  585. }
  586. -(void)photoStackView:(PhotoStackView *)photoStackView didSelectPhotoAtIndex:(NSUInteger)index {
  587. DebugLog(@"selected %lu", (unsigned long)index);
  588. NSDictionary* item= self.stack_contents[[NSString stringWithFormat:@"item_%ld",index]];
  589. if([item[@"type"] isEqualToString:@"video"])
  590. {
  591. NSString* video_code=item[@"code"];
  592. // [TextUtils expression_varable:video regex:<#(NSString *)#>]
  593. NSString* video_id=[self.playerView Embed2VID:video_code];
  594. NSDictionary *playerVars = @{
  595. @"playsinline" : @1,
  596. @"autoplay" : @(1),
  597. @"rel":@0,
  598. @"showinfo": @0,
  599. @"modestbranding":@0,
  600. @"enablejsapi":@1
  601. };
  602. [self.playerView loadWithVideoId:video_id playerVars:playerVars];
  603. for(UIView *v in self.playerView.subviews)
  604. {
  605. if([v isKindOfClass:[UIButton class] ])
  606. [self.playerView bringSubviewToFront:v];
  607. }
  608. self.playerView.webView.allowsPictureInPictureMediaPlayback=false;
  609. self.playerView.hidden=false;
  610. return;
  611. } else {
  612. ContentPreviewController *preVC = [[UIStoryboard storyboardWithName:@"PhotoList" bundle:nil] instantiateViewControllerWithIdentifier:@"ContentPreviewController"];
  613. preVC.content = self.stack_contents;
  614. // preVC.currentIndex = index;
  615. [preVC setOffset:index];
  616. UIViewController *superVC= [RAUtils getViewController:self];
  617. [superVC.navigationController pushViewController:preVC animated:true];
  618. return;
  619. }
  620. // }
  621. // 调用展示窗口
  622. ImageScrollerViewController *imgShow = [[ImageScrollerViewController alloc] initWithSourceData:[self.photos mutableCopy] withIndex:index];
  623. UIViewController *vc= [RAUtils getViewController:self];
  624. [vc.navigationController pushViewController:imgShow animated:true];
  625. }
  626. -(void) neworder
  627. {
  628. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  629. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  630. NSDictionary* return_json = [RANetwork new_Order];
  631. dispatch_async(dispatch_get_main_queue(), ^{
  632. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  633. if([[return_json valueForKey:@"result"] intValue]==2)
  634. {
  635. int result=[[return_json valueForKey:@"result"] intValue];
  636. if(result==2)
  637. {
  638. //successed.
  639. NSString* order_code = [return_json valueForKey:@"orderCode"];
  640. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  641. appDelegate.order_code = order_code;
  642. int count=[self.quantity_text.text intValue];
  643. [self addtocart:count];
  644. }
  645. }
  646. else
  647. {
  648. UIViewController *vc= [RAUtils getViewController:self];
  649. NSString *msg = [return_json valueForKey:@"err_msg"];
  650. NSString *title = @"Add To Cart";
  651. if ([msg hasPrefix:@"Out of Stock.\n"]) {
  652. title = @"Add To Cart: Out of Stock";
  653. msg = [msg substringFromIndex:[@"Out of Stock.\n" length]];
  654. self.btnNotifyMe.enabled = YES;
  655. self.btnaddCart.enabled = NO;
  656. }
  657. [RAUtils message_alert:msg title:title controller:vc] ;
  658. }
  659. });
  660. });
  661. }
  662. #pragma mark - UIAlertViewDelegate
  663. // Called when a button is clicked. The view will be automatically dismissed after this call returns
  664. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  665. {
  666. UIApplication * app = [UIApplication sharedApplication];
  667. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  668. if(alertView.tag==1234)//库存小于0
  669. {
  670. // availability warring;
  671. int count=[self.quantity_text.text intValue];
  672. if(buttonIndex!=alertView.cancelButtonIndex)
  673. {
  674. if(appDelegate.order_code==nil)
  675. {
  676. [self neworder];
  677. }
  678. else
  679. {
  680. [self addtocart:count];
  681. }
  682. }
  683. return;
  684. }
  685. // int count=[self.quantity_text.text intValue];
  686. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  687. if(buttonIndex==alertView.cancelButtonIndex)
  688. {
  689. }
  690. else if(buttonIndex==1)
  691. {
  692. //open exist
  693. UIViewController *vc= [RAUtils getViewController:self];
  694. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  695. ovc.showNavibar = true;
  696. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  697. if(appDelegate.order_code==nil)
  698. {
  699. [self neworder];
  700. }
  701. else
  702. {
  703. if(self.set_update_data)
  704. self.set_update_data(true);
  705. int model_count = [[order_detail valueForKey:@"model_count"] intValue];
  706. for(int i=0;i<model_count;i++)
  707. {
  708. NSDictionary* item = [order_detail objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  709. NSString* product_id = [item valueForKey:@"product_id"];
  710. if([product_id isEqualToString:self.product_id])
  711. {
  712. self.cqty = [[item valueForKey:@"QTY"]intValue];
  713. if(self.set_cqty)
  714. self.set_cqty(self.cqty);
  715. break;
  716. }
  717. }
  718. int count=[self.quantity_text.text intValue];
  719. [self addtocart:count];
  720. [main_vc checklogin:true];
  721. }
  722. };
  723. ovc.init_style = OL_OPEN;
  724. ovc.onCancel = ^(){
  725. [main_vc checklogin:true];
  726. };
  727. [vc.navigationController pushViewController:ovc animated:true];
  728. }
  729. else
  730. {
  731. //create new;
  732. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  733. {
  734. UIViewController *vc= [RAUtils getViewController:self];
  735. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  736. cvc.showNavibar = true;
  737. cvc.contact_type = @"Sales_Order_Customer";
  738. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  739. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  740. appDelegate.customerInfo = value;
  741. if(appDelegate.order_code==nil)
  742. [self neworder];
  743. [main_vc checklogin:true];
  744. };
  745. cvc.onCancel = ^(){
  746. UIViewController *vc= [RAUtils getViewController:self];
  747. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order" controller:vc];
  748. };
  749. cvc.onReset = ^(){
  750. [main_vc checklogin:true];
  751. };
  752. [vc.navigationController pushViewController:cvc animated:true];
  753. }
  754. else
  755. {
  756. [self neworder];
  757. }
  758. }
  759. }
  760. - (void)valueChanged:(UIStepper *)sender {
  761. self.quantity_text.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  762. [celldelegate stepClicked:(int)sender.value];
  763. }
  764. #pragma mark -
  765. #pragma mark Actions
  766. -(void)ClearPhotos
  767. {
  768. NSMutableArray *photosMutable = [self.photos mutableCopy];
  769. [photosMutable removeAllObjects];
  770. self.photos = photosMutable;
  771. [self.photoStack reloadData];
  772. }
  773. - (void)AddPhoto :(UIImage*)photo {
  774. NSMutableArray *photosMutable = [self.photos mutableCopy];
  775. [photosMutable addObject:photo];
  776. self.photos = photosMutable;
  777. [self.photoStack reloadData];
  778. self.pageControl.numberOfPages = [self.photos count];
  779. if(self.photos.count<2-1)//1 for qrcode;
  780. self.photoStack.showBorder=false;
  781. else
  782. self.photoStack.showBorder=true;
  783. }
  784. #pragma mark -
  785. #pragma mark Setup
  786. -(void)setup {
  787. // int availability = [self.count_Label.text intValue];
  788. self.stepper.minimumValue= self.step;
  789. self.stepper.maximumValue = INTMAX_MAX;
  790. self.stepper.stepValue= self.step;
  791. self.stepper.value= self.quantity;
  792. [self.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  793. if(true)//(self.photos==nil)
  794. {
  795. self.photos = [NSArray array];
  796. }
  797. if(true)//(self.photoStack==nil)
  798. {
  799. self.photoStack.dataSource = self;
  800. self.photoStack.delegate = self;
  801. // [self addSubview:self.photoStack];
  802. self.pageControl.numberOfPages = [self.photos count];
  803. }
  804. }
  805. - (IBAction)notifyMeButtonClicked:(UIButton *)sender {
  806. if (self.notifyMeBlock) {
  807. self.notifyMeBlock();
  808. }
  809. }
  810. #pragma mark - TextField Delegate
  811. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  812. NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  813. int buyQTY = [newString intValue];
  814. self.stepper.value = buyQTY;
  815. return YES;
  816. }
  817. #pragma mark - Private
  818. /**检查是否缺货*/
  819. - (void)checkAvailability:(int)availability buyQTY:(int)buyQty cartQTY:(int)cartQty { // availability不可以通过textField.text取得
  820. //#ifdef BUILD_NPD
  821. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  822. if (!appDelegate.bLogin || buyQty < 1) {
  823. return;
  824. }
  825. #ifdef OFFLINE_MODE
  826. if (appDelegate.offline_mode) {
  827. return;
  828. }
  829. #endif
  830. if (appDelegate.user_type == USER_ROLE_CUSTOMER) { // customer
  831. if (!appDelegate.order_code) { // 未打开订单状态
  832. if ((buyQty + cartQty) > availability) {
  833. self.btnaddCart.enabled = NO;
  834. self.btnNotifyMe.enabled = YES;
  835. } else {
  836. self.btnaddCart.enabled = YES;
  837. self.btnNotifyMe.enabled = NO;
  838. }
  839. } else {
  840. // 打开订单状态
  841. if (![Singleton sharedInstance].currentOrderIsMerged) {
  842. // Shop Order
  843. self.btnaddCart.enabled = YES;
  844. // 默认未缺货
  845. self.btnNotifyMe.enabled = NO;
  846. if ((buyQty + cartQty) > availability) { // 缺货
  847. self.btnNotifyMe.enabled = YES;
  848. }
  849. } else {
  850. // Purchase Order
  851. if ((buyQty + cartQty) > availability) { // 缺货
  852. self.btnaddCart.enabled = NO;
  853. self.btnNotifyMe.enabled = YES;
  854. } else { // 未缺货
  855. self.btnNotifyMe.enabled = NO;
  856. self.btnaddCart.enabled = YES;
  857. }
  858. }
  859. }
  860. } else { // empoyee
  861. if ((buyQty + cartQty) > availability) { // 缺货
  862. self.btnaddCart.enabled = NO;
  863. self.btnNotifyMe.enabled = YES;
  864. } else { // 未缺货
  865. self.btnNotifyMe.enabled = NO;
  866. self.btnaddCart.enabled = YES;
  867. }
  868. }
  869. //#endif
  870. }
  871. - (IBAction)introduceBtnClick:(UIButton *)sender {
  872. if (self.introduceBlock) {
  873. self.introduceBlock(sender);
  874. }
  875. }
  876. - (IBAction)descriptionBtnClick:(UIButton *)sender {
  877. if (self.descriptionBlock) {
  878. self.descriptionBlock(sender);
  879. }
  880. }
  881. - (IBAction)OnClosePlayer:(id)sender {
  882. [self.playerView stopVideo];
  883. self.playerView.hidden=true;
  884. }
  885. @end