//
//  ConfirmOrderVC.m
//  PayDemo
//
//  Created by Mac Book DQ02 on 16/6/20.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "ConfirmOrderVC.h"


@interface ConfirmOrderVC ()

@end

@implementation ConfirmOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"确认订单";
    //改变字体颜色
    //     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:(UIBarMetricsDefault)];
//    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
//    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    self.navigationController.navigationBar.titleTextAttributes = textAttrs;
    
    [Manager setNavi:self.navigationController.navigationBar];

    
    //设置导航栏左边按钮
    //   self.navigationItem.leftBarButtonItem = [Manager rightItemTarget:self img:@"nav_back" tag:1000000 action:@selector(leftBarButtonItemIsClicked:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_back"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemClicked)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.receiverTel.keyboardType = UIKeyboardTypePhonePad;
    
}

#pragma mark 返回按钮事件
-(void)leftBarButtonItemClicked
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark --加减号按钮事件，改变商品购买数量
- (IBAction)changBuyCountClicked:(UIButton *)sender {
    if (sender.tag == 101) {
        _buyCountTF.text = [NSString stringWithFormat:@"%d",[_buyCountTF.text intValue]+1];
    }
    else
    {
        if ([_buyCountTF.text intValue]<2) {
            return;
        }
        _buyCountTF.text = [NSString stringWithFormat:@"%d",[_buyCountTF.text intValue]-1];

    }
    [self setFinalBuy];
}

#pragma mark --数量文本框手动编辑事件
- (IBAction)buyCountTFEndEdit:(id)sender {
    [self setFinalBuy];
}

#pragma mark --设置最终购买数量和总价格标签内容
-(void)setFinalBuy
{
    _buyCountLab.text = _buyCountTF.text;
    _totalPriceLab.text = [NSString stringWithFormat:@"¥%.2f",[_buyCountTF.text intValue]*5.00];
}

#pragma mark --确认订单按钮事件
- (IBAction)orderConfirmBtnClicked:(id)sender {
    
    if ([_receiverName.text isEqualToString:@""] || [_receiverTel.text isEqualToString:@""] || [_receiverAddress.text isEqualToString:@""]) {
        UIAlertController *action = [UIAlertController alertControllerWithTitle:@"订单提交失败" message:@"请您完善信息！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:nil];
        [action addAction:ensure];
        [self presentViewController:action animated:NO completion:nil];
        
        return;
    }

    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = @"2088021830678838";
    order.sellerID = @"gaotingshan517@163.com";
    order.outTradeNO = [self generateTradeNO]; //订单ID（由商家自行制定）
    order.subject = @"登山卡"; //商品标题
    order.body = @"登山卡"; //商品描述
   // order.totalFee = [NSString stringWithFormat:@"%.2f",[_buyCountTF.text intValue] * 5.00]; //商品价格
   order.totalFee = [NSString stringWithFormat:@"%.2f",0.01]; //商品价格
    order.notifyURL =  @"http://www.xxx.com"; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showURL = @"m.alipay.com";

    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);

    NSDictionary *parameterDic = [[NSDictionary alloc]initWithObjectsAndKeys:orderSpec,@"orderInfo", nil];
    
    [Manager alipayRequest:parameterDic orderSpec:orderSpec flag:1];
    
 }

#pragma mark -
#pragma mark   ==============产生随机订单号==============


- (NSString *)generateTradeNO
{
    static int kNumber = 15;
    
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}



- (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
