//
//  OrderWebViewController.m
//  ALand
//
//  Created by Javen on 16/8/26.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "OrderWebViewController.h"
#import "OrderViewController.h"
@interface OrderWebViewController ()

@end

@implementation OrderWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"AppModel"]) {
        // 打印所传过来的参数，只支持NSNumber, NSString, NSDate, NSArray,
        // NSDictionary, and NSNull类型
        /**
         *  body = 000100012016080100012;
         */
        NSString *billNumber = message.body;
        NSDictionary *dic = billNumber.mj_keyValues;
        self.payView = [PayView viewWithNibName:@"PayView"];
        self.payView.billUuid = dic[@"body"];
        WS(weakSelf);
        [self.payView show];
        self.payView.completion = ^(NSString *result, PingppError *error) {
            [weakSelf.payView hide];
            if (result) {
                NSString *info;
                if ([result isEqualToString:@"cancel"]) {
                    info = @"支付未完成";
                }else if ([result isEqualToString:@"success"]){
                    info = @"支付成功";
                }else if ([result isEqualToString:@"fail"]){
                    info = @"支付失败";
                }
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:info message:@"订单可在订单管理中查看" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *toOrders = [UIAlertAction actionWithTitle:@"查看订单" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    for (id vc in weakSelf.navigationController.viewControllers) {
                        if ([vc isKindOfClass:[OrderViewController class]]) {
                            [weakSelf.navigationController popToViewController:vc animated:YES];
                        }
                    }
                }];
                [alert addAction:toOrders];
                
                UIAlertAction *continueBuy = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [weakSelf.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:weakSelf.URL]]];
                }];
                [alert addAction:continueBuy];
                [weakSelf.navigationController PresentModalViewController:alert animated:YES];
                
            }
        };
        
        NSLog(@"%@", message.body);
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
