var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data:{
    order_id:'',
    list:[],//商品信息列表
    payment:[],//支付方式信息列表
    total_price:0,
    payment_id:0,
    total_info:[],
    is_hiden:true,
    use_account:0,
    is_showshop: false,//是否显示自提门店
    is_showmask: false,
    is_pwd: false,//是否显示密码
    is_payment: false,
  },
  onLoad: function (options) {//接收上个页面传的参数
    this.setData({
      order_id: options.order_id,
      param:{
        order_id: options.order_id,
        type:14
      }
    });
    var self = this;
    app.HCsetNavigationBarTitle('继续付款')
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function (ret) {
        self.getIndex();
    });
  },
  getIndex:function(){
    var self = this;
    var action = 'c=Order&a=continue_payhtml';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data, function (ret) {
      //console.log(ret);
      if (ret.status == 1) {
        self.setData({
          list: ret.data,
          total_price: ret.data.order_amount,
          pay_price: ret.data.order_amount,
          memberinfo:ret.data.memberinfo,
        });
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    },true);
  },
  //关闭遮罩
  cloce_mask: function () {
    if (this.data.is_pwd == true) {
      this.setData({
        is_pwd: false,
      })
    }
    if (this.data.is_payment == true) {
      this.setData({
        is_payment: false
      })
    }
    this.setData({
      is_showmask: false,
    })
  },
  //选择余额
  switch1Change: function (e) {
    var use_account;
    if (e.detail.value == true) {
      use_account = 1;
    } else {
      use_account = 0;
    }
    var param = this.data.param;
    param.use_account = use_account;
    this.setData({
      param: param
    });
    this.count_total();
  },
  //选择积分
  switch2Change: function (e) {
    var score;
    if (e.detail.value == true) {
      score = 1;
    } else {
      score = 0;
    }
    var param = this.data.param;
    param.score = score;
    this.setData({
      param: param
    });
    this.count_total();
  },
  //计算总价
  count_total:function(){
    var self=this;
    var action = 'c=Order&a=continue_count_total';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data.param, function (ret) {
      if (ret.status == 1) {
        self.setData({
          total_info: ret.total_info,
          total_price: ret.total_info.total_price,
          pay_price: ret.total_info.pay_price,
        });
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  //选择支付方式
  radiopayChange: function (e) {
    var payment_id = e.detail.value;
    var param = this.data.param;
    param.payment_id = payment_id;
    this.setData({
      param: param
    });
    this.count_total();
  },
  //设置支付密码
  setpwd: function (e) {
    var param = this.data.param;
    param.pay_pwd = e.detail.value;
    this.setData({
      param: param
    });
  },
  //提交订单
  sub_order: function () {
    var self = this;
    if (self.data.pay_price > 0 && (self.data.param.use_account > 0 || self.data.param.score > 0)) {
      self.setData({
        is_pwd: true,
        is_payment: true,
        is_showmask: true
      });
    } else if (self.data.pay_price > 0 && self.data.param.use_account == 0) {
      self.setData({
        is_payment: true,
        is_showmask: true
      });
    } else if (self.data.param.use_account > 0 || self.data.param.score > 0 && self.data.pay_price == 0) {
      self.setData({

        is_pwd: true,
        is_showmask: true
      });
    } else {
      self.balance();
    }
  },
  balance:function(){
    var self=this;
    var action = 'c=Order&a=sub_continue_order';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data.param, function (ret) {
      if (ret.status == 1) {
        if (ret.data && ret.data.order_id && self.payment_id != 12) {
          self.goToDetaile(ret.data.order_id);
        } else if (ret.data && ret.data.pay_id) {
          self.setData({
            id: ret.data.pay_id,
          });
          self.web_pay(ret.data.pay_id);
        }
      } else {
        if (ret.data.url == 3) {
          app.HCerror('提示', ret.msg, true, function (ret) {
            if (ret.confirm) {
              self.jumprespwd();
            }
          }); return;
        }
        if (ret.data.app_pay == 1 && self.data.param.use_account > 0 || self.data.param.score > 0) {
          self.setData({
            is_pwd: true,
            is_payment: true,
            is_showmask: true
          });
          return;
        }
        if (ret.data.app_pay == 1) {
          self.setData({
            is_payment: true,
            is_showmask: true
          });
          return;
        }
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  web_pay: function (pay_id) {
    var self = this;
    var action = 'c=Wxsmallpay&a=pay_done';
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', self.data, function (ret) {
      if (ret.status == 1) {
        app.HCrequestPayment(ret.wxsmallcode, function (ret) {
          if (ret.status == 1) {
            app.HCswitchTab('../../userinfo/index/index');
          } else {
            app.HCerror('提示', '支付失败'); app.HCswitchTab('../../userinfo/index/index'); return;
          }
        });
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    }, true);
  },
  //跳转
  goToDetaile: function (e) {
    app.HCredirectTo('../../buycart/order_success/order_success?order_id=' + e);
  }, 
  jumprespwd: function () {
    app.HCnavigateTo('../../userinfo/reset_pwd/reset_pwd');
  },
})